Skip to content

Commit 44f5742

Browse files
azrairshadzhendrikse
authored andcommitted
(BAEL-746) How to Copy an Array in Java (eugenp#1474)
1 parent 3140ea1 commit 44f5742

3 files changed

Lines changed: 249 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.arraycopy.model;
2+
3+
public class Address implements Cloneable {
4+
private String country;
5+
private String state;
6+
private String city;
7+
private String street;
8+
private String zipcode;
9+
10+
public String getCountry() {
11+
return country;
12+
}
13+
14+
public void setCountry(String country) {
15+
this.country = country;
16+
}
17+
18+
public String getState() {
19+
return state;
20+
}
21+
22+
public void setState(String state) {
23+
this.state = state;
24+
}
25+
26+
public String getCity() {
27+
return city;
28+
}
29+
30+
public void setCity(String city) {
31+
this.city = city;
32+
}
33+
34+
public String getStreet() {
35+
return street;
36+
}
37+
38+
public void setStreet(String street) {
39+
this.street = street;
40+
}
41+
42+
public String getZipcode() {
43+
return zipcode;
44+
}
45+
46+
public void setZipcode(String zipcode) {
47+
this.zipcode = zipcode;
48+
}
49+
50+
@Override
51+
protected Object clone() throws CloneNotSupportedException {
52+
super.clone();
53+
Address address = new Address();
54+
address.setCity(this.city);
55+
address.setCountry(this.country);
56+
address.setState(this.state);
57+
address.setStreet(this.street);
58+
address.setZipcode(this.zipcode);
59+
return address;
60+
}
61+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.arraycopy.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Employee implements Serializable {
6+
private static final long serialVersionUID = -2454619097207585825L;
7+
private int id;
8+
private String name;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.baeldung.arraycopy;
2+
3+
import com.baeldung.arraycopy.model.Address;
4+
import com.baeldung.arraycopy.model.Employee;
5+
import org.apache.commons.lang3.SerializationUtils;
6+
import org.junit.Assert;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import java.util.Arrays;
11+
12+
public class ArrayCopyUtilTest {
13+
private static Employee[] employees;
14+
private static final int MAX = 2;
15+
16+
@BeforeClass
17+
public static void setup(){
18+
employees = new Employee[MAX];
19+
Employee employee;
20+
for(int i = 0; i < MAX; i++) {
21+
employee = new Employee();
22+
employee.setName("Emp"+i);
23+
employee.setId(i);
24+
employees[i] = employee;
25+
}
26+
}
27+
28+
@Test
29+
public void givenArrayOfPrimitiveType_whenCopiedViaSystemsArrayCopy_thenSuccessful(){
30+
int[] array = {23, 43, 55};
31+
int[] copiedArray = new int[3];
32+
33+
System.arraycopy(array, 0, copiedArray, 0, 3);
34+
35+
Assert.assertTrue(array.length == copiedArray.length);
36+
Assert.assertTrue(copiedArray[0] == array[0]);
37+
Assert.assertTrue(copiedArray[1] == array[1]);
38+
Assert.assertTrue(copiedArray[2] == array[2]);
39+
}
40+
41+
@Test
42+
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaSystemsArrayCopy_thenSuccessful(){
43+
int[] array = {23, 43, 55, 12, 65, 88, 92};
44+
int[] copiedArray = new int[3];
45+
46+
System.arraycopy(array, 2, copiedArray, 0, 3);
47+
48+
Assert.assertTrue(3 == copiedArray.length);
49+
Assert.assertTrue(copiedArray[0] == array[2]);
50+
Assert.assertTrue(copiedArray[1] == array[3]);
51+
Assert.assertTrue(copiedArray[2] == array[4]);
52+
}
53+
54+
@Test
55+
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaArraysCopyOfRange_thenSuccessful(){
56+
int[] array = {23, 43, 55, 12, 65, 88, 92};
57+
58+
int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
59+
60+
Assert.assertTrue(3 == copiedArray.length);
61+
Assert.assertTrue(copiedArray[0] == array[1]);
62+
Assert.assertTrue(copiedArray[1] == array[2]);
63+
Assert.assertTrue(copiedArray[2] == array[3]);
64+
}
65+
66+
@Test
67+
public void givenArrayOfPrimitiveType_whenCopiedViaArraysCopyOf_thenValueChangeIsSuccessful(){
68+
int[] array = {23, 43, 55, 12};
69+
int newLength = array.length;
70+
71+
int[] copiedArray = Arrays.copyOf(array, newLength);
72+
73+
Assert.assertNotNull(copiedArray);
74+
Assert.assertTrue(copiedArray.length == array.length);
75+
Assert.assertTrue(copiedArray[0] == array[0]);
76+
Assert.assertTrue(copiedArray[1] == array[1]);
77+
Assert.assertTrue(copiedArray[2] == array[2]);
78+
Assert.assertTrue(copiedArray[3] == array[3]);
79+
array[0] = 9;
80+
Assert.assertTrue(copiedArray[0] != array[0]);
81+
copiedArray[1] = 12;
82+
Assert.assertTrue(copiedArray[1] != array[1]);
83+
}
84+
85+
@Test
86+
public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){
87+
Employee[] copiedArray = Arrays.copyOf(employees, employees.length);
88+
89+
Assert.assertNotNull(copiedArray);
90+
Assert.assertTrue(copiedArray.length == employees.length);
91+
employees[0].setName(employees[0].getName()+"_Changed");
92+
//change in employees' element caused change in the copied array
93+
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
94+
}
95+
96+
@Test
97+
public void givenArrayOfPrimitiveType_whenCopiedViaArrayClone_thenValueChangeIsSuccessful(){
98+
int[] array = {23, 43, 55, 12};
99+
100+
int[] copiedArray = array.clone();
101+
102+
Assert.assertNotNull(copiedArray);
103+
Assert.assertTrue(copiedArray.length == array.length);
104+
Assert.assertTrue(copiedArray[0] == array[0]);
105+
Assert.assertTrue(copiedArray[1] == array[1]);
106+
Assert.assertTrue(copiedArray[2] == array[2]);
107+
Assert.assertTrue(copiedArray[3] == array[3]);
108+
array[0] = 9;
109+
Assert.assertTrue(copiedArray[0] != array[0]);
110+
copiedArray[1] = 12;
111+
Assert.assertTrue(copiedArray[1] != array[1]);
112+
}
113+
114+
@Test
115+
public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
116+
Employee[] copiedArray = employees.clone();
117+
118+
Assert.assertNotNull(copiedArray);
119+
Assert.assertTrue(copiedArray.length == employees.length);
120+
employees[0].setName(employees[0].getName()+"_Changed");
121+
//change in employees' element changed the copied array
122+
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
123+
}
124+
125+
@Test
126+
public void givenArraysOfCloneableNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
127+
Address[] addresses = createAddressArray();
128+
129+
Address[] copiedArray = addresses.clone();
130+
131+
Assert.assertNotNull(copiedArray);
132+
Assert.assertTrue(copiedArray.length == addresses.length);
133+
addresses[0].setCity(addresses[0].getCity()+"_Changed");
134+
Assert.assertTrue(copiedArray[0].getCity().equals(addresses[0].getCity()));
135+
}
136+
137+
@Test
138+
public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){
139+
Employee[] copiedArray = SerializationUtils.clone(employees);
140+
141+
Assert.assertNotNull(copiedArray);
142+
Assert.assertTrue(copiedArray.length == employees.length);
143+
employees[0].setName(employees[0].getName()+"_Changed");
144+
//change in employees' element didn't change in the copied array
145+
Assert.assertFalse(copiedArray[0].getName().equals(employees[0].getName()));
146+
}
147+
148+
private Address[] createAddressArray(){
149+
Address[] addresses = new Address[1];
150+
addresses[0] = createAddress();
151+
return addresses;
152+
}
153+
154+
private Address createAddress() {
155+
Address address = new Address();
156+
address.setCountry("USA");
157+
address.setState("CA");
158+
address.setCity("San Francisco");
159+
address.setStreet("Street 1");
160+
address.setZipcode("59999");
161+
return address;
162+
}
163+
}

0 commit comments

Comments
 (0)