美文网首页
集合的总结练习

集合的总结练习

作者: 富尔察纯生 | 来源:发表于2018-01-11 11:18 被阅读0次

ArrayList

package array;

import java.util.ArrayList;
import java.util.List;

public class ListReview {
    public static void main(String[] args) {

        List<String> stringList = new ArrayList<>();
        stringList.add("zs");
        stringList.add("ls");
        stringList.remove("ls");
        if (stringList.contains("zs")) {
            System.out.println("yes");
        } else {
            System.out.println("no");

        }
        stringList.set(0, "ww");
        stringList.add("ww");
        for (String string : stringList) {
            System.out.println(string);
        }


    }
}

MapReview

public class MapReview {
    public static void main(String[] args) {
        Map<String ,String>words =new HashMap<>();
        words.put("dog","狗 ");
        words.put("cat","猫");




      String str =  words.remove("dog" );
        System.out.println(str+"666");

        words.put("cat","小猫");

        for (String key:words.keySet())
        {
            System.out.println(key+"----"+words.get(key));
        }

        if (words.containsKey("cat"))
        {
            System.out.println("包含");
        }
        else {
            System.out.println("不包含");
        }

        for (Map.Entry entry:words.entrySet())
        {
            System.out.println(entry.getKey()+"----"+entry.getValue());
        }





    }
}

<pre>

练习

1.第一题 (Map)利用Map,完成下面的功能:

从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。

历届世界杯冠军

届数 举办年份 举办地点 冠军

第一届 1930年 乌拉圭 乌拉圭

第二届 1934年 意大利 意大利

第三届 1938年 法国 意大利

第四届 1950年 巴西 乌拉圭

第五届 1954年 瑞士 西德

第六届 1958年 瑞典 巴西

第七届 1962年 智利 巴西

第八届 1966年 英格兰 英格兰

第九届 1970年 墨西哥 巴西

第十届 1974年 前西德 西德

第十一届 1978年 阿根廷 阿根廷

第十二届 1982年 西班牙 意大利

第十三届 1986年 墨西哥 阿根廷

第十四届 1990年 意大利 西德

第十五届 1994年 美国 巴西

第十六届 1998年 法国 法国

第十七届 2002年 韩日 巴西

第十八届 2006年 德国 意大利

第十九届 2010年 南非 西班牙

第二十届 2014年 巴西 德国

(Map)在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯

image.png
image.png
package array;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class WordCcups {
    public static void main(String[] args) {

        Map<Integer, String> year2country = new HashMap<>();
        year2country.put(1930, "乌拉圭");
        year2country.put(1934, "意大利");
        year2country.put(1938, "意大利");
        year2country.put(1950, "乌拉圭");
        year2country.put(1954, "西德");
        year2country.put(1958, "巴西");
        year2country.put(1962, "巴西");
        year2country.put(1966, "英格兰");
        year2country.put(1970, "巴西");
        year2country.put(1974, "西德");
        year2country.put(1978, "阿根廷");
        year2country.put(1982, "意大利");
        year2country.put(1986, "阿根廷");
        year2country.put(1990, "西德");
        year2country.put(1994, "巴西");
        year2country.put(1998, "法国");
        year2country.put(2002, "巴西");
        year2country.put(2006, "意大利");
        year2country.put(2010, "西班牙");
        year2country.put(2014, "德国");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入年份:");
        int year = scanner.nextInt();
        if (!year2country.containsKey(year)) {
            System.out.println("该年没有世界杯");
        } else {
            System.out.println("概念的冠军是" + year2country.get(year));
        }
        System.out.println("请输入国家名:");
        String countryname = scanner.next();


        boolean bFind = false;

//        for (Integer key:year2country.keySet())
//        {
//            String country=year2country.get(key);
        for (Map.Entry entry : year2country.entrySet()) {


            if (entry.getValue().equals(countryname)) {
                System.out.println(entry.getKey());
                bFind = true;
            }

        }

        if (bFind == false) {
            System.out.println("该国家没有获得世界杯");
        }


    }
}

2.第二题 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

2008 北京奥运会男足参赛国家:

科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利

提示:分配一个,删除一个

image.png
public static void main(String[] args) {

        //map,保存最终分组结果
        //key保存的是第几组,value是该组对应的国家集合
        Map<Integer,List<String>> groupNum2Countrys = new HashMap<>();

        List<String> stringList = new ArrayList<>();

        String strCountrys = "科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利";
        String[] countrys = strCountrys.split(",");

        for(String str : countrys)
        {
            stringList.add(str);
        }

        //分组,分4组

        //分第1组
        //随机从集合中选出一个国家,放到第1组里;然后将这个选出的国家,从原来集合中干掉(删除)
        //重复以上步骤4次

        for(int j = 0; j < 4; j++)
        {
            List<String> lstGroup = new ArrayList<>();

            for(int i = 0; i < 4; i++)
            {
                Random random = new Random();
                int index = random.nextInt(stringList.size());
                String selectCountry = stringList.get(index);//从大集合中选出一个随机的国家
                //把这个国家加入到第一组

                lstGroup.add(selectCountry);
                //然后将这个选出的国家,从原来集合中干掉(删除)
                stringList.remove(selectCountry);
            }

            //以上已经给第一组分配完国家
            //将该组加入到map中
            groupNum2Countrys.put(j+1,lstGroup);
        }
        //输出分组情况
        //遍历map,输出key-value
        for(Integer integer : groupNum2Countrys.keySet())
        {
            System.out.println("第" + integer + "组" );
            List<String> lstCountrys = groupNum2Countrys.get(integer);
            //输出第一组的所有国家名字
           for(String str : lstCountrys)
           {
               System.out.print(str + "\t");
           }
            System.out.println();
        }



    }


相关文章

  • 集合的总结练习

    ArrayList MapReview 练习 1.第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个...

  • JS基础练习

    github作业地址: JavaScript基础练习 JavaScript集合练习 pre-pos 作业总结 1、...

  • Javascript 编程入门练习

    a.Javascript 基础练习-git项目地址b.Javascrpt 集合练习-git项目地址 总结及学到的知...

  • kotlin练习 ---- 集合练习

    kotlin练习 - 集合练习 Set集合 Set集合创建 Set集合的使用 List集合 List集合创建 Li...

  • 集合总结

    集合Collection总结 集合Map总结

  • 第三章_数据类型

    字符串练习 列表练习 字典练习 集合练习

  • java集合框架学习总结

    title: java集合框架学习总结 tags:集合框架 categories:总结 date: 2017-03...

  • Java集合(四)

    1. 简单练习HashSet集合-产生1到20,10个不重复的随机数 2. 集合小练习 3. TreeSet集合的...

  • JS-Basic

    本文主要结构 任务JavaScript基础练习JavaScript集合练习 JavaScript基础练习学到的主要...

  • 集合总结

    纯粹是个人学习总结,如有不对的地方请吐槽。 集合说明 集合关系图 ArrayList、LinkedList与Vec...

网友评论

      本文标题:集合的总结练习

      本文链接:https://www.haomeiwen.com/subject/ifpanxtx.html