(1) 自己写循环,一个一个比较
Date[] dates = ...
Date min = null;
Date max = null;
for(int i=0; i<dates.length; i++) {
if(i==0) {
min = dates[i];
max = dates[i];
} else {
if(dates[i].compareTo(min)<0) {min=dates[i];}
if(dates[i].compareTo(max)>0) {max=dares[i];}
}
}
(2) 用SortedSet.
Date[] dates = ...;
SortedSet set = new TreeSet(Arrays.asList(dates));
Date min = set.first();
Date max = set.last();
Date[] dates = ...
Date min = null;
Date max = null;
for(int i=0; i<dates.length; i++) {
if(i==0) {
min = dates[i];
max = dates[i];
} else {
if(dates[i].compareTo(min)<0) {min=dates[i];}
if(dates[i].compareTo(max)>0) {max=dares[i];}
}
}
(2) 用SortedSet.
Date[] dates = ...;
SortedSet set = new TreeSet(Arrays.asList(dates));
Date min = set.first();
Date max = set.last();
也可以用 Date 对象的 getTime() 取得一个 long 值来比较。有比较办法之后要查最大最小的就不用多说了吧?!
接口,可以放入到SortedSet里面
通过treemap利用Comparable接口排序
Date d1=(Date)o1;
Date d2=(Date)o2;
if(d1.after(d2)){
return 1;
}
return 0;
}
看错了,以为是比较List里面的数据。
Date[] array = new Date[100];
..........//
List resutList = Arrays.asList(array);
Comparator c = new Follow();
Collections.sort(resutList, c);
}}class Follow implements Comparator {
Follow(){
}
public int compare(Object o1, Object o2) {
Date d1 = (Date) o1;
Date d2 = (Date) o2;
if (d1.after(d2)) {
return 1;
}
return 0;
}
}