- import java.util.*;
- class CollectionsDemo
- {
- public static void main(String[] args)
- {
- sortDemo();
- }
- public static void sortDemo()
- {
- List<String> list = new ArrayList<String>();
- list.add("abcd");
- list.add("aaa");
- //list.add("zz");
- list.add("kkkkk");
- list.add("qq");
- list.add("z");
-
- //System.out.println(list);
- //Collections.sort(list);
- //Collections.sort(list);
- //Collections.swap(List,1,2);
- //int index = Collections.binarySearch(list,"abcd");
- int index = Collections.binarySearch(list,"qq",new StrLenComparator());
- System.out.println(list);
- System.out.println("index = "+index);
- //Collections.max(List);
- }
- }
- class StrLenComparator implements Comparator<String>
- {
-
- public int compare(String s1,String s2)
- {
- if(s1.length() > s2.length())
- return 1;
- if(s1.length() < s2.length())
- return -1;
- return s1.compareTo(s2);
-
- }
- }
复制代码
请教一下,为什么输出结果是-1呢,难道我的代码有欠缺吗?
|
|