package sort.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
-
Collections工具类
-
1.binarySearch(List<? extends Comparable<? super T>> list,T key) 二分法查找, 首先容器有序
-
2.sort(List list)根据元素的自然顺序 对指定列表按升序进行排序
-
sort(List list, Comparator<? super T> c) 根据指定比较器产生的顺序对指定列表进行排序。
-
3.reverse(List<?> list) 反转指定列表中元素的顺序
-
4.shuffle(List<?> list) 打乱顺序;模拟洗牌过程
-
5.swap(List<?> list, int i, int j) 在指定列表的指定位置处交换元素。
*/
public class CollectionsDemo01 {//Collections对象的工具类
public static void main(String[] args) {
List cards = new ArrayList();//cards底牌
//shuffle洗牌 ,模拟斗地主
for(int i=1; i<=54;i++){cards.add(i);//获取牌 } //自动装箱 洗牌 Collections.shuffle(cards);//打乱顺序 //依次发牌 给3个人 List<Integer> p1 = new ArrayList<Integer>(); List<Integer> p2 = new ArrayList<Integer>(); List<Integer> p3 = new ArrayList<Integer>(); List<Integer> last = new ArrayList<Integer>();//底牌 for(int i=0; i<51;i+=3){//留3张底牌 p1.add(cards.get(i));//获取牌 p2.add(cards.get(i+1));//获取牌 p3.add(cards.get(i+2));//获取牌 } //3张底牌 last.add(cards.get(51)); last.add(cards.get(52)); last.add(cards.get(53)); System.out.println("张三:"+p1); System.out.println("李四:"+p2); System.out.println("王五:"+p3); System.out.println("底牌为:"+last); System.out.println("***********************"); test1();
}
//反转
public static void test1(){
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);System.out.println(list);//打印 //reverse反转 Collections.reverse(list); System.out.println("反转后:"+list);
}
}
//结果-----------------------------------------------------
张三:[30, 36, 54, 48, 34, 47, 42, 45, 9, 1, 20, 5, 53, 2, 21, 33, 16]
李四:[46, 29, 32, 27, 31, 14, 51, 25, 11, 26, 22, 44, 17, 23, 8, 49, 52]
王五:[35, 7, 19, 3, 15, 40, 39, 10, 24, 37, 50, 6, 12, 4, 41, 13, 38]
底牌为:[43, 28, 18]
[1, 2, 3, 4]
反转后:[4, 3, 2, 1]