package com.qiku.day19;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Zuo02 {
public static void main(String[] args) {
/**
* 2、定义集合List, 依次将1,2,3,4,5,6,7,8,9 添加到集合中
* 使用Collections中的方法:
* 打乱顺序 求最大值 求最小值 升序排列 交换位置
* 将该集合所有元素拷贝到另一个集合中并打印
*/
List<Integer> list = new ArrayList<>();
for (int i = 1; i < 10; i++) {
list.add(i);
}
Collections.shuffle(list);
System.out.println("打乱顺序:"+list);
Integer max = Collections.max(list);
System.out.println("最大值:"+max);
Integer min = Collections.min(list);
System.out.println("最小值:"+min);
Collections.sort(list);
System.out.println("升序排列:"+list);
Collections.swap(list,3,4);
System.out.println("交换位置:"+list);
List<Integer> str = new ArrayList<>();
for (int i = 0; i < 9; i++) {
str.add(1);
}
Collections.copy(str,list);
System.out.println("元素拷贝:"+str);
}
}
package com.qiku.day19;
import java.util.Arrays;
import java.util.Scanner;
public class Zuo03 {
public static void main(String[] args) {
/**
* 3、编写代码,模拟如下异常:
*/
//ArithmeticException类 - 算术异常
int a = 1;
System.out.println(a / 0);
//ArrayIndexOutOfBoundsException类 - 数组下标越界异常
int[] arr = new int[2];
arr[2] = 1;
//NullPointerException - 空指针异常
arr = null;
System.out.println(arr.length);
//ClassCastException - 类型转换异常
A b = new B();
C c = (C) b;
System.out.println(c);
//NumberFormatException - 数字格式异常
int d = Integer.parseInt("abc");
System.out.println(d);
//OutOfMemoryError - 内存溢出错误
String e = "a";
for (int i = 0; i < 100000; i++) {
e += e;
}
System.out.println(e);
}
}
class A {
}
class B extends A {
}
class C extends B {
}
package com.qiku.day19;
import java.util.Arrays;
public class Zuo04 {
public static void main(String[] args) {
/**
* 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[] strs = str.split(" ");
System.out.println(Arrays.toString(strs));
for (int i = 0; i < strs.length; i++) {
String a = strs[i];
int b = 0;
int c = str.indexOf(a);
while (c!=-1){
b++;
c=str.indexOf(a,c+a.length());
}
System.out.println(a+"="+b);
}
}
}