Collection:单列集合类的根接⼝,⽤于存储⼀系列符合某种规则的元素,它有两个重要的⼦接 ⼝,分别是 java.util.List 和 java.util.Set 。其中, List 的特点是元素有序、元素可重 复。 Set 的特点是元素⽆序,⽽且不可重复。 List 接⼝的主要实现类有 java.util.ArrayList 和 java.util.LinkedList , Set 接⼝的主要实现类有 java.util.HashSet 和 java.util.TreeSet 。
方法演示 package gather; /* Collection是所有单列集合的⽗接⼝,因此在Collection中定义了单列集合(List和Set)通⽤的⼀些⽅法, 这些⽅法可⽤于操作所有的单列集合。⽅法如下 public boolean add(E e) : 把给定的对象添加到当前集合中 。 public void clear() :清空集合中所有的元素。 public boolean remove(E e) : 把给定的对象在当前集合中删除。 public boolean contains(E e) : 判断当前集合中是否包含给定的对象。 public boolean isEmpty() : 判断当前集合是否为空。 public int size() : 返回集合中元素的个数。 public Object[] toArray() : 把集合中的元素,存储到数组中。 */ import java.util.ArrayList; import java.util.Collection; public class Colletion01 { public static void main(String[] args) { //使用集合 //多态 //泛型规定存储数据 Collection<String> collection = new ArrayList<String>(); //添加 collection.add("皮辉辉"); collection.add("龙儿子"); collection.add("秀秀秀"); System.out.println(collection); //boolean contains(E e) 判断0是否在集合中存在 System.out.println("判断 皮辉辉 是否在集合中 "+collection.contains("皮辉辉")); //boolean remove/消除 删除在集合中的o元素 System.out.println("删除龙儿子"+collection.remove("龙儿子")); System.out.println(collection); //size() 集合中有几个元素 System.out.println("集合中有"+collection.size()+"个元素" ); //object[] toArray()转换成一个object数组 Object[] objects = collection.toArray(); //遍历数组 for (int i = 0; i < objects.length; i++) { System.out.println(objects[i]); } //void clear() 清空集合 collection.clear(); System.out.println("集合内容为空"+collection); //boolean isEMpty() 判断是否为空 System.out.println(collection.isEmpty()); } }
集合综合案例
package com.wenj; import com.sun.javafx.image.BytePixelSetter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.FormatFlagsConversionMismatchException; /* 集合综合案例 斗地主发牌 */ public class Brand { public static void main(String[] args) { /* 1:准备发牌操作 */ //创建盘盒,来存储牌面的 ArrayList<String> pokerBox = new ArrayList<String>(); //创建花色集合 ArrayList<String> colcos = new ArrayList<String>(); //创建数字集合 ArrayList<String> numbers = new ArrayList<String>(); //分别给花色 以及 数字集合添加元素 colcos.add("♥"); colcos.add("♦"); colcos.add("♠"); colcos.add("♣"); for (int i = 2; i <= 10; i++) { numbers.add(i + " "); } numbers.add("J"); numbers.add("Q"); numbers.add("K"); numbers.add("A"); //创造牌,接拼牌操作 //拿出每一个花色,然后跟每一个数字 进行结合 存储到牌盒中 for (String colcor : colcos) { //color 每一个花色 //遍历数组集合 for (String number : numbers) { //结合 String card = colcor + number; //card 卡片 //存储到牌盒中 pokerBox.add(card); } } //大小王 pokerBox.add("小王"); pokerBox.add("大王"); // System.out.println(pokerBox); //洗牌 将牌盒中的索引打乱 //Ccollections类 工具类 都是静态方法 //shuffer方法 /* * static void shuffle(List<?> list) * 使⽤默认随机源对指定列表进⾏置换 */ //洗牌 Collections.shuffle(pokerBox); //System.out.println(pokerBox); //发牌 //创建 三个玩家集合 创建一个底牌集合 ArrayList<String> Player1 = new ArrayList<String>(); ArrayList<String> Player2 = new ArrayList<String>(); ArrayList<String> Player3 = new ArrayList<String>(); ArrayList<String> dipa = new ArrayList<String>(); //遍历 牌盒 必须知道索引 for (int i = 0; i < pokerBox.size(); i++) { //获取牌面 String card = pokerBox.get(i); //留出三张牌 存到 底牌集合中 if (i >= 51) { //存到底牌集合中 dipa.add(card); } else { //玩家1 %3 == 0 if (i % 3 == 0) { Player1.add(card); } else if (i % 3 == 1) { //玩家二 Player2.add(card); } else { //玩家三 Player3.add(card); } } } //牌 System.out.println("1号:"+Player1); System.out.println("2号:"+Player2); System.out.println("3号:"+Player3); System.out.println("底牌:"+dipa); } }