Java 数组拷贝的相关问题

Java 数组拷贝的相关问题

给定一个数组,我们需要将其元素复制到不同的数组中,how to?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static void demo1(){
int[] a = new int[]{1, 8, 3};
// Create an array b[] of same size as a[]
int b[] = new int[a.length];

// Doesn't copy elements of a[] to b[], only makes
// b refer to same location
b = a;
// Change to b[] will also reflect in a[] as 'a' and
// 'b' refer to same location.
b[0]++;

System.out.println("Contents of a[] ");
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
}
// output
Contents of a[]
2 8 3

Contents of b[]
2 8 3

显然,这是直接传递引用值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void demo2(){
int a[] = {1, 8, 3};

// Create an array b[] of same size as a[]
int b[] = new int[a.length];

// Copy elements of a[] to b[]
for (int i=0; i<a.length; i++)
b[i] = a[i];

// Change b[] to verify that b[] is different
// from a[]
b[0]++;

System.out.println("Contents of a[] ");
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
}
// out
Contents of a[]
1 8 3

Contents of b[]
2 8 3

b 重新开辟了空间,由于数组元素是值类型,所以,对b的修改不会同步到a。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void demo3() {
int a[] = {1, 8, 3};

// Copy elements of a[] to b[]
int b[] = a.clone();

// Change b[] to verify that b[] is different
// from a[]
b[0]++;

System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
//out
Contents of a[]
1 8 3

Contents of b[]
2 8 3

对数组的克隆,也是重新开辟空间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void demo4(){
int a[] = {1, 8, 3};

// Create an array b[] of same size as a[]
int b[] = new int[a.length];

// Copy elements of a[] to b[]
System.arraycopy(a, 0, b, 0, 3);

// Change b[] to verify that b[] is different
// from a[]
b[0]++;

System.out.println("Contents of a[] ");
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
}
//out
Contents of a[]
1 8 3

Contents of b[]
2 8 3

arraycopy也是重新开辟了空间。

上面都是对值类型数组的操作,那么对象数组呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public static void demo5(){
User[] a = new User[]{new User("abby"), new User("ben"), new User("charlene")};

// Create an array b[] of same size as a[]
User[] b = new User[a.length];

// Copy elements of a[] to b[]
System.arraycopy(a, 0, b, 0, 3);

// Change b[] to verify that b[] is different
// from a[]
b[0].setName("david");

System.out.println("Contents of a[] ");
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
System.out.println("\n++++++++++++ split +++++++++++");
for (int i=0; i<b.length; i++)
System.out.print(a[i] + "{"+a[i].equals(b[i]) +"}"+b[i]+"\n");
}
// output
Contents of a[]
User{name='david'} User{name='ben'} User{name='charlene'}

public static void demo6(){
User[] a = new User[]{new User("abby"), new User("ben"), new User("charlene")};

// Create an array b[] of same size as a[]
User[] b = new User[a.length];

// Copy elements of a[] to b[]
b=a.clone();

// Change b[] to verify that b[] is different
// from a[]
b[0].setName("david");

System.out.println("Contents of a[] ");
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");

System.out.println("\n\nContents of b[] ");
for (int i=0; i<b.length; i++)
System.out.print(b[i] + " ");
System.out.println("\n++++++++++++ split +++++++++++");
for (int i=0; i<b.length; i++)
System.out.print(a[i] + "{"+a[i].equals(b[i]) +"}"+b[i]+"\n");
}
Contents of b[]
User{name='david'} User{name='ben'} User{name='charlene'}
++++++++++++ split +++++++++++
User{name='david'}{true}User{name='david'}
User{name='ben'}{true}User{name='ben'}
User{name='charlene'}{true}User{name='charlene'}


//output
Contents of a[]
User{name='david'} User{name='ben'} User{name='charlene'}

Contents of b[]
User{name='david'} User{name='ben'} User{name='charlene'}
++++++++++++ split +++++++++++
User{name='david'}{true}User{name='david'}
User{name='ben'}{true}User{name='ben'}
User{name='charlene'}{true}User{name='charlene'}

可以看到对于对象数组,clone和arraycopy 会开辟空间,但是只是存储了元素的引用值。

-------------本文结束感谢您的阅读-------------
坚持分享,您的支持将鼓励我继续创作!
0%