没有养成写注释习惯,找问题有点难,代码如下:
package Test;
import java.util.*;
public class Test1 {
public static void main(String[] args) {
TreeSet ts = new TreeSet(new myCom());
ts.add("a");
ts.add("b");
ts.add("abcd");
ts.add("abc");
Iterator it = ts.iterator();
while (it.hasNext()) {
sop(it.next());
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
// 自定义比较器
class myCom implements Comparator {
public int compare(String str1, String str2) {
int num = new Integer(str1.length()).compareTo(str2.length());
if (num == 0)
return s1.compareTo(s2);
return num;
}
}
打印结果就一个啊!问题出在哪啊?
|
|