int[] numbers = { 2005, 1007, 1994, 2015 }; int[] tempNumbers=Arrays.copyOf(numbers, 6);//对于多余的元素将被赋予0值 System.out.println(Arrays.toString(tempNumbers));
还可以通过System.arraycopy方法
int[] numbers = { 2005, 1007, 1994, 2015 }; int[] tempNumbers=new int[5]; System.arraycopy(numbers, 0, tempNumbers, 1, numbers.length); System.out.println(Arrays.toString(tempNumbers)); numbers[0]=2000; System.out.println(tempNumbers[0]);
3.数组排序
int[] numbers = { 2005, 1007, 1994, 2015 }; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers));
多维数组对于二维数组和不规则数组,其实就是把数组的元素再看成一个数组来处理就可以。
String input=""; int[][] table={{11,15,9},{16,7,10},{15,12,13}}; for (int i = 0; i < table.length; i++) { int[] temp=table[i]; for (int j = 0; j < temp.length; j++) { input+=String.valueOf(temp[j])+" "; } } System.out.println(input);