package s0221Collections类; |
//提供了很多操纵容器对象的方法 |
import java.util.ArrayList; |
import java.util.Collections; |
import java.util.List; |
public class Main { |
public static void main(String[] args) { |
List<Integer> list = new ArrayList<Integer>(); |
List<Integer> list2= new ArrayList<Integer>(); |
|
|
Collections.addAll(list, 1 , 2 , 3 , 4 , 5 ); //addAll(Collection c, T... elements)添加指定元素到容器中 |
Collections.addAll(list2, 1 , 2 , 3 , 4 , 5 , 6 ); |
System.out.println( "初始状态" +list); |
|
|
Collections.reverse(list); |
System.out.println( "反转容器元素" +list); |
|
|
Collections.shuffle(list); |
System.out.println( "元素打乱顺序,可以模拟洗牌" +list); |
|
|
Collections.copy(list2,list); //容器元素复制,保证复制后的容器空间比原来的容器大,不然会报错 |
System.out.println( "list2=" +list2); |
|
Collections.swap(list2, 0 , 1 ); |
System.out.println( "list2交换位置:" +list2); |
|
Collections.fill(list, 3 ); //使用指定元素替换指定列表中的所有元素。 |
System.out.println( "list=" +list); |
|
int n=Collections.frequency(list, 3 ); |
System.out.println( "返回某个对象在容器中的数目:" +n); |
|
|
System.out.println( "返回最大元素" +Collections.max(list2)); |
|
Collections.replaceAll(list, 3 , 4 ); |
System.out.println( "替换所有3为4,list=" +list); |
|
Collections.synchronizedList(list); //使list变成线程安全的 |
|
|
} |
} |