01.Java基础
学习笔记
数组
一维数组
int[] nums=new int[10];
int[] nums={1,2,3};
二维数组
int[][] array=new int[2][3];
int[][] array={{1,2,3},{2,3,4};
// 打印二维数组
for (int[] ints:a){
for (int anint:ints){
System.out.print(anint+" ");
}
System.out.println();
}
Arrays类
//打印数组元素
System.out.println(Arrays.toString(nums));
Arrays.sort(nums);
//填充
Arrays.fill(num,1);
异常
throw和throws
//throw:主动抛出异常在方法中使用
throw new ArithmeticException();
//throws:假设这个方法中,处理不了这个异常。在方法上抛出异常
public void test()throws ArithmeticException{}
自定义异常
//自定义异常类,继承Exception
public class MyException extends Exception {
private int detail;
public MyException(int a) {
this.detail=a;
}
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
}
// 测试类
public class Test {
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException"+e);
}
}
static void test(int a) throws MyException {
if (a>10){
throw new MyException(a);
}
System.out.println("OK");
}
}