Languages
[Edit]
EN

Java - clone array

0 points
Created by:
Nikki-Mathews
517

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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