A:Collections类概述: 针对集合操作 的工具类 B:Collections成员方法 public static void sort(List list): 排序,默认按照自然顺序 public static int binarySearch(List<?> list,T key): 二分查找 public static T max(Collection<?> coll): 获取最大值 public static void reverse(List<?> list): 反转 public static void shuffle(List<?> list): 随机置换 C:案例演示: Collections工具类的常见方法讲解
案例:
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; public class MyTest2 { public static void main(String[] args) { // Collection Set List // A: // Collections类概述: // 针对集合操作 的工具类 // B: // Collections成员方法 // public static <T > void sort (List < T > list):排序, 默认按照自然顺序 // public static <T > int binarySearch (List < ? > list, T key):二分查找 // public static <T > T max(Collection < ? > coll):获取最大值 // public static void reverse (List < ? > list):反转 // public static void shuffle (List < ? > list):随机置换 ArrayList<Integer> list = new ArrayList<>(); list.add(100); list.add(1002); list.add(1030); list.add(1050); list.add(100); //list.sort(); Collections.sort(list); //Collections.sort(list, new Comparator<Integer>() { // @Override // public int compare(Integer o1, Integer o2) { // return 0; // } //}); //System.out.println(list); int i = Collections.binarySearch(list, 1050); System.out.println(i); System.out.println(Collections.max(list)); System.out.println(Collections.min(list)); Collections.reverse(list);//反转集合中的元素 System.out.println(list); Collections.shuffle(list);//随机打乱集合中元素的顺序 System.out.println(list); } }//A: //案例演示: //模拟斗地主洗牌和发牌,牌没有排序 //得有一副牌 案例:
import java.util.ArrayList; import java.util.Collections; public class MyTest { public static void main(String[] args) { //A: //案例演示: //模拟斗地主洗牌和发牌,牌没有排序 //得有一副牌 ArrayList<String> pokerBox = new ArrayList<>(); //生成54张牌放到牌盒里面 String[] colors = {"♠", "♥", "♦", "♣"}; String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for (String color : colors) { for (String num : nums) { pokerBox.add(color.concat(num)); } } //手动添加大小王 pokerBox.add("★"); pokerBox.add("☆"); //洗牌 Collections.shuffle(pokerBox); Collections.shuffle(pokerBox); Collections.shuffle(pokerBox); //发牌 ArrayList<String> 高进 = new ArrayList<>(); ArrayList<String> 刀仔 = new ArrayList<>(); ArrayList<String> 星仔 = new ArrayList<>(); ArrayList<String> 底牌 = new ArrayList<>(); // 高进 = (ArrayList<String>) pokerBox.subList(0,17); // 高进 0 3 6 9 //刀仔 1 4 7 10 // 星仔 2 5 8 11 for (int i = 0; i < pokerBox.size(); i++) { if (i >= pokerBox.size() - 3) { 底牌.add(pokerBox.get(i)); } else if (i % 3 == 0) { 高进.add(pokerBox.get(i)); } else if (i % 3 == 1) { 刀仔.add(pokerBox.get(i)); } else { 星仔.add(pokerBox.get(i)); } } //看牌 lookPoker("高进",高进); lookPoker("刀仔", 刀仔); lookPoker("星仔", 星仔); lookPoker("底牌", 底牌); } private static void lookPoker(String name, ArrayList<String> list) { System.out.println(name); for (String s : list) { System.out.print(s+" "); } System.out.println(); } }结果: 高进 ♦K ♣6 ♠3 ♥8 ♥2 ♥3 ♣10 ♥K ♦6 ♣8 ♠7 ♣3 ♠K ♣7 ♦4 ♥9 ♦7 刀仔 ♦8 ♠J ★ ♥Q ♦Q ♥4 ☆ ♥5 ♠9 ♦5 ♣9 ♣Q ♠5 ♠4 ♥10 ♦A ♥J 星仔 ♠A ♦2 ♦9 ♣5 ♣J ♦3 ♦10 ♥6 ♠2 ♦J ♣2 ♥7 ♣4 ♣K ♠Q ♠10 ♠8 底牌 ♥A ♠6 ♣A