数组的概念
数组的定义(声明)
int[] arr = new int[3];//0,0,0
int[] arr = new int[]{100,200,300};
int[] arr = {100,200,300};
int[] arr; //先声明
arr = {3,4,5}; //编译错误
arr = new int[]{5,6,7};//正确
数组的访问
- 数组的长度:数组名.length ;获取数组的长度
int[] arr = new int[]{100,200,300};
int len = arr.length;
- 通过下标来访问数组中的元素;下标是从0开始的,最大的下标值为:(数组.length-1)
int[] arr = new int[3];
arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
arr[3] = 500; //数组下标越界异常
- 数组下标越界异常:java.lang.ArrayIndexOutOfBoundsException
数组的复制
System.arraycopy(源数组名,起始复制位置,目标数组名,目标数组下标起始位置,复制的个数)
数组的扩容
int[] arr = new int[] {111,222,333,444,555};
int[] arr2 = Arrays.copyOf(arr, 10);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
网友评论