【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 <Character,Integer>hm=new HashMap<Character,Integer>();
        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<Student, Integer> hm = new HashMap<Student,Integer>();
        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<Student> key = hm.keySet();
        for(Student s:key){
            System.out.println("键为:"+s+"\t值为:"+hm.get(s));
        }

        //方式二
        Set<Entry<Student, Integer>> 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<Person, String> tree = new TreeMap<Person,String>();
        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<Person> keySet = tree.keySet();
        for(Person p:keySet){
            System.out.println("键为:"+p+"\t值为:"+tree.get(p));
        }

        //方式二
        Set<Entry<Person, String>> 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);

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

        Set<HashMap<String, 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<Student> jc = new ArrayList<Student>();

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

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

        Set<ArrayList<Student>> keySet = arrayList.keySet();
        for(ArrayList<Student> 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<String> pokerBox = new ArrayList<String>();
        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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值