import java.util.*;
class TreeSetTest3
{
public static void main(String[] args)
{
TreeSet ts=new TreeSet(new StringLength());
ts.add("aaa");
ts.add("bdaaa");
ts.add("aca");
ts.add("ab");
ts.add("acdt");
Iterator it= ts.iterator();
while (it.hasNext())
{
System.out.println(it.next());
}
}
}
class StringLength implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=(String)o1;
String s2=(String)o2;
int num=s1.length()-s2.length();
//int num=new Integer (s1.length()).compareTo (new Integer(s2.length()));
if (num==0)
return s1.compareTo(s2);
return num;
}
}
老师为什么不用注释上面的那种方法 代码更少啊 是不是有什么缺陷? |