Collection集合:
所有单例集合的最顶层接口,里面定义了所有单例集合的共性的方法
集合是java中提供的一种容器,可以用来存储多个数据
集合和数组都是容器
数组的长度是固定的,集合的长度是可变的
数组中存储的是同一类型的数据,可以存储基本数据类型的值,
集合存储的都是对象,,热切对象的类型可以不一致,,开发中当对象多时,使用集合进行存储
集合框架:
Collection接口(List接口和Set接口共性抽取)
定义的是所有单例集合中共性的方法
所有的单例集合都可以使用共性的方法
没有索引的方法
Vector集合 ArrayList集合 LinkedList集合 TreeSet集合 HashSet集合 LinkedHashSet集合
学习集合的目标:
会使用集合存储数据,会遍历集合,把数据取出来,掌握集合的特性
List接口: 有序的集合(存储和取出元素的顺序相同)
允许存储重复的元素
有索引,可以使用普通的for循环遍历
继承: 子类共性抽取形成父类接口
Vector集合 ArrayList集合 LinkedList集合
Set接口: 不允许存储重复元素
没有索引,不能使用普通的for循环遍历
TreeSet集合 HashSet集合() 无序的集合(存入和取出的顺序有可以能不一致)
LinkedHashSet集合(有序的集合)
boolean add(E e); 向集合中添加元素
boolean remove(E e); 删除集合中的某个元素
void clear(); 清空集合中的所有元素
boolean contains(E e); 判断集合中是否有某个元素
boolean isEmpty(); 判断集合是否为空
int size(); 获取集合的长度
Object[] toArray(); 将集合组成一个数组
============================
public class CollectionCls {
public static void main(String[] args) {
//创建集合对象 使用多态,接口指向实现类.
Collection<String> coll = new ArrayList<>();
//返回值是一个布尔值,一般都返回true,所以不用接收
boolean b1 = coll.add("hello");
coll.add("world");
coll.add("heima");
coll.add("java");
System.out.println(coll);
//删除
boolean result = coll.remove("hello");
System.out.println(result);
System.out.println(coll);
//清空集合内容,集合还存在,不清空集合.
// coll.clear();
// System.out.println(coll);
//包含
boolean res = coll.contains("heima");
System.out.println(res);
//判断是否为空
boolean empty = coll.isEmpty();
System.out.println(empty);
//长度
int size = coll.size();
System.out.println(size);//3
//将集合转换为数组
Object[] array = coll.toArray();
//遍历数组
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
/*
* 斗地主案例的分析:
* 第一一个数组/集合存储四种花色
* 定义一个数组/集合存储13个序号
* 循环遍历这数组/集合 组装52张牌
*
* 洗牌:
* 使用集合工具类Collection的方法
* static void shuffle(List<?> list) 使用指定的随机要对指定列表进行置换
* 会随机打乱集合中元素的顺序
* 发牌:
* 要求一个人17张牌 ,集合的索引/3
* 定义四个集合,存储玩家的牌和底牌
* 索引%3, 0%3=0,1%3=1, 2%3=2, 3%3=0
* 索引>=51,底牌发牌
* 看牌:直接打印集合.
* 遍历存储底牌和集合
*
*/
public class TestCls {
public static void main(String[] args) {
ArrayList<String> poker = new ArrayList<>();
//定义两个数组,一个存储字符串的花色,另一个存储字符串的序号
String[] colors = {"黑桃","红桃","梅花","方片"};
String[] nums = {"2 "," A","K "," Q"," J"," 10"," 9"," 8"," 7",
"6 "," 5"," 4"," 3"};
//先把大王和小王添加到poker集合中
poker.add("大王");
poker.add("小王");
//循环嵌套遍历两个数组,组装52张牌
for(String num : nums){
for(String color : colors){
// System.out.println(color+num);
//把组装好的牌存储到poker集合中
poker.add(color+num);
}
}
// System.out.println(poker);
/*
* 洗牌:
* 使用集合的工具类Collection中的方法
* static void shuffle(List<?> List)
* 使用默认的随机源对指定的列表进行置换
*
*/
//这次输出发现poker牌的顺序就变了,而且每次都不一样
Collections.shuffle(poker);
System.out.println(poker);
/*
* 发牌:
* 定义四个集合,存储玩家和底牌
*
*/
ArrayList<String> play01 = new ArrayList<>();
ArrayList<String> play02 = new ArrayList<>();
ArrayList<String> play03 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
/*
* 遍历poker集合获取每一张牌
* 使用poker的索引%3给三个玩家轮流发牌
* 剩余三张给底牌
* 先判断索引i>=51,否则就没有了
* 使用for循环换,增强for里面没有索引
*
*/
for(int i = 0; i<poker.size(); i++){
//获取每一张牌
String p = poker.get(i);
//轮流发牌
if(i>=51){
dipai.add(p);
}else if(i%3==0){
//给玩家1发牌
play01.add(p);
}else if(i%3==1){
//给玩家2发牌
play02.add(p);
}else if(i%3==2){
//给玩家3发牌
play03.add(p);
}
}
//看牌
System.out.println("刘德华"+play01);
System.out.println("周润发"+play02);
System.out.println("周星驰"+play03);
System.out.println("底牌"+dipai);
}
}