java 2 维数组_Java中二维数组和异常的内容及应用

一、二维数组

数组中嵌套数组就是二维数组

二维数组的声明

数据类型[][] 数组名; --推荐

数据类型 数组名[][];

初始化:

动态:

数据类型[][] 数组名 = new 数据类型[一维的长度][二维的长度];--每一个第二位的小数组长度相同

数据类型[][] 数组名 = new 数据类型[一维的长度][];--第二位的每一个小数组的长度可以不同,第二位的小数组还没有创建

每个数组 arr[外层数组索引]=new 数据类型[长度] |...一维数组创建方式

静态:

数据类型[][] 数组名 = new 数据类型[][]{{1,2},{1},{1,2,3}...};

数据类型[][] 数组名 = {{1,2},{1},{1,2,3}...};

二维数组的遍历:

双重for循环嵌套

1、例子

public class ArrayDemo02 {

public static void main(String[] args) {

//二维数组的声明

int[][] arr;

//动态初始化1

arr=new int[2][3];

//赋值

arr[0][0]=1;

arr[0][1]=2;

arr[0][2]=3;

arr[1][0]=4;

arr[1][1]=5;

arr[1][2]=6;

//根据索引获取值

System.out.println(arr[1][2]);

//动态初始化2

String[][] arr2=new String[3][];

arr2[0]=new String[2];

arr2[0][0]="英";

arr2[0][1]="雄";

arr2[1]=new String[]{"教案"};

arr2[2]=new String[]{"哈哈","呵呵","嘻嘻"};

//arr2[2]={"哈哈","呵呵","嘻嘻"}; 不能使用这种简易方式创建第二维数组

System.out.println("------遍历1----------");

//i是外层第一维数组的索引

for(int i=0;i

for(String s:arr2[i]){

System.out.println(s);

}

}

System.out.println("---------遍历2-----------");

for(String[] a:arr2){

for(String s:a){

System.out.println(s);

}

}

System.out.println("---------通过遍历赋值arr数组-----------");

int num=101;

/*for(int i=0;i

for(int j=0;j

arr[i][j]=num;

num++;

}

}*/

for(int[] i:arr){

for(int j=0;j

i[j]=num++;

}

}

System.out.println("---------遍历刚刚赋值的数组arr-----------");

for(int[] a:arr){

for(int s:a){

System.out.println(s);

}

}

//静态初始化1

int[][] arr3 = new int[][]{{1,2},{1},{1,2,3}};

for(int[] a:arr3){

for(int s:a){

System.out.println(s);

}

}

//静态初始化2

int[][] arr4 ={{1,2},{1},{1,2,3}};

print(new int[][]{{1,2},{1},{1,2,3}});

}

public static void print(int[][] arr){

for(int[] a:arr){

for(int s:a){

System.out.println(s);

}

}

}

}

二、异常

1、异常:

程序生病了

Throwable

/\

Error Exception

/ \

CheckedException RuntimeException

Error:这类错误一般是虚拟机生产或脱出的,程序员无法控制,不需要管

Exception:

检查时异常|编译时异常 CheckedException:如果遇到,必须进行处理,否则程序无法运行,发生在编译期

运行时异常 RuntimeException:程序运行才知道是否有异常 增强程序的健壮性处理 if..

常见的一些运行时异常:

1.空指针异常NullPointerException

2.数组下标越界 ArrayIndexOutOfBoundsException

3.负数异常 NegativeArraySizeException

4.数学异常 ArithmeticException

5.字符串索引越界异常 StringIndexOutOfBoundsException

1、代码

public class ExceptionDemo04 {

public static void main(String[] args) {

String str=null;

//增强程序的健壮性

if(str!=null){

str.charAt(3);

}else{

str="haha";

str.charAt(6);

}

//ArrayIndexOutOfBoundsException

/*int[] arr=new int[3];

System.out.println(arr[5]);*/

int[] arr=new int[-3];

System.out.println(5/0);

//编译时异常

InputStream is=new FileInputStream("D:/test.txt");

}

}

