【JavaSE】作业练习1118

1、请编写程序,统计一个字符串中每个字符出现的次数

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

public class Demo1 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一串字符串");
        String s=sc.nextLine();
        HashMap hm=new HashMap();
        char[] ch = s.toCharArray();
        for(char c:ch){
            Integer value = hm.get(c);
            if(value==null){
                hm.put(c,1);
            }else{
                value++;
                hm.put(c, value);
            }

        }
        System.out.println(hm);
    }
}

2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Demo2 {
    public static void main(String[] args) {
        HashMap hm = new HashMap();
        hm.put(new Student("张三",15), 1);
        hm.put(new Student("李四",20), 2);
        hm.put(new Student("王五",16), 3);
        hm.put(new Student("赵六",18), 4);
        System.out.println(hm);

        //方式一
        Set key = hm.keySet();
        for(Student s:key){
            System.out.println("键为:"+s+"\t值为:"+hm.get(s));
        }

        //方式二
        Set> entrySet = hm.entrySet();
        for(Entry e:entrySet){
            System.out.println("键为:"+e.getKey()+"值为:"+e.getValue());
        }
    }
}

3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历

import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

public class Demo3 {
    public static void main(String[] args) {
        TreeMap tree = new TreeMap();
        tree.put(new Person("张三",45),"北京市" );
        tree.put(new Person("李四",23),"上海市" );
        tree.put(new Person("王五",67),"北京市" );
        tree.put(new Person("赵六",36),"南京市" );
        System.out.println(tree);

        //方式一
        Set keySet = tree.keySet();
        for(Person p:keySet){
            System.out.println("键为:"+p+"\t值为:"+tree.get(p));
        }

        //方式二
        Set> entrySet = tree.entrySet();
        for(Entry e:entrySet){
            System.out.println("键为:"+e.getKey()+"\t值为:"+e.getValue());
        }
    }
}

4、请编写程序,完成集合嵌套,并遍历
  jc  基础班
      张三 20
      李四 22
  jy  就业班
      王五 21
      赵六 23
HashMap嵌套HashMap

import java.util.HashMap;
import java.util.Set;

public class Demo4 {
    public static void main(String[] args) {
        HashMap<String, Integer> jc = new HashMap<String,Integer>();
        jc.put("张三", 20);
        jc.put("李四", 22);

        HashMap<String, Integer> jy = new HashMap<String,Integer>();
        jy.put("王五", 21);
        jy.put("赵六", 23);

        HashMapString, Integer>, String> big = new HashMapString, Integer>,String>();
        big.put(jc, "基础班");
        big.put(jy,"就业班");

        SetString, Integer>> bigKey = big.keySet();
        for(HashMap<String, Integer> hm:bigKey){
            Set<String> key = hm.keySet();
            System.out.println(big.get(hm));
            for(String s:key){
                System.out.println("\t"+s+"\t"+hm.get(s));
            }
        }
    }
}

HashMap嵌套ArrayList

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class Demo5 {
    public static void main(String[] args) {
        ArrayList jc = new ArrayList();

        jc.add(new Student("张三", 20));
        jc.add(new Student("李四", 22));
        ArrayList jy = new ArrayList();
        jy.add(new Student("王五", 21));
        jy.add(new Student("赵六", 23));

        HashMap,String> arrayList = new HashMap,String> ();
        arrayList.put(jc,"基础班");
        arrayList.put(jy,"就业班");

        Set> keySet = arrayList.keySet();
        for(ArrayList arr:keySet){
            System.out.println(arrayList.get(arr));
            for(Student s:arr){
                System.out.println("\t"+s);
            }
        }
    }
}

5、生成扑克牌并模拟洗牌

import java.util.ArrayList;
import java.util.Collections;

public class Demo6 {
    public static void main(String[] args) {
        ArrayList pokerBox = new ArrayList();
        String[] colors = { "♥", "♠", "♦", "♣" };
        String[] nums = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
        for (String color : colors) {
            for (String num : nums) {

                pokerBox.add(color + num);
            }
        }

        //添加大王小王
        pokerBox.add("大王");
        pokerBox.add("小王");

        //洗牌
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
    }
}

你可能感兴趣的:(学习笔记)