2022-3-24作业

Cloud_vv 2022-03-24 21:28:49

1、有如下map:
Map<String,String> map = new HashMap<>();
map.put(“aaa”,“111”);
map.put(“bbb”,“111”);
map.put(“ccc”,“111”);
map.put(“ddd”,“222”);

使用3种方式,遍历并打印

import java.util.*;

public class Java01 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("aaa","111");
        map.put("bbb","111");
        map.put("ccc","111");
        map.put("ddd","111");
        //遍历Map  1.通过遍历所有的key 遍历map
        Set<String> keys = map.keySet();
        Iterator<String> itkey = keys.iterator();
        while (itkey.hasNext()){
            String key=itkey.next();
            String value = map.get(key);
            System.out.println(key+":"+value);
        }
        //遍历Map  2.通过遍历所有的value 遍历map
        Collection<String> values = map.values();
        Iterator<String> valueit = values.iterator();
        while (valueit.hasNext()){
            String value = valueit.next();
            System.out.println(value);
        }
        //遍历Map  3.通过遍历所有的entry 遍历map
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> entryIt = entries.iterator();
        while (entryIt.hasNext()){
            Map.Entry<String, String> entry = entryIt.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+": "+value);
        }
  }
}

2、定义集合List, 依次将1,2,3,4,5,6,7,8,9 添加到集合中
使用Collections中的方法:
打乱顺序
求最大值
求最小值
升序排列
交换位置
将该集合所有元素拷贝到另一个集合中并打印

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Java02 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        System.out.println(list);
        Collections.shuffle(list);//随机排序
        System.out.println(list);
        int max=0;
        max=Collections.max(list);//求最大值
        System.out.println(max);
        int min=0;
        min=Collections.min(list);//求最小值
        System.out.println(min);
        Collections.sort(list);//升序排列
        System.out.println(list);
        Collections.swap(list,0,1);//交换位置
        System.out.println(list);
        List<Integer> newlist = new ArrayList<>();
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        newlist.add(100);
        Collections.copy(newlist,list);
        System.out.println(newlist);
    }
}

3、编写代码,模拟如下异常:
ArithmeticException类 - 算术异常
ArrayIndexOutOfBoundsException类 - 数组下标越界异常
NullPointerException - 空指针异常
ClassCastException - 类型转换异常
NumberFormatException - 数字格式异常
OutOfMemoryError - 内存溢出错误


public class Java03 {
    public static void main(String[] args) {
//ArithmeticException类 - 算术异常
        int num = 10;
//        int a=num/0;

////        ArrayIndexOutOfBoundsException类 - 数组下标越界异常
        int[] arr = new int[2];
//        arr[4]=0;

//        NullPointerException - 空指针异常
        arr = null;
//        int length=arr.length;

//        ClassCastException - 类型转换异常
        Z z = new X();
//        V v=(V) z;

//        NumberFormatException - 数字格式异常
//        int abc = Integer.parseInt("abc");
        System.out.println();
//
//        OutOfMemoryError - 内存溢出错误
        String str="a";
//        for (int i = 0; i <100 ; i++) {
//            str=str+str;
//        }
        System.out.println(str);
    }
}

class Z {
}

class X extends Z {
}

class V extends X {
}

4、分析以下需求,并用代码实现:
(1)统计每个单词出现的次数
(2)有如下字符串
"If you want to change your fate I think you must come to the school to learn java"(用空格间隔)
(3)打印格式:
to=3
think=1
you=2


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

public class Java04 {
    public static void main(String[] args) {
        String str = "If you want to change your fate I think you must come to the school to learn java";
        //将字符串以空格分隔,分别装入String数组
        String []sts=str.split(" ");
        TreeMap<String,Integer>map=new TreeMap<>();
        //遍历数组,每次将元素装入map,相同则在原有数目加一,否则设置初值一
        for(String string:sts){
            map.put(string, map.containsKey(string)?map.get(string)+1:1);
        }
        //遍历TreeMap
        for(Entry<String,Integer> entry:map.entrySet()){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
    }

    }


...全文
131 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

7,481

社区成员

发帖
与我相关
我的任务
社区描述
新人大本营,在这个社区你可以见证自己的成长。
其他 其他
社区管理员
  • community_284
  • CSDN官方博客
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

进入本社区请遵循以下规则:

  • 禁止在社区里发广告
  • 禁止在社区讨论违法的话题

试试用AI创作助手写篇文章吧