使用System.arraycopy拷贝数组
TestArrayCopy.java
/*
使用System.arraycopy拷贝数组
使用java.lang.System类的静态方法arraycopy
*/
public class TestArrayCopy {
public static void main(String args[]) {
String[] s = {"Mircosoft","IBM","Sun","Oracle","Apple"};
String[] sBak = new String[6];
//拷贝数组System.arraycopy
System.arraycopy(s,0,sBak,0,s.length);
for(int i=0;i<sBak.length;i++){
System.out.print(sBak[i]+" ");
}
System.out.println();
int[][] intArray = {{1,2},{1,2,3},{3,4}};
int[][] intArrayBak = new int[3][];
//拷贝数组System.arraycopy
System.arraycopy(intArray,0,intArrayBak,0,intArray.length);
//intArray和intArrayBak操作的值是同样的值
//改变intArrayBak的值
intArrayBak[2][1] = 100;
//intArray中对应的值同样改变了
for(int i = 0;i<intArray.length;i++){
for(int j =0;j<intArray[i].length;j++){
System.out.print(intArray[i][j]+" ");
}
System.out.println();
}
}
}
F:\java>javac TestArrayCopy.java F:\java>java TestArrayCopy Mircosoft IBM Sun Oracle Apple null 1 2 1 2 3 3 100 F:\java>