EN
Java - clone array
0 points
In this article, we would like to show you how to clone array in Java.
Quick solution:
xxxxxxxxxx
1
int[] original = new int[]{1, 2, 3};
2
int[] clone = original.clone();
In this example, we use clone()
method to make a copy of the existing array.
xxxxxxxxxx
1
public class Example {
2
3
public static void main(String[] args) {
4
int[] original = new int[]{1, 2, 3};
5
int[] clone = original.clone();
6
7
System.out.println("Original array elements:");
8
for (int x : original) {
9
System.out.println(x);
10
}
11
12
System.out.println("Clone array elements:");
13
for (int x : clone) {
14
System.out.println(x);
15
}
16
}
17
}
Output:
xxxxxxxxxx
1
Original array elements:
2
1
3
2
4
3
5
6
Clone array elements:
7
1
8
2
9
3