Java数组中的排序和二分查找
2018-01-11 17:52
549 查看
【题目】
建立一个关于日期的类,其中包括日期比较的类,并且重写toString方法。在main函数中创建一个find()方法,利用二分查找的方法找出日期,若有则返回其在数组中的位置,若不存在则给出提示。
【相关内容】
一、toString方法的重写
toString回反应当前对象的自我描述。
在System.out.print()方法中如果插入的是一个对,就会调用对象中的toString方法。
如果在类中重写toString方法
二、日期的排序(自定义排序的书写)
在冒泡排序的基础上,可以在类中创建cmp()方法来实现日期之间的比较。
顺序为 年份--》月份--》日期
返回不同的值,来判日期之间的大小。若比当前日期大则返回1,若相同返回0,小则返回-1。
二分查找建立在一个有序序列上,每次查找序列的中间值进行比较,并更新左右位置的数值。
middle = left + ( right - left) / 2
代码:
建立一个关于日期的类,其中包括日期比较的类,并且重写toString方法。在main函数中创建一个find()方法,利用二分查找的方法找出日期,若有则返回其在数组中的位置,若不存在则给出提示。
【相关内容】
一、toString方法的重写
toString回反应当前对象的自我描述。
在System.out.print()方法中如果插入的是一个对,就会调用对象中的toString方法。
package testCode123; class people{ String name; people(String n){ this.name = n; } } public class TestCode { public static void main(String[] args){ people p = new people("小明"); System.out.println(p); } }输出为 testCode123.people@6f7a29a1
如果在类中重写toString方法
class people{ String name; people(String n){ this.name = n; } public String toString() { return "我的名字是:"+name; } }输出为 我的名字是:小明 所以我们可以通过重写toString方法来实现我们想要的输出。
二、日期的排序(自定义排序的书写)
在冒泡排序的基础上,可以在类中创建cmp()方法来实现日期之间的比较。
顺序为 年份--》月份--》日期
返回不同的值,来判日期之间的大小。若比当前日期大则返回1,若相同返回0,小则返回-1。
public int cmp(Date a){ return year>a.year ? 1 :year < a.year ? -1 :month > a.month ? 1 :month < a.month ? -1 :day > a.day ? -1 :day < a.day ? 1 : 0; }三、二分查找
二分查找建立在一个有序序列上,每次查找序列的中间值进行比较,并更新左右位置的数值。
middle = left + ( right - left) / 2
代码:
package testCode123;
class Date{
int year;
int month;
int day;
Date(int y,int m,int d){
this.year=y;this.month=m;this.day=d;
}
public int cmp(Date a){ return year>a.year ? 1 :year < a.year ? -1 :month > a.month ? 1 :month < a.month ? -1 :day > a.day ? -1 :day < a.day ? 1 : 0; }
public String toString(){
return year + "年" + month + "月" +
day + "日";
}
}
public class TestDateSort {
public static void main(String[] args){
Date[] days = new Date[10];
days[0] = new Date(2006, 5, 4);
days[1] = new Date(2006, 7, 4);
days[2] = new Date(2008, 5 ,4);
days[3] = new Date(2004, 5, 9);
days[4] = new Date(2004 ,5, 4);
days[5] = new Date(2005 ,2,18);
days[6] = new Date(2007, 10,29);
days[7] = new Date(2002,11,28);
days[8] = new Date(2010,6,3);
days[9] = new Date(2007,1,6);
bubbleSort(days);
for(int i = 0; i < days.length ; i++ ){
/*
System.out.println(days[i].year + "年" + days[i].month + "月" +
days[i].day + "日");
*/
System.out.println(days[i]); //当我们直接打印一个对象的时候,我们调用其中的toString方法
}
Date test1 = new Date(2010,6,3);
int l = find(test1,days);
if(l==-1)
System.out.println("未找到当前日期!!!");
else
System.out.println(l);
}
public static Date[] bubbleSort(Date[] a){
int len = a.length;
for(int i = 0; i < len; i++){
for(int j = 0 ;j<i;j++){
if(a[j].cmp(a[i])>0){ //返回值去和0进行比较,大于零说明a[j]要比a[i]大 所以大的就要往后走
Date z;
z = a[i];
a[i] = a[j];
a[j] = z;
}
}
}
return a;
}
public static int find(Date obj,Date[] cal){
int location= 0;
int left,right,middle;
left = 0;
right = cal.length-1;
while(left <= right){
middle = left + (right - left)/2;
if(obj.cmp(cal[middle])==0)
{
location = middle;
return location;
}
else if(obj.cmp(cal[middle])>0){
left = middle + 1;
}else{
right = middle - 1;
}
//System.out.println(left + " " +right +" "+ middle);
}
return -1;
}
}
相关文章推荐
- 黑马程序员_Java基础_04数组main函数args详解、数组排序、二分查找实例
- java基础知识4--数组,排序,二分查找
- 栋哥带你学Java数组排序和二分查找
- Java数组遍历、求最大值、选择排序、冒泡排序、二分查找
- JAVA数组排序——二分(折半)查找
- JAVA基础复习十三-StringBuffer、数组排序、二分查找、自动拆装箱
- java数组排序,二分查找
- 【Java版本】二分查找&数字在排序数组中出现的次数
- 已排序数组二分查找
- 二分查找的一个题---找出排序数组中绝对值最小的数
- java数组排序与折半查找
- java冒泡,选择排序及折半(二分)查找
- Java数组排序和查找
- Java数组及数组排序、查找
- 重学java之随即数组二分查找
- 剑指Offer--数组在排序数组中出现的次数--二分查找的应用
- 使用java从一个整数数组中查找第二大的数,仅用一次循环,不使用java自带的排序
- 小米笔试:循环排序数组二分查找
- java基础--常用数组的一些操作方法-查找-最值-排序
- 重学java之随机数组二分查找