- import java.util.*;
- class TreeSetTest
- {
- public static void main(String[] args)
- {
- TreeSet ts = new TreeSet(new strlencomparator());
- ts.add("abc");
- ts.add("aac");
- ts.add("abdc");
- ts.add("accad");
- ts.add("abca");
- Iterator it = ts.iterator();
- while (it.hasNext())
- {
- System.out.println(it.next());
- }
- }
- class strlencomparator implements Comparator
- {
- public int compare(object o1,object o2)
- {
- String s1 = (String)o1;
- String s2 = (String)o2;
- if(s1.length() > s2.length())
- return 1;
- if(s1.length() < s2.length())
- return -1;
- return 0;
- }
- }
- }
复制代码 以上代码错误如下:
|
|