Java-数组-异常(基础)
1.数组
相同数据类型的集合
数组的长度一旦定义就不能改变
数组中的每一个元素可以用下标表示位置,如果一个数组中有n个元素,那么下标的取值范围是0~n-1.
数组格式
数据类型[] 数组名 = new 数据类型[长度];
数据类型[] 数组名 = new 数据类型{元素1,元素2,...}
一个格式
int[]a = new int[5];
下一个数组定义格式
int [] a =new int[]{111,222,333};
length属性会返回数组长度
System.out.println(a.length);
全部打印
package com.kjxy.array;public class Test {public static void main(String[] args) {//int[]a = new int[5];//定义一个数组,开辟一个内存时间为10的//数组new出来的也应该在堆内存中int [] a =new int[]{111,222,333};//获取数据长度System.out.println(a.length);}
}
用for语句循环遍历数组长度
package com.kjxy.array;public class Test {public static void main(String[] args) {//int[]a = new int[5];//定义一个数组,开辟一个内存时间为10的//数组new出来的也应该在堆内存中int [] a =new int[]{111,222,333};//获取数据长度for (int i = 0; i <a.length ; i++) {System.out.println(a[i]);}}
}
数组如何在内存当中的实现
接下来我们用实例来演示一下
写一个person类
package com.kjxy.array;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
然后创建一个主程序
package com.kjxy.array;public class Test {public static void main(String[] args) {Person[] ps = new Person[3];ps[0] = new Person("张三",18);ps[1] = new Person("李四",19);ps[2] = new Person("王五",20);for (int i = 0; i < ps.length; i++) {System.out.println(ps[i]);}}
}
输出打印,以为是他们的名字,结果发现是他们的地址,这是调用了toStirng方法
如何实现只输出姓名加年龄呢,此时我们就需要修改一下代码
在person类当中添加一下tostring即可
@Override
public String toString() {return this.name+" " + this.age;
}
发现已经成功打印出姓名加方法了
接下来我们画图来表示其在内存中的演示情况
2.异常
异常是Java用来处理在程序执行期间出现问题的一种机制。有了异常,可以帮助定位程序执行期间出现的问题
1.抛出(throw)与捕获(catch)
抛出:当出现异常时,Java会自动生成一个异常类的对象,该对象中封装着当前出现的问题信息,然后会将该对象交给Java运行 时系统,这个过程叫做抛出(throw)
捕获:Java运行时系统接受异常类的对象,会根据对象的实际情况做出相应处理,这个过程叫做捕获
api文档演示
2.try...catch
try中写的是可能会出现异常的代码
一个try可以对应多个catch
如果执行期间出现异常,会终止后面代码的执行,直接执行对应catch中的代码,如果没有异常,正常顺序执行代码
其中finally当中的代码可写可不写
实例代码
不用try...catch语句
package com.kjxy.ex;public class Test1 {public static void main(String[] args) {int a =5 ;int b =0 ;System.out.println(a / b);}
}
用了语句
package com.kjxy.ex;public class Test1 {public static void main(String[] args) {int a =5 ;int b =0 ;try {System.out.println(a / b);}catch (ArithmeticException e){System.out.println("分母不能为0");}}
}
然后这样可以抛出错误
示例代码
package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("abc.txt");} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}
运行结果,发现系统找不到指定的文件
但是后面继续写代码的话,就不会抛出异常,代码会往下走,只有抛出异常的会出现报错,发现hello world会执行
package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("abc.txt");} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("hello world");}
}
进行演示finally关键字的使用
package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("Z:\\Desktop\\All\\Java\\20250612\\src\\com\\kjxy\\ex/abc.txt");} catch (FileNotFoundException e) {throw new RuntimeException(e);}finally {System.out.println("最后一定要执行的内容");}System.out.println("hello world");}
}
利用Java反射机制反射出当前的包
3.throw与throws
throws:声明某个方法可能会抛出哪些异常
throw:抛出某个异常类的对象
package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test3 {public static void main(String[] args) {fun1();}public static void fun1(){fun2();}public static void fun2(){fun3();}public static void fun3() throws FileNotFoundException {InputStream in =new FileInputStream("abc");}
}
发现fun3不报错了,但是fun2报错了,谁调用我,谁就去解决问题,或者使用try...catch语句直接使用直接在本行处理也行
发现错误抛出给fun3了
层层向上抛出,抛到Main函数选择用try...catch处理,这样就不会报错了
package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test3 {public static void main(String[] args) {try {fun1();} catch (FileNotFoundException e) {throw new RuntimeException(e);}}public static void fun1() throws FileNotFoundException {fun2();}public static void fun2() throws FileNotFoundException {fun3();}public static void fun3() throws FileNotFoundException {InputStream in =new FileInputStream("abc");}
}
此时我们再自定义一个类进行尝试
package com.kjxy.ex;public class Test4 {public static void main(String[] args) {fun(3,5);}public static void fun(int a,int b){if (a<b){throw new MyException("哈哈");}}
}
自定义的异常
package com.kjxy.ex;public class MyException extends Exception{public MyException(String message){super(message);}
}
发现有报错情况
所以这时候我们应该有两种情况,一种try...catch,一种向上抛出
所以我们选择向上抛出
抛出到main函数时我们选择使用try...catch捕获异常
更多的是看Java的api文档则更好解决问题