数组

数组:用来存储一批同种类型的内存区域

数组变量名存储的是数组在内存中的地址,数组是引用类型。

数组的最大索引:最大长度减1 length-1


//数组: 数据类型 [] 数组名称 = new 数据类型[]{元素1,....}
//     数据类型 [] 数组名称 = {元素1,....}
//double [] scores = new double[]{99.5, 99.3,88.9};
double [] scores = {99.5, 99.3,88.9};
int [] ages ={1, 2, 3 };
String [] names ={"hello","world"};

//取值 :数组名[索引]
int [] ids = {12, 13 , 14};
//             0   1   2
System.out.println(ids[0]);//12

//赋值 :数组名[索引] = 数据
ids[2] = 15;
System.out.println(ids[2]);


//数组长度
System.out.println(ids.length);

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

 

数组使用常见的问题:

1.如果访问的元素位置超过最大索引执行时,会出现ArrayIndexOutOfBoundsException(数组索引越界异常);

2.如果数组变量中没有存储存储数组的地址,而是null,那在访问数组信息时会出现NullPointerException(空指针异常)

posted @ 2022-01-05 21:14  wtws  阅读(43)  评论(0)    收藏  举报