day05--数组的异常_索引越界异常的操作介绍(取值和赋值)
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.inmind.array01;
|
||||
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//定义一个整数变量
|
||||
int i = 10;
|
||||
System.out.println(i);
|
||||
//简写形式定义一个整数数组
|
||||
int[] arr = {1, 2, 3};
|
||||
int[] arr1 = {4, 5, 6};
|
||||
System.out.println(arr[1]);
|
||||
System.out.println(arr1[2]);
|
||||
|
||||
System.out.println(arr);
|
||||
System.out.println(arr1);
|
||||
|
||||
arr = arr1;
|
||||
System.out.println(arr);
|
||||
System.out.println(arr1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.inmind.array01;
|
||||
|
||||
/**
|
||||
* ArrayIndexOutOfBoundsException
|
||||
* 数组索引越界异常
|
||||
* 注意:数组的长度固定不变的,数组的最大的索引值为arr.length-1
|
||||
* 0~(arr.length-1)
|
||||
*/
|
||||
public class Demo03 {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {1, 2, 3};
|
||||
System.out.println(arr[0]);
|
||||
System.out.println(arr[1]);
|
||||
System.out.println(arr[2]);
|
||||
//如果使用错误的索引访问数组的数据,那就会报越界异常java.lang.ArrayIndexOutOfBoundsException
|
||||
//System.out.println(arr[3]);
|
||||
int index = 2;
|
||||
if (index > 0 && index < arr.length) {
|
||||
System.out.println(arr[index]);
|
||||
}
|
||||
|
||||
System.out.println("程序正常结束");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user