单列集合——Collection集合

Collection集合是最上层的接口,是所有集合的父接口


概述:

Collection集合是最上层的接口,是所有集合的父接口,collection接口下面有list和set接口,还有一些单例集合的共用方法,没有带索引的方法

一、collection集合图解

 

 

二、collection共性方法:

java.util.Collection接口
    所有单列集合的最顶层的接口,里边定义了所有单列集合共性的方法
    任意的单列集合都可以使用Collection接口中的方法
 
 
共性的方法:
  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(): 把集合中的元素,存储到数组中。

 

2.扑克牌实例:

代码如下(示例):

public class doudizhu {
    public static void main(String[] args) {
        //1:准备牌
        //创建俩个集合
        //创建一个map结合储存牌的索引和装好的牌
        HashMap<Integer,String> poker = new HashMap<>();
        //创建一个list集合存储牌的索引
        ArrayList<Integer> pokerindex = new ArrayList<>();
        //定义俩个集合,储存花色和牌的序号
        List<String> colos = List.of("♥", "♦", "♠", "♣");
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        //存储大王小王
        //定义一个索引
        int index = 0;
        poker.put(index,"大王");
        pokerindex.add(index);
        index++;
        poker.put(index,"小王");
        pokerindex.add(index);
        index++;
        //循环遍历俩个集合。组装52张牌,存储到集合中
        for (String number : numbers) {
            for (String colo : colos) {
                poker.put(index,colo+number);
                pokerindex.add(index);
                index++;
            }
        }
        System.out.println(poker );
        //2:洗牌
        //使用collections中的shuffle(list)方法
        Collections.shuffle(pokerindex);
        System.out.println(pokerindex);
//        发牌
        //定义四个集合,储存玩家牌的索引,和底牌的索引
        ArrayList<Integer>  palyer01 = new ArrayList<>();
        ArrayList<Integer>  palyer02 = new ArrayList<>();
        ArrayList<Integer>  palyer03 = new ArrayList<>();
        ArrayList<Integer>  dipai    = new ArrayList<>();
        //遍历储存牌索引的list集合 ,获取每一个牌的索引
        for (int i = 0; i < pokerindex.size(); i++) {
            Integer in = pokerindex.get(i);
            //先判断底牌
            if (i>=51){
                //给底牌发牌
                dipai.add(in);
            } else if (i%3==0){
                palyer01.add(in);
            }else if (i%3==1){
                palyer02.add(in);
            }else if (i%3==2){
                palyer03.add(in);
            }
        }
        //4:排序使用collections中的sort(list)  默认是升序
        Collections.sort(palyer01);
        Collections.sort(palyer01);
        Collections.sort(palyer01);
        Collections.sort(dipai);
        //5:看牌定义一个看牌的方法  提高代码的复用性
//        参数:String name:玩家名称;
//        HashMap<Integer,String> poker:存储牌的集合
//        Arrlist<Integer>  list :存储玩家底牌和list集合
//                查表法:
//        遍历玩家或者底牌的索引,获取底牌的索引使用牌的索引去map集合中找到对应的牌

    }
    public static void lookpoker(String name,HashMap<Integer ,String> poker,ArrayList<Integer> list){

    }
}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了collection的使用,而collection提供了大量的共性方法,Collection 接口用于表示任何对象或元素组。常规方式处理一组元素时,就使用这一接口。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值