C

Core Java tutorial for beginners

Clean • Professional

Java Arrays – Copying Arrays in Java with Examples

4 minute

Copying Arrays in Java

In Java, arrays are fixed in size, and sometimes you need to create a copy of an existing array — either fully or partially.

There are multiple ways to copy arrays in Java, ranging from manual loops to built-in methods like System.arraycopy(), Arrays.copyOf(), and Arrays.copyOfRange().

Java provides multiple ways to copy arrays — from simple loops to advanced built-in methods.


Types of Array Copying in Java

learn code with durgesh images

1. Manual Copy using Loop

You can copy elements one by one using a for or for-each loop.

public class ManualCopyExample {
    public static void main(String[] args) {
        int[] original = {10, 20, 30, 40, 50};
        int[] copy = new int[original.length];

        for (int i = 0; i < original.length; i++) {
            copy[i] = original[i];
        }

        System.out.print("Copied Array: ");
        for (int n : copy)
            System.out.print(n + " ");
    }
}

Output:

Copied Array: 10 20 30 40 50

Points to remember:

  • Easy to understand and beginner-friendly.
  • Works with both primitive and object arrays.
  • Slower for large arrays.

2. Using System.arraycopy()

The fastest and most efficient way to copy arrays in Java.

public class SystemArrayCopyExample {
    public static void main(String[] args) {
        int[] original = {1, 2, 3, 4, 5};
        int[] copy = new int[original.length];

        System.arraycopy(original, 0, copy, 0, original.length);

        System.out.print("Copied Array: ");
        for (int n : copy)
            System.out.print(n + " ");
    }
}

Output:

Copied Array: 1 2 3 4 5

Points to remember:

  • Performs shallow copy (copies references for objects).
  • Very fast, suitable for performance-critical code.
  • Used internally by Java libraries.

3. Using Arrays.copyOf() and Arrays.copyOfRange()

Java’s java.util.Arrays class provides two useful methods for copying arrays.

Example 1 – Arrays.copyOf()

import java.util.Arrays;

public class CopyOfExample {
    public static void main(String[] args) {
        int[] original = {10, 20, 30, 40, 50};

        int[] copy = Arrays.copyOf(original, original.length);

        System.out.println("Copied Array: " + Arrays.toString(copy));
    }
}

Output:

Copied Array: [10, 20, 30, 40, 50]

Example 2 – Arrays.copyOfRange()

import java.util.Arrays;

public class CopyOfRangeExample {
    public static void main(String[] args) {
        int[] original = {10, 20, 30, 40, 50};

        int[] subArray = Arrays.copyOfRange(original, 1, 4);

        System.out.println("Copied Range: " + Arrays.toString(subArray));
    }
}

Output:

Copied Range: [20, 30, 40]

Points to remember:

  • Creates a new array — doesn’t modify the original.
  • Ideal for resizing or partial copying.
  • Returns a copy with the specified length or range.

4. Using clone() Method

Every array in Java has a clone() method to make a copy.

public class CloneArrayExample {
    public static void main(String[] args) {
        int[] original = {5, 10, 15, 20};
        int[] copy = original.clone();

        System.out.println("Original Array: " + java.util.Arrays.toString(original));
        System.out.println("Cloned Array: " + java.util.Arrays.toString(copy));
    }
}

Output:

Original Array: [5, 10, 15, 20]
Cloned Array: [5, 10, 15, 20]

Points to remember:

  • Creates a shallow copy.
  • For primitive arrays, it behaves like deep copy.
  • For object arrays, only references are copied.

5. Using Stream API (Java 8 and Above)

Java 8 Streams offer a functional way to copy or filter arrays.

import java.util.Arrays;

public class StreamCopyExample {
    public static void main(String[] args) {
        int[] original = {3, 6, 9, 12, 15};

        int[] copy = Arrays.stream(original).toArray();

        System.out.println("Copied Array: " + Arrays.toString(copy));
    }
}

Output:

Copied Array: [3, 6, 9, 12, 15]

Points to remember:

  • Modern and clean approach using Streams.
  • Great for filtering, mapping, or transforming while copying.
  • Slightly slower than System.arraycopy() for large arrays.

Which Method Should You Use?

MethodBest Use CasePerformance
Manual LoopSmall arrays or beginnersSlow
System.arraycopy()High-speed copyingFastest
Arrays.copyOf() / copyOfRange()Resizing / range copyEfficient
clone()Simple shallow copyModerate
Stream APIFunctional programmingSlightly slower

 

Article 0 of 0