package com.itjob.test2;
import java.util.Arrays;
public class ArrayCopy {
/**
* @author x521
* @createDate 2008-5-21
* 一维数组的拷贝---浅拷贝
*/
public static void main(String[] args) {
copyArray();
copyObject();
}
public static void copyArray() {
int[] a = new int[5];
int[] b = new int[6];
a[0] = 12;
a[1] = 45;
a[2] = 90;
a[3] = 100;
a[4] = 160;
for(int aa : a) {
System.out.print(aa + " ");
}
System.out.println("/n ---");
System.arraycopy(a, 0, b, 1, a.length);
for(int bb : b) {
System.out.print(bb + " ");
}
System.out.println();
System.out.println("************");
a[4] = 200;
for(int bbb : b) {
System.out.print(bbb + " ");
}
System.out.println("/n*************");
for(int aaa : a) {
System.out.print(aaa + " ");
}
System.out.println("/n****************");
}
public static void copyObject() {
TestCopy[] tc1 = new TestCopy[2];
TestCopy[] tc2 = new TestCopy[2];
tc1[0] = new TestCopy();
tc1[1] = new TestCopy();
tc1[0].setName("h1");
tc1[1].setName("h2");
for(TestCopy co : tc1) {
System.out.print(co.getName() + " ");
}
System.out.println();
System.arraycopy(tc1, 0, tc2, 0, tc1.length);
for(TestCopy tc : tc2) {
System.out.print(tc.getName() + " ");
}
//重新设置tc2[1]的值 后 tc1[1]的值 也发生了变化
// 这种情况属于 数组的浅拷贝问题
tc2[1].setName("hello");
System.out.println();
for(TestCopy tc11 : tc1) {
System.out.print(tc11.getName() + " ");
}
System.out.println("/n----");
}
}
class TestCopy {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// output:
12 45 90 100 160
---
0 12 45 90 100 160
************
0 12 45 90 100 160
*************
12 45 90 100 200
****************
h1 h2
h1 h2
h1 hello
----
/*
* TwoArrayCopy.java
*/
package com.itjob.test2;
import java.util.Arrays;
public class TwoArrayCopy {
/**
* @author x521
* @createDate 2008-5-21
* 二维数组的拷贝---浅拷贝
*/
public static void main(String[] args) {
int[][] a = new int[2][3];
int[][] b = new int[2][3];
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("****************");
a[0][2] = 100;
for(int[] aa : a) {
System.out.println(Arrays.toString(aa));
}
System.out.println("****************");
System.arraycopy(a, 0, b, 0, a.length);
//修改a[1][2]的值 ,并且b[1][2]的值也被改变
a[1][2] = 500;
for(int[] bb : b) {
System.out.println(Arrays.toString(bb));
}
System.out.println("****************");
for(int[] aa : a) {
System.out.println(Arrays.toString(aa));
}
}
}
//output:
0 0 0
0 0 0
****************
[0, 0, 100]
[0, 0, 0]
****************
[0, 0, 100]
[0, 0, 500]
****************
[0, 0, 100]
[0, 0, 500]
/*
* TwoArrayCopy1.java
*/
package com.itjob.test2;
import java.util.Arrays;
public class TwoArrayCopy1 {
/**
* @author x521
* @createDate 2008-5-21
* 二维数组的拷贝---深拷贝
*/
public static void main(String[] args) {
int[][] a = new int[2][3];
int[][] b = new int[2][3];
a[0][0] = 12;
a[0][1] = 23;
a[0][2] = 20;
a[1][0] = 15;
a[1][1] = 11;
a[1][2] = 12;
for(int[] aa : a) {
System.out.println(Arrays.toString(aa));
}
System.out.println("**************");
for(int i=0; i<a.length; i++) {
for(int j=0; j<a[i].length; j++) {
b[i][j] = a[i][j];
}
}
for(int[] bb : b) {
System.out.println(Arrays.toString(bb));
}
System.out.println("**************");
//修改b[0][2]的值 而 a[0][2]的值没有被改变
//这属于多维数组的深拷贝
b[0][2] = 100;
for(int[] aaa : a) {
System.out.println(Arrays.toString(aaa));
}
System.out.println("**************");
for(int[] bbb : b) {
System.out.println(Arrays.toString(bbb));
}
System.out.println("**************");
}
}
//output :
[12, 23, 20]
[15, 11, 12]
**************
[12, 23, 20]
[15, 11, 12]
**************
[12, 23, 20]
[15, 11, 12]
**************
[12, 23, 100]
[15, 11, 12]
**************