本帖最后由 郭冰川 于 2013-1-28 18:52 编辑
有点疑问,具体在代码注释上面
import java.util.*;
class TreeSetTest
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new MyCompare());
ts.add("abcd");
ts.add("cc");
ts.add("cba");
ts.add("aaa");
ts.add("z");
ts.add("hahaha");
Iterator it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class MyCompare implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1 = (String)o1;
String s2 = (String)o2;
//int num = s1.length() - s2.length();
//这里有点疑问,毕老师一直用下面转成Integer的方法,我觉得上面那个方法也挺好用的,求高手给分析下区别
int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
if (num == 0)
{
return s1.compareTo(s2);
}
return num;
}
}
|