//注意:Collection值接口,Collections是类class CollectionDemo1{ public static void fun1(){ List<String> str=new ArrayList<>(); str.add("zhangsan"); str.add("wangwu"); str.add("liliu"); System.out.println("交换顺序之前:"); for (String string : str) { System.out.println(string+" "); } System.out.println(); Collections.swap(str, 1, 2); System.out.println("交换顺序之后:"); for (String string : str) { System.out.println(string+" "); } System.out.println(); Collections.sort(str); System.out.println("按照自然顺序排列:");//会按照字符串的顺序排列 for (String string : str) { System.out.println(string+" "); } System.out.println("二分法查找:"); int s = Collections.binarySearch(str, "wangwu"); System.out.println(s); System.out.println("打乱顺序"); Collections.shuffle(str); for (String string : str) { System.out.println(string+" "); } System.out.println("填充:"); Collections.fill(str, "Jolin"); for (String string : str) { System.out.println(string+" "); //内容全部会变成Jolin } } }
时间: 2024-09-29 09:21:05