栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单列集合——Collection集合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单列集合——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 poker = new HashMap<>();
        //创建一个list集合存储牌的索引
        ArrayList pokerindex = new ArrayList<>();
        //定义俩个集合,储存花色和牌的序号
        List colos = List.of("♥", "♦", "♠", "♣");
        List 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  palyer01 = new ArrayList<>();
        ArrayList  palyer02 = new ArrayList<>();
        ArrayList  palyer03 = new ArrayList<>();
        ArrayList  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 poker:存储牌的集合
//        Arrlist  list :存储玩家底牌和list集合
//                查表法:
//        遍历玩家或者底牌的索引,获取底牌的索引使用牌的索引去map集合中找到对应的牌

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

    }
}

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891235.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号