定义语法
- int [] ary1 = new int[6]; //动态创建
- int [] ary2 = {1, 1, 2, 3, 5, 8}; //静态创建
获取数组值为 数组名[index],获取长度用 数组名.length
※定义数组,必须指明长度
java中的要求:
1. 数组存放的元素类型是统一的;
2. 数组定义好之后大小不能改变;
定义的时候要指定大小;
【数组操作常见问题】
1)下标(index)越界
ArrayIndexOutOfBoundsException
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 9
at Demo4_3.main(Demo4_3.java:34)
// 方式3:do...while
int i = 0;// 0~8
// 定义数据array,里面有9个元素
int [] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
do {
// 打印输出
System.out.println (array[i]);// array[9] ---ArrayIndexOutOfBoundsException
} while (i++ < array.length);
2)空指针
Exception in thread “main” java.lang.NullPointerException
at Demo4_5.main(Demo4_5.java:10)
https://www.cnblogs.com/QQ846300233/p/6683843.html
关于数组的倒序输出