复制数组变量 例如 int myArray[{1,2,3,4); int another[myArray; 复制数组元素 System.arraycopy(from,fromIndex,to, tolndex,count) 例如 int myArray[{1,2,3,4); int another[new int4]; Sysytem.arraycopy(myArray,0,another,0,4);
7-16 Programming in Java • 复制数组变量 – 例如 int myArray[ ] = {1,2,3,4}; int another[ ] = myArray; 数组的复制 • 复制数组元素 – System.arraycopy(from, fromIndex, to, toIndex, count) – 例如 int myArray[ ] = {1,2,3,4}; int another[ ] = new int[4]; Sysytem.arraycopy(myArray,0,another,0,4);
下 从一个方法里返回一个数组 Public class ArrayReturn public int[returnsArray(boolean flag) int[array1={1,2,3,4,5,6}; int[]array2={10,20,30}; if (flag){return array1; else return array2; public static void main(Sring[args){ ArrayReturn x new ArrayReturnO; System.out.println(x.returnsArray(true).length);}}
7-17 Programming in Java 从一个方法里返回一个数组 示例 Public class ArrayReturn { public int[ ] returnsArray(boolean flag) { int[ ] array1 = {1,2,3,4,5,6}; int[ ] array2 = {10,20,30}; if (flag) { return array1; } else { return array2; } } public static void main(Sring[ ] args){ ArrayReturn x = new ArrayReturn(); System.out.println(x.returnsArray(true).length);}}