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