zoukankan      html  css  js  c++  java
  • Java学习第十八天

    1:Map(掌握)
    (1)将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。 
    (2)Map和Collection的区别?
    A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
    B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍
    (3)Map接口功能概述(自己补齐)
    A:添加功能
    B:删除功能
    C:判断功能
    D:获取功能
    E:长度功能
    (4)Map集合的遍历
    A:键找值
    a:获取所有键的集合
    b:遍历键的集合,得到每一个键
    c:根据键到集合中去找值
    
    B:键值对对象找键和值
    a:获取所有的键值对对象的集合
    b:遍历键值对对象的集合,获取每一个键值对对象
    c:根据键值对对象去获取键和值
    
    代码体现:
    Map<String,String> hm = new HashMap<String,String>();
    
    hm.put("it002","hello");
    hm.put("it003","world");
    hm.put("it001","java");
    
    //方式1 键找值
    Set<String> set = hm.keySet();
    for(String key : set) {
    String value = hm.get(key);
    System.out.println(key+"---"+value);
    }
    
    //方式2 键值对对象找键和值
    Set<Map.Entry<String,String>> set2 = hm.entrySet();
    for(Map.Entry<String,String> me : set2) {
    String key = me.getKey();
    String value = me.getValue();
    System.out.println(key+"---"+value);
    }
    (5)HashMap集合的练习
    A:HashMap<String,String>
    B:HashMap<Integer,String>
    C:HashMap<String,Student>
    D:HashMap<Student,String>
    (6)TreeMap集合的练习    
    A:TreeMap<String,String>
    B:TreeMap<Student,String>
    (7)案例
    A:统计一个字符串中每个字符出现的次数
    B:集合的嵌套遍历
    a:HashMap嵌套HashMap
    b:HashMap嵌套ArrayList
    c:ArrayList嵌套HashMap
    d:多层嵌套
    
    2:Collections(理解)    
    (1)是针对集合进行操作的工具类
    (2)面试题:Collection和Collections的区别
    A:Collection 是单列集合的顶层接口,有两个子接口List和Set
    B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等
    (3)常见的几个小方法:
    A:public static <T> void sort(List<T> list)
    B:public static <T> int binarySearch(List<?> list,T key)
    C:public static <T> T max(Collection<?> coll)
    D:public static void reverse(List<?> list)
    E:public static void shuffle(List<?> list)
    (4)案例
    A:ArrayList集合存储自定义对象的排序
    B:模拟斗地主洗牌和发牌
    C:模拟斗地主洗牌和发牌并对牌进行排序
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.TreeSet;
    
    public class PokerDemo {
        public static void main(String[] args) {
            HashMap<Integer,String> hm = new HashMap<Integer,String>();
            ArrayList<Integer> array = new ArrayList<Integer>();
            String[] colors = {"♠","♥","♣","♦"};
            String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
            
            int index = 0;
            for(String number:numbers){
                for(String color:colors){
                    String poker = color.concat(number);
                    hm.put(index, poker);
                    array.add(index);
                    index++;
                }
            }
            hm.put(index, "小王");
            array.add(index);
            index++;
            hm.put(index, "大王");
            array.add(index);
            
            Collections.shuffle(array);
            
            TreeSet<Integer> person1 = new TreeSet<Integer>();
            TreeSet<Integer> person2 = new TreeSet<Integer>();
            TreeSet<Integer> person3 = new TreeSet<Integer>();
            TreeSet<Integer> endpoker = new TreeSet<Integer>();
            
            for(int i=0;i<array.size();i++){
                if(i>=array.size()-3){
                    endpoker.add(array.get(i));
                }else if(i%3==0){
                    person1.add(array.get(i));
                }else if(i%3==1){
                    person2.add(array.get(i));
                }else{
                    person3.add(array.get(i));
                }
            }
            
            lookPoker("第一个人",person1,hm);
            lookPoker("第二个人",person2,hm);
            lookPoker("第三个人",person3,hm);
            lookPoker("底牌",endpoker,hm);
        }
        
        //看牌的功能
        public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
            System.out.println(name+"的牌是:");
            for(Integer key:ts){
                System.out.print(hm.get(key)+" ");
            }
            System.out.println();
        }
    }
  • 相关阅读:
    BZOJ 1899: [Zjoi2004]Lunch 午餐
    BZOJ3670: [Noi2014]动物园
    BZOJ3712: [PA2014]Fiolki
    BZOJ1057: [ZJOI2007]棋盘制作
    BZOJ4326: NOIP2015 运输计划
    BZOJ4721: [Noip2016]蚯蚓
    BZOJ1131: [POI2008]Sta
    BZOJ1856: [Scoi2010]字符串
    BZOJ4003: [JLOI2015]城池攻占
    [AH2017/HNOI2017]单旋
  • 原文地址:https://www.cnblogs.com/zhuzhutest/p/6136596.html
Copyright © 2011-2022 走看看