import java.util.*;public class TestCollections {
public static void main(String[] args) {
List l1 = new LinkedList();
for(int i=0; i<=9; i++) {
l1.add("a"+i);
}
System.out.println(l1);
Collections.shuffle(l1); // 随机输出
System.out.println(l1);
Collections.sort(l1); // 进行顺序排列
System.out.println(l1);
Collections.reverse(l1); //逆序排列
System.out.println(l1);
System.out.println(Collections.binarySearch(l1,"a4")); // 二分法查找位置
}
}为什么最后输出a4的位置为-1 ,a9的位置为-11……
public static void main(String[] args) {
List l1 = new LinkedList();
for(int i=0; i<=9; i++) {
l1.add("a"+i);
}
System.out.println(l1);
Collections.shuffle(l1); // 随机输出
System.out.println(l1);
Collections.sort(l1); // 进行顺序排列
System.out.println(l1);
Collections.reverse(l1); //逆序排列
System.out.println(l1);
System.out.println(Collections.binarySearch(l1,"a4")); // 二分法查找位置
}
}为什么最后输出a4的位置为-1 ,a9的位置为-11……
====>也就是说,没有找到你的“a4”元素。注意,对象的比较
System.out.println(l1);
Collections.shuffle(l1);
System.out.println(Collections.binarySearch(l1, "a9")); // 二分法查找位置
System.out.println(l1);// 随机输出
Collections.sort(l1); // 进行顺序排列
System.out.println(l1);
System.out.println(Collections.binarySearch(l1, "a9")); // 二分法查找位置
// Collections.reverse(l1); // 逆序排列
System.out.println(Collections.binarySearch(l1, "a9")); // 二分法查找位置
System.out.println(l1);
System.out.println(Collections.binarySearch(l1, "a9")); // 二分法查找位置
}
}9
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
4
[a5, a2, a0, a1, a9, a4, a6, a7, a8, a3]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
9
9
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
9
=====》问题出在函数上。