public static void main(String[] args) {
// 0、给List排序
List<Integer> list = new ArrayList<Integer>();
int array[] = { 6, 3, 4, 9, 0, 2, 5, 7, 1, 8 };
for (int i = 0; i < array.length; i++) {
list.add(new Integer(array[i]));
}
System.out.println("排序前:");
System.out.println(list); System.out.println("排序后:");
Collections.sort(list);
System.out.println(list);
System.out.println("==================================");
// 1、打乱List的顺序
List<Integer> list1 = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list1.add(new Integer(i));
}
System.out.println("打乱前:");
System.out.println(list1); System.out.println("打乱后:");
Collections.shuffle(list1);// 打乱List
System.out.println(list1);
System.out.println("==================================");
// 2、倒序排列List
List<Integer> list2 = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list2.add(new Integer(i));
}
System.out.println("倒叙前:");
System.out.println(list2); System.out.println("倒叙后:");
Collections.reverse(list2);// 倒序排列List
System.out.println(list2);
System.out.println("==================================");
// 3、使用指定元素替换指定列表中的所有元素。
List<String> list3 = new ArrayList<String>();
String str[] = { "dd", "aa", "bb", "cc", "ee" };
for (int j = 0; j < str.length; j++) {
list3.add(new String(str[j]));
}
System.out.println("替换前:");
System.out.println(list3); System.out.println("替换后:");
Collections.fill(list3, "gg");
System.out.println(list3);
System.out.println("==================================");
// 4、返回给定 collection 的最小元素和最大元素
List<Integer> list4 = new ArrayList<Integer>();
int arr[] = { 6, 3, 4, 9, 0, 2, 5, 7, 1, 8 };
for (int i = 0; i < arr.length; i++) {
list4.add(new Integer(arr[i]));
}
System.out.println("集合内容为:");
System.out.println(list4); System.out.println("集合中最小值为:" + Collections.min(list4));
System.out.println("集合中最大值为:" + Collections.max(list4));
System.out.println("==================================");
// 5、用两个参数,一个目标 listTarget 和一个源 listSrc, 将源的元素拷贝到目标,并覆盖它的内容。目标 List
// 至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。
int shuzu1[] = { 111, 222, 333, 444, 555 };
List<Integer> listTarget = new ArrayList<>();
for (int i = 0; i < shuzu1.length; i++) {
listTarget.add(new Integer(shuzu1[i]));
}
System.out.println("目标集合被覆盖前:");
System.out.println(listTarget); int shuzu2[] = { 666, 777 };
List<Integer> listSrc = new ArrayList<>();
for (int j = 0; j < shuzu2.length; j++) {
listSrc.add(new Integer(shuzu2[j]));
}
System.out.println("目标集合被覆盖后:");
Collections.copy(listTarget, listSrc);/// 后面一个参数是目标列表 ,前一个是源列表
System.out.println(listTarget);
System.out.println("=================================="); } 排序前:
[6, 3, 4, 9, 0, 2, 5, 7, 1, 8]
排序后:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
==================================
打乱前:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
打乱后:
[3, 0, 5, 7, 8, 1, 6, 4, 2, 9]
==================================
倒叙前:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
倒叙后:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
==================================
替换前:
[dd, aa, bb, cc, ee]
替换后:
[gg, gg, gg, gg, gg]
==================================
集合内容为:
[6, 3, 4, 9, 0, 2, 5, 7, 1, 8]
集合中最小值为:0
集合中最大值为:9
==================================
目标集合被覆盖前:
[111, 222, 333, 444, 555]
目标集合被覆盖后:
[666, 777, 333, 444, 555]
==================================
05-11 02:35