2、throw :

制造异常

异常处理方式:

抛出异常: throws 把异常抛到上一层

捕获异常:

try{

可能会出现异常的代码

}catch(ClassNotFoundException e){ //= new ClassNotFoundException();

如果出现ClassNotFoundException类型的异常,执行这里的代码....

}catch(NullPointerException e){

如果出现NullPointerException异常,执行这里的代码

}...

catch(Exception e){

接收除了以上的其他的异常,执行这里的代码

}finally{

无论try中的代码是否出现异常,finally中这里的内容一定会执行

}

注意:

1.try后面可以接一个到多个catch,捕获不同类型的异常

2.把大范围的catch写在最后,小范围写在前面,否则永远执行不到

3.如果try中一旦出现异常,try中的后面的代码都不会执行,执行对应catch中的代码

(1)代码

public class ExceptionDemo05 {

public static void main(String[] args) {

try{

String[] s=null;

System.out.println(s[1]);

InputStream is=new FileInputStream("D:/test.txt");

System.out.println(5/0);

System.out.println("没有出现异常");

}catch(NullPointerException e){

e.printStackTrace();

System.out.println(e.getMessage());

System.out.println("出现了空指针啦");

} catch (FileNotFoundException e) {

e.printStackTrace();

System.out.println("出现了文件未找到啦");

} catch(Exception e){

e.printStackTrace();

System.out.println("出现异常啦");

} finally{

System.out.println("一定会执行的代码");

}

//如果代码一旦出现异常,后面的代码执行不了

System.out.println("哈哈");

System.out.println("哈哈");

System.out.println("哈哈");

System.out.println("哈哈");

System.out.println("哈哈");

System.out.println("哈哈");

new Demo().hehe(4);

}

}

class Demo{

void test() throws ClassNotFoundException{

throw new ClassNotFoundException();

}

void haha() throws NullPointerException, ClassNotFoundException{

test();

}

void hehe(int a){

try {

if(a==5){

return;

}

String[] s=null;

System.out.println(s[1]);

} catch (NullPointerException e) {

e.printStackTrace();

}finally{

System.out.println("一定会执行的代码");

}

System.out.println("123235435464576");

}

}

//如果存在方法的重写, 子类重写方法抛出的异常<=父类的方法抛出的异常

class Fu{

void test() throws ClassNotFoundException{

}

}

class Zi extends Fu{

void test() throws ClassNotFoundException{

}

}

3、自定义异常:

除了java提供的异常类以外,可以自定义异常

学习异常:

1.异常分类,特点

2.异常处理 ***

编译时异常:1)throws 2)try..catch

运行时异常:1)增强程序健壮性 2)throws 3)try..catch

3.自定义异常,使用

(1)代码

public class DefinedException {

public static void main(String[] args) {

Person p=new Person();

p.setName("彭于晏");

try{

p.setAge(-35);

}catch(AgeException e){

System.out.println(e.getMessage());

e.printStackTrace();

}

System.out.println(p);

}

}

//自定义异常 继承异常类 Exception-->编译时异常 继承自RuntimeException-->运行时异常

class AgeException extends Exception{

//class AgeException extends RuntimeException{

private String message;

public AgeException() {

// TODO Auto-generated constructor stub

}

public AgeException(String message) {

this.message = message;

}

public String getMessage() {

return message;

}

}

class Person{

private String name;

private int age;

public Person() {

// TODO Auto-generated constructor stub

}

public Person(String name, int age) {

super();

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

}

public int getAge() {

return age;

}

public void setAge(int age) throws AgeException {

if(age>=1 && age<=150){

this.age = age;

}else{

throw new AgeException(age+"年龄不合法啦");

}

}

@Override

public String toString() {

return "Person [name=" + name + ", age=" + age + "]";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值