Java基础知识点_10
异常体系
异常体系结构:
Throwable :顶层父类
Error :错误,没有办法进行捕获处理的非常严重的错误
Exception :异常
RuntimeException : 运行时异常
除RuntimeException 之外: 编译时期异常
throw关键字和throws关键字的区别
1、throw是对异常对象的抛出(放在方法体中),throws是对异常类型的声明(放在方法头后面)
2、throw是对异常对象实实在在的抛出,一旦使用了throw关键字,就一定有一个异常对象出现;throws是对可能出现的异常类型的声明,即使声明了一些异常类型,在这个方法中,也可以不出现任何异常。
3、throw后面只能跟一个异常对象;throws可以跟很多个异常类型
数组和集合获取长度的方法
数组:.lengh 注意:没有lengh后面没有括号
集合: .size()
泛型的好处
1、提高了数据的安全性,将运行时的问题,提前暴露在编译时期
2、避免了强转的麻烦
数组的计算笔试题
a.获取数组的最大、最小值
package com.tohka.demos;
public class Demo1 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i]>max) {
max =arr[i];
}
}
System.out.println(max);
}
}
b.数组元素交换
public class Demo2 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int temp = arr[1];
arr[1] = arr[2];
arr[2] = temp;
System.out.println(Arrays.toString(arr));
}
}
c.数组的反转
package com.tohka.demos;
import java.util.Arrays;
public class Demo3 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
for (int i = 0; i <= arr.length/2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
System.out.println(Arrays.toString(arr));
}
}

浙公网安备 33010602011771号