package array;
/**
* java之数组复制
* <p>ClassName: TestArrayCopy</p>
* <p>Description: 数组复制</p>
* <p>Author: Administrator</p>
* <p>Date: 2016年4月26日</p>
*/
public class TestArrayCopy {
public static void main(String args[]) {
//一维数组复制
String[] from = { "Mircosoft", "IBM", "Sun", "Oracle", "Apple" };
String[] to = new String[6];
System.arraycopy(from, 0, to, 0, from.length);
for (int i = 0; i < to.length; i++) {
System.out.print(to[i] + " ");
}
System.out.println();
System.out.println("///");
//二维数组复制
int[][] intArray = { { 1, 2 }, { 3, 4, 5 }, { 6, 7 } };
int[][] intArrayBak = new int[3][];
System.arraycopy(intArray, 0, intArrayBak, 0, intArray.length); //copy的是引用,即堆内存的地址
intArrayBak[2][1] = 100; //改变堆内存中相应地址所存放的值
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();
}
}
}
运行结果:
Mircosoft IBM Sun Oracle Apple null
///
1 2
3 4 5
6 100