Math
floor():向下取整
ceil():向上取整
round():四舍五入
自动装箱与拆箱
装箱:自动将基本数据类型转换为包装器类型。
拆箱:自动将包装器类型转换为基本数据类型。
Scanner
1.引入包:import java.util.Scanner;
输入:Scanner input = new Scanner(System.in);
2.获取输入:int c=input.nextInt();
next()只读空格之前的数据,且光标指向本行。
nextLine()读到行尾,光标定位在下一行。
数组
1.定义与内存分配
定义:int[] arr;
或int arr[];
分配内存:int[] arr; arr=new int[3];
或int[] arr=new int[3];
String[] str={new 类(), new类(), ....}
二维:int[ ][ ] num=new int[2][3];
2.初始化
静态:声明与初始化同时进行。一维:int[] arr=new int[]{1,2,3};
二维:int[ ][ ] num={{1,2,3},{4,5}};
动态:声明后初始化。
int[ ] score=new int[3];
score[0]=12;
score[1]=24;
score[2]=10;
不规则二维:只指定行,列单独定。
int[ ][ ] num=new int[3][ ];
num[0]=new int[2];//第一行两列
num[1]=new int[3];//第二行三列
num[2]=new int[4];//第三行四列
3.行数:num.length
第i+1列长度:num[i].length
异常
父类Throwable——子类Error+Exception。
1.Error类:系统溢出:虚拟机错误、编程死锁etc
2.Exception:编码、环境、用户操作问题,包括检查异常+非检查异常RuntimeException。
A.非检查:非包括空指针、数组下标越界、类型转换、算术异常。
B,检查:文件异常IOException、SQL异常(连接错误)。手动添加throw/catch。
3.try{运行内容}
catch(xxxxxxxException e){异常处理} //顺序:先子类后父类
…
catch(Exception e){
e.printStackTrace();
return -1;
}
finally{最终要执行的} //在执行catch中的return前执行
4.抛出异常:throws:声明类型。
public void 方法名(参数) throws 异常列表{
//方法或:
throw new Exception();
5.自定义异常:class 类 extends 异常类
6.向上抛出,异常链,包装处理。
多重catch块后可加个catch(Exception)处理漏的。
尽量添加finally块释放资源。