数组

2020-12-31  本文已影响0人  GuoDJ

数组的概念

数组的定义(声明)

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};//正确

数组的访问

int[] arr = new int[]{100,200,300};
int len = arr.length;
int[] arr = new int[3];
arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
arr[3] = 500; //数组下标越界异常

数组的复制

 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]);
}
上一篇下一篇

猜你喜欢

热点阅读