Languages
[Edit]
EN

Java - clone array

0 points
Created by:
Nikki-Mathews
337

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

Java - Arrays (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join