/**
* 集合排序
* 使用排序这个方法,如果一个自定义的类一定要实现comparable接口,否则会出现类型转换异常,string类本身已经实现好了comparable接口
*/
private static void test09() {
List<String> list = new ArrayList<>();
Collections.addAll(list, "wang", "1", "8");
Collections.addAll(list, "a", "d");
Collections.addAll(list, "b", "c", "e");
System.out.println("排序之前的集合:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + "、");
}
Collections.sort(list);//将集合重新排序
System.out.println("\n" + "排序之后的集合:");
Iterator<String> iet = list.iterator();
while (iet.hasNext()) {
System.out.print(iet.next() + "、");
}
}