java 15
1、Collections
Collections:是java针对集合操作专门提供的一个工具类
静态方法
public static <T> void sort(List<T> list)
public static <T> int binarySearch(List<?> list,T key)
public static <T> T max(Collection<?> coll)
public static void reverse(List<?> list)
public static void shuffle(List<?> list)
运用示例
public class CollectionsDemo1 {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(34);
list1.add(7);
list1.add(12);
list1.add(734);
list1.add(38);
list1.add(29);
System.out.println(list1);
System.out.println("-------------------------------");
//public static <T> void sort(List<T> list)
Collections.sort(list1); // 底层是调用了Arrays工具类中的sort方法
System.out.println(list1);
//public static <T> int binarySearch(List<?> list,T key)
// 二分查找【前提是序列是有序的】
//[7, 12, 29, 34, 38, 734]
System.out.println(Collections.binarySearch(list1, 100));
//public static <T> T max(Collection<?> coll) 求集合中最大值
System.out.println(Collections.max(list1));
ArrayList<String> list2 = new ArrayList<>();
list2.add("hello");
list2.add("world");
list2.add("java");
list2.add("apple");
list2.add("hadoop");
System.out.println(Collections.max(list2));
//public static void reverse(List<?> list)
Collections.reverse(list1);
System.out.println(list1);
System.out.println("===========================");
//public static void shuffle(List<?> list) 随机打乱元素
Collections.shuffle(list1);
System.out.println(list1); // [34, 734, 38, 7, 12, 29] [34, 12, 7, 38, 29, 734]
}
}
我们在说Vector类的时候,说到即便是安全的,我们也不会用它。使用Collections工具类可以将任意一个不安全的集合类变成一个线程安全的集合类
Collections.synchronizedList()
public class CollectionsDemo2 {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
Vector<Integer> v1 = new Vector<>();
// List<Integer> list2 = Collections.synchronizedList(list1);
// list2.add(1);
// list2.add(1);
// list2.add(1);
// for (Integer i : list2) {
// System.out.println(i);
// }
}
}
2、递归
递归:方法定义时内部调用自身的现象
fun1().fun2() 这叫方法的链式调用
fun1(show1()) 这叫方法的嵌套调用
写递归需要考虑的东西
1、开始条件
2、出口
递归故事:
1、从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容: 1
从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容: 2
从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容: 3
从前有座山,山里有座庙,庙里有个老和尚,老和尚在给小和尚讲故事,讲的故事内容: 4
....
结束条件:山倒了,老和尚圆寂了。
2、来数加学习大数据,找工作,挣钱,娶老婆,生娃:
来数加学习大数据,找工作,挣钱,娶老婆,生娃:
来数加学习大数据,找工作,挣钱,娶老婆,生娃:
...
结束条件:挣不到钱,没有生娃...
需求:使用递归实现一个数的阶乘
public class DiGui {
public static void main(String[] args) {
int n = 4;
System.out.println(n+"的阶乘为"+JieChen(n));
}
public static int JieChen(int n){
if(n==1){
return 1;
}else{
return n*JieChen(--n);
}
}
}
3、异常处理
什么叫异常
异常:java程序编译或运行过程中出现的问题
Throwable:
Error: 表示非常严重的问题,自己无法解决的问题
Exception:
除了RuntimeException其它异常【编译时期异常】: 一般指的是异常尚未处理就编译了
RuntimeException【运行时期异常】: 运行过程中报错了,一般情况下是因为语法逻辑不正确导致
JVM遇到问题时,默认的处理方案是:停止程序,抛出错误。
异常的处理方案
1、try...catch...finally
try...catch使用注意事项
1、当try中出现异常时,JVM会对应创建一个异常类对象
2、自上而下会与catch中的异常进行匹配,若匹配上就相当于处理了,执行catch中的逻辑
3、若try中有多个异常,当第一个异常触发时,try中的其它后续代码都不会执行,JVM会对应创建一个异常类对象进行第二步匹配
4、可以直接写一个catch里面是所有异常的父类、
5、若存在多个catch匹配,需要将父类异常往后写
6、若使用jdk新特性的写法的话,异常类之间不能存在继承关系
public class ExceptionDemo2 {
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55};
// try {
// System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
// System.out.println(10/3); // new ArithmeticException()
// System.out.println("江川比李刚帅!");
// }catch (ArrayIndexOutOfBoundsException e){ // = new ArrayIndexOutOfBoundsException()
// System.out.println("出错啦!!");
// }catch (ArithmeticException e){
// System.out.println("除0错误!!");
// }
// try {
// System.out.println(arr[2]); // new ArrayIndexOutOfBoundsException()
// System.out.println(10/0); // new ArithmeticException()
// System.out.println("江川比李刚帅!");
// }catch (ArithmeticException e){
// System.out.println("除0错误!!");
// }catch (Exception e){ // = new ArrayIndexOutOfBoundsException()
// System.out.println("出错啦!!");
// }
try {
System.out.println(arr[8]); // new ArrayIndexOutOfBoundsException()
System.out.println(10 / 3); // new ArithmeticException()
System.out.println("江川比李刚帅!");
} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) { // = new ArrayIndexOutOfBoundsException()
System.out.println("出错啦!!");
}
System.out.println("李刚真帅!");
}
}
打印异常printStackTrace()
public class ExceptionDemo3 {
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55};
// System.out.println(arr[8]);
// System.out.println(3/0);
try {
// System.out.println(arr[8]);
System.out.println(3/0);// new ArrayIndexOutOfBoundsException()
} catch (Exception e) { // = new ArrayIndexOutOfBoundsException()
// System.out.println(e.getMessage());
// System.out.println(e.toString());
//void printStackTrace()
//将此throwable和其追溯打印到标准错误流。
e.printStackTrace();
}
System.out.println("李刚是世界首富!!");
}
}
finally
无论try中的代码是否出错,finally都会执行。
finally一般来说,用作释放资源的作用。
public class ExceptionDemo4 {
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55};
try {
System.out.println(3/2);// new ArrayIndexOutOfBoundsException()
} catch (Exception e) { // = new ArrayIndexOutOfBoundsException()
e.printStackTrace();
} finally {
System.out.println("数加666");
}
System.out.println("李刚是世界首富!!");
}
}
throw
在方法的定义上抛出异常类, throws本身是不会处理异常的,只是当时方法内部不用处理了,
但是当调用者调用有问题的方法时,需要调用者进行处理
throw: 抛出
在方法内部抛出异常对象,表示一个确定的异常
注意:
当调用者调用抛出运行时期异常的方法时,可以选择不去处理,也能运行。
当调用者调用抛出编译时期异常的方法是,必须要进行处理,否则编译不通过无法运行。
小故事理解 throw throws try...catch 的使用:
江川是一个快乐的小矿工,有一天他挖到了一个远古炸弹
标签:Java,System,day15,初学,File,println,new,public,out
From: https://www.cnblogs.com/qianzhiqiang798/p/18475525