进阶day03-Collections中的addAll和shuffle方法
This commit is contained in:
40
javaSE-day03/src/com/inmind/collections04/Demo13.java
Normal file
40
javaSE-day03/src/com/inmind/collections04/Demo13.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package com.inmind.collections04;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
/*
|
||||
- java.utils.Collections是集合工具类,用来对集合进行操作。部分方法如下:
|
||||
|
||||
- public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
|
||||
- public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
|
||||
|
||||
|
||||
- public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
|
||||
- public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
|
||||
|
||||
*/
|
||||
public class Demo13 {
|
||||
public static void main(String[] args) {
|
||||
//public static <T> boolean addAll(Collection<T> c, T... elements):往集合中添加一些元素。
|
||||
Collection<String> lists = new ArrayList<>();
|
||||
lists.add("关羽");
|
||||
lists.add("刘备");
|
||||
lists.add("张飞");
|
||||
|
||||
System.out.println(lists);
|
||||
System.out.println(Collections.addAll(lists, "张三", "张三1", "张三2"));
|
||||
System.out.println(lists);
|
||||
System.out.println("--------------------------------------------");
|
||||
//- public static void shuffle(List<?> list) 打乱顺序:打乱集合顺序。
|
||||
ArrayList<String> lists1 = new ArrayList<>();
|
||||
lists1.add("关羽");
|
||||
lists1.add("刘备");
|
||||
lists1.add("张飞");
|
||||
System.out.println(lists1);
|
||||
Collections.shuffle(lists1);
|
||||
System.out.println(lists1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user