java.util.Collection接口
集合是一种容器,能够存储多种数据
数组长度固定,集合长度可变,
数组能够存储基本类型,也能够存对象,集合只能存储的对象,类型能够不一致java
学习顶层
使用底层数组
定义的是全部单列集合中共性的方法,全部单列集合均可以使用共性的方法
没有带索引的方法框架
1.有序的集合(存储和取出元素顺序相同)
2.容许存储重复元素
3.有索引,能够使用普通的for循环遍历学习
1.不容许存储重复元素
2.没有索引,不能用普通的for循环遍历spa
无序集合,存储和取出的顺序有可能不一致code
无序集合,存储和取出的顺序有可能不一致对象
有序集合blog
boolean add(E e)索引
void clear()接口
boolean remove(E e)
boolean contains(E e)
当前集合是否包含所给定的对象
boolean isEmpty()
int size()
Object[] toArray()
斗地主案例
无序版本
public class StandardCard { public static void main(String[] args) { //1.准备牌,定义一个ArrayList的集合,泛型用String ArrayList<String> poker = new ArrayList<>(); //定义两个数组,一个存花色,一个存序号 String[] colors = {"♠", "♣", "♦", "♥"}; String[] num = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; //存入大王小王 poker.add("大王"); poker.add("小王"); //循环嵌套遍历两个数组,组装52张牌 for (String number: num) { for (String color : colors) { poker.add(color + number); } } System.out.println(poker); System.out.println("=========="); //打乱 Collections.shuffle(poker); System.out.println(poker); System.out.println("==========="); //发牌,四个集合:三个玩家,一个底牌 ArrayList<String> player1 = new ArrayList<>(); ArrayList<String> player2 = new ArrayList<>(); ArrayList<String> player3 = new ArrayList<>(); ArrayList<String> leftCard = new ArrayList<>(); /* 遍历poker集合,使用集合的索引%3给3个玩家轮流发牌 剩余3张牌给底牌 先判断底牌 (i >= 51) */ for (int i = 0; i < poker.size(); i++) { String s = poker.get(i); if (i >= 51) { leftCard.add(s); }else if (i % 3 == 0) { player1.add(s); }else if (i % 3 == 1) { player2.add(s); }else if (i % 3 == 2) { player3.add(s); } } System.out.println(player1); System.out.println(player2); System.out.println(player3); System.out.println(leftCard); } }
有序
public class Demo02DiZhu { public static void main(String[] args) { List<String> colors = List.of("♠", "♥", "♦", "♣"); List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"); //定义三个玩家、底牌 ArrayList<Integer> player1 = new ArrayList<>(); ArrayList<Integer> player2 = new ArrayList<>(); ArrayList<Integer> player3 = new ArrayList<>(); ArrayList<Integer> leftCard = new ArrayList<>(); //定义Map和List的索引 //Map装牌(花色和数字)以及对应的索引号,List装一样的索引号, // 至关于遍历List的值,代入Map的key,求value HashMap<Integer, String> mapCard = new HashMap<>(); ArrayList<Integer> numCard = new ArrayList<>(); int index = 0; mapCard.put(index, "大王"); numCard.add(index); index++; mapCard.put(index, "小王"); numCard.add(index); index++; for(String shuzi : numbers) { for(String huase : colors) { mapCard.put(index, huase + shuzi); numCard.add(index); index++; } } //将牌打乱 Collections.shuffle(numCard); for (int m = 0; m < numCard.size(); m++) { Integer n = numCard.get(m); if (m >= 51) { leftCard.add(n); }else if (m % 3 == 0) { player1.add(n); }else if (m % 3 == 1) { player2.add(n); }else if (m % 3 == 2) { player3.add(n); } } Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); lookPoker("player1", mapCard, player1); lookPoker("player2", mapCard, player2); lookPoker("player3", mapCard, player3); lookPoker("leftcard", mapCard, leftCard); } public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) { System.out.println(name + ": "); for (Integer key : list) { String value = poker.get(key); System.out.println(value + " "); } System.out.println(); } }