数组
存放数据的容器java
数组的声明
数据类型[] 数组变量名;
int a[3]; 非法的,声明数组时 []里面没有具体数值程序员
java中的数组 --》int[] arr = new int[5];//5表示数组的长度(容量)面试
数组也是一个引用类型,因此经过 new 开辟空间
-------------------------------------------------数据库
一维数组初始化数组
第一种:静态初始化jvm
int[] arr1 = {};
int[] arr2 = {1,3,4,5};ide
数组的长度 --》arr2.length(数组下标从0开始)
注意:数组超过本身的长度时(数组越界) 报数组下标越界异常this
第二种:动态初始化对象
普通动态初始化
int[] arr3 = new int[5];string
for(int i = 1;i<=5;i++){
arr3[i-1] =i;
}
-------------
引用动态初始化
public class Person{
public String name;
public Person(String name){
this.name = name;
}
@Override
public String toString() {
return "Person [name=" + name + "]";
}
}
-------
public class A{
public static void main(String[] args){
Person[] persons = new Person[5];
for(int i = 0;i<5;i++){
persons[i] = new Person("name_" + i);
}
for(int i = 0;i<persons.length;i++){
//输出对象 必须重写toString方法
System.out.println(persons[i]);
}
}
}
----------------------------------------------------------------
加强的for循环 --》jdk5.0中
for(数据类型 变量名 : 数组){System.out.println(变量名);}
其中变量名 随便取
int[] arr = new arr[10];
同等
for(int x:arr){
System.out.println(x);
}
--------------------------------------------------------------
二维数组 --》一维数组的数组
静态初始化
int[][] arr1 = {{1},{2,3,5},{3,7}};
二维数组的声明
特色:第一维必需要固定 即声明的时候必须明确指定
第二维能够不肯定 具体多长就看初始化多少值
int[][] arr2 = new int[3][];
int[][] arr3 = new int[3][3];
//arr2数组踩开辟好空间 arr3声明时 就进行空间开辟了
arr2[0] = new int[1];
arr2[1] = new int[3];
arr2[2] = new int[2];
动态初始化
for(int i = 0;i<arr2.length;i++){//arr2.length表示一维数组的长度
for(int j = 0;j<arr2[i].length;j++){//arr2[i].length表示二维里面的长度
}
}
-------------------------------------------------------------
异常
public class ExceptionDemo {
public static void main(String[] args) {
//受检查异常
//通常由程序员根据具体需求,本身折腾
//编译前,强制使用try catch 或者 throws 等等方式进行处理
try {
Class clzz = Class.class.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//非受检异常 (运行时异常)
//通常来讲,由系统定义,咱们是做为使用者
int i = 10 / 0 ;
}
}
-----------------------------------------------------------
public class ExceptionDemo2 {
public static void main(String[] args) {
//异常捕获
try { // 这里放可能出现异常的代码
}catch (NullPointerException e) {
//若是出现此类型的异常,进入这个代码空间
//打印错误信息,进行必须代码补救
//使用catch捕捉异常时,必定要注意:
//一、多个异常时不能把Exception 放在最前面,由于Exception是全部异常的父类
// 被抛出的异常,均可以进这个catch(子类转换成父类,向上转型)
//二、通常来讲,Exception用于兜底
//三、能够不存在catch try{} finally{} try{}catch(){}\
// try 语句,中catch 跟 finally能够同时存在,能够能够不一样时存在
//可是必须存一个,不是catch 就是 finally
}catch (ClassCastException e) {
}catch (Exception e) { //若是出现此类型的异常,进入这个代码空间
}finally {
//无论有没有异常,这里都执行
}
try{
int i = 10 / 1;
}catch(Exception e){
e.printStackTrace();
System.out.println("出现异常了...");
}finally {
//无论有没有异常,这里都执行
//运用:此处通常用于处理收尾工做
//数据库操,文件
System.out.println("这里是finally");
}
//e.printStackTrace()
//异常栈信息打印
//method3();
}
public static void method1(){
int i = 10/ 0;
}
public static void method2(){
method1();
}
public static void method3(){
try {
method2();
} catch (Exception e) {
}
}
-------------------------------------------------------------
//研究 finally
//面试陷阱 --- 出现频率跟 string 传值 差很少
public class ExceptionDemo3 {
public static void main(String[] args) {
//System.out.println(method());
System.out.println(method2());
}
public static int method2(){
int i = 0;
try {
System.out.println("进来了");
i = 10 / 1;
System.out.println("除完了" + i);
//return 跟 finally 配合的时候,return 的值会被保存一份,变量在
//finally 中进行操做,最终结果不影响保存的值
return i;
} catch (Exception e) {
System.out.println("exception");
return 2;
}finally {
System.out.println("finally");
++i;
//return 1;
}
}
public static int method(){
int i = 0;
try {
System.out.println("进来了");
i = 10 / 1;
System.out.println("除完了" + i);
return i;
} catch (Exception e) {
System.out.println("exception");
return 2;
}finally {
System.out.println("finally");
//return 1;
}
}
}
---------------------------------
throws throw 关键字
Throws 格式 public 返回值类型 方法名(参数列表) throws 异常类{}
throws --》声明的方法 表示此方法不处理异常 而交给方法的调用处 进行处理
throw --》用在方法代码中 主动抛出一个异常 若是是受检异常 必须用throws
关键字声明这个异常
Exception RuntimeException
类 MyException
MyException exends Exception{
public MyException(){
}
public MyException(String name){
super(name);}
public MyException(String name,Throwable throwabl){
super(name,throwable);
}
}
------------
类 ThrowNoSDemo2
public class ThrowNoSDemo2 {
public static void main(String[] args) {
try {
method2(2);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void method(){
File file = new File("aaaa");
// throws 强制性进行异常抛送
//FileReader fileReader = new FileReader(file);
}
public static void method2(int a) throws Exception{
if(a < 0){
//非检查异常
//程序主动抛异常,由程序进行控制
throw new RuntimeException("你传负数给我,我死掉算了....");
}
if(a % 2 == 0){
//受检查异常
//程序主动抛异常,由程序进行控制
throw new MyException("你传偶数给我,我又想死了,不过....");
}
}
}
------------
类 ThrowsDemo public class ThrowsDemo { public static void main(String[] args) { //try{ method(); //} catch (Exception e) { //} //method3(); } //处理异常的方法: //一、本身经过try...catch获取,而后处理 //二、经过throws 的方式把异常抛出,让调用者进行处理,若是调用者不进行处理,那么异常 // 继续向上抛,直到抛到jvm去,而后报错 // 运行时异常,不须要显式的使用throws 语句进行抛异常 public static void method(){ //try { //出现算术异常,不强制使用try进行获取,若是不catch,那么运行时候报错, //就直接抛到jvm进行处理, 也就最终的调用方main 那 //若是经过try 进去获取,那么这个异常会在方法内部本身独自解决 int i = 10/ 0; //} catch (Exception e) { //} } public static void method2() throws FileNotFoundException{ File file = new File("aaaa"); //受检查异常 //一、本身内部处理 /*try { FileReader fileReader = new FileReader(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ //二、经过throws 进行上抛,方法内不进行处理 FileReader fileReader = new FileReader(file); } public static void method3() throws FileNotFoundException{ //由于method2 显式抛出异常,因此,调用者必须对这个异常进行处理 //一、try..catch 自行处理 //二、继续向上抛 /*try { method2(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ method2(); } }