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
Map<String,String> map = new HashMap<>();
map.put("aaa","111");
map.put("bbb","111");
map.put("ccc","111");
map.put("ddd","111");
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);
}
Collection<String> values = map.values();
Iterator<String> valueIt = values.iterator();
while (valueIt.hasNext()){
String value = valueIt.next();
System.out.println(value);
}
Set<Map.Entry<String, String>> entries = map.entrySet();
Iterator<Map.Entry<String, String>> entyrIt = entries.iterator();
while (entyrIt.hasNext()){
Map.Entry<String,String> entry = entyrIt.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(entry);
}
种方式,遍历并打印
2、定义集合List, 依次将1,2,3,4,5,6,7,8,9 添加到集合中 使用Collections中的方法: 打乱顺序 求最大值 求最小值 升序排列 交换位置 将该集合所有元素拷贝到另一个集合中并打印
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 9; i++) {
list.add(i+1);
}
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
int max = Collections.max(list);
System.out.println(max);
int min = Collections.min(list);
System.out.println(min);
Collections.sort(list);
System.out.println(list);
Collections.swap(list,0,1);
List<Integer> newList = new ArrayList<>();
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
newList.add(1);
Collections.copy(newList,list);
System.out.println(newList);
3、编写代码,模拟如下异常: ArithmeticException类 - 算术异常 ArrayIndexOutOfBoundsException类 - 数组下标越界异常 NullPointerException - 空指针异常 ClassCastException - 类型转换异常 NumberFormatException - 数字格式异常 OutOfMemoryError - 内存溢出错误
//ArithmeticException类 - 算术异常
int i1 = num/0;
System.out.println(i1);
//ArrayIndexOutOfBoundsException类 - 数组下标越界异常
int[] arrs = new int[2];
arrs[3]=12;
System.out.println(arrs[3]);
//NullPointerException - 空指针异常
int i = null;
//ClassCastException - 类型转换异常
class A{}
class B extends A{}
class C extends B{}
A a = new B();
C c = (C)a;
//NumberFormatException - 数字格式异常
int a = Integer.parseInt("abc");
System.out.println(a);
//OutOfMemoryError - 内存溢出错误
String str = "a";
for (int i = 0; i < 77777777; i++) {
str = str + str;
}
System.out.println(str);
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
String str ="If you want to change your fate I think you must come to the school to learn java";
String[] arr = str.split("\\s+");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
HashMap<String,Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (!map.containsKey(arr[i])){
map.put(arr[i],1);
}else if (map.containsKey(arr[i])){
int i1 = map.get(arr[i]);
map.put(arr[i],i1+1 );
}
}
System.out.println(map);