String str = String.valueOf(d);
publicclassTestDateSort{publicstaticvoidmain(String[]args){Date[]days=newDate[5];days...
public class TestDateSort {
public static void main(String[] args) {
Date[] days = new Date[5];
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);
Date d = new Date(2006, 7, 4);
String str = String.valueOf(d);
//str = d.toString();
bubbleSort(days);
for(int i=0; i<days.length; i++) {
System.out.println(days[i]);
}
System.out.println(binarySearch(days, d));
}
public static Date[] bubbleSort(Date[] a){
int len = a.length;
for(int i = len-1;i>=1;i--){
for(int j = 0;j<=i-1;j++){
if(a[j].compare(a[j+1]) > 0){
Date temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
public static int binarySearch(Date[] days, Date d) {
if (days.length==0) return -1;
int startPos = 0;
int endPos = days.length-1;
int m = (startPos + endPos) / 2;
while(startPos <= endPos){
if(d.compare(days[m]) == 0) return m;
if(d.compare(days[m]) > 0) {
startPos = m + 1;
}
if(d.compare(days[m]) < 0) {
endPos = m -1;
}
m = (startPos + endPos) / 2;
}
return -1;
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y; month = m; day = d;
}
public int compare(Date date) {
return year > date.year ? 1
: year < date.year ? -1
: month > date.month ? 1
: month < date.month ? -1
: day > date.day ? 1
: day < date.day ? -1 : 0;
}
public String toString() {
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
String str = String.valueOf(d);这句是什么意思啊?它的作用是什么? 展开
public static void main(String[] args) {
Date[] days = new Date[5];
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);
Date d = new Date(2006, 7, 4);
String str = String.valueOf(d);
//str = d.toString();
bubbleSort(days);
for(int i=0; i<days.length; i++) {
System.out.println(days[i]);
}
System.out.println(binarySearch(days, d));
}
public static Date[] bubbleSort(Date[] a){
int len = a.length;
for(int i = len-1;i>=1;i--){
for(int j = 0;j<=i-1;j++){
if(a[j].compare(a[j+1]) > 0){
Date temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
public static int binarySearch(Date[] days, Date d) {
if (days.length==0) return -1;
int startPos = 0;
int endPos = days.length-1;
int m = (startPos + endPos) / 2;
while(startPos <= endPos){
if(d.compare(days[m]) == 0) return m;
if(d.compare(days[m]) > 0) {
startPos = m + 1;
}
if(d.compare(days[m]) < 0) {
endPos = m -1;
}
m = (startPos + endPos) / 2;
}
return -1;
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y; month = m; day = d;
}
public int compare(Date date) {
return year > date.year ? 1
: year < date.year ? -1
: month > date.month ? 1
: month < date.month ? -1
: day > date.day ? 1
: day < date.day ? -1 : 0;
}
public String toString() {
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
String str = String.valueOf(d);这句是什么意思啊?它的作用是什么? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询