C

Core Java tutorial for beginners

Clean • Professional

Core 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

🏆 Top 5 Interview Questions – Copying Arrays in Java


1. What is the difference between shallow copy and deep copy in arrays?

Answer:

Copy TypeDescriptionExample Use
Shallow CopyCopies only references (not actual objects) — changes in one affect the other.Object arrays
Deep CopyCreates a new copy of actual data — both arrays are independent.Primitive type arrays or cloned custom objects

Example:

int[] a = {1, 2, 3};
int[] b = a; // Shallow copy (both point to same memory)

2. Which is the fastest method to copy arrays in Java?

Answer: System.arraycopy() is the fastest because it uses native (low-level) system code for memory copying.

It’s highly optimized and used internally by Java for performance-critical operations.


3. What is the difference between clone() and Arrays.copyOf()?

Answer:

Featureclone()Arrays.copyOf()
Return TypeReturns the same type of arrayYou can specify new length
Range SupportCopies full array onlyCan copy partially
Null SafetyCan throw NullPointerExceptionHandles safely
CustomizationLimitedFlexible resizing and range selection

4. How can you copy only a part (range) of an array in Java?

Answer: You can use Arrays.copyOfRange(original, from, to).

Example:

int[] arr = {10, 20, 30, 40, 50};
int[] sub = Arrays.copyOfRange(arr, 1, 4);
System.out.println(Arrays.toString(sub));

Output:

[20, 30, 40]

5. What happens when you clone an array of objects?

Answer: When you clone an object array, Java performs a shallow copy — it copies only references, not the actual objects.

That means changing an object inside the cloned array also affects the original.

Example:

String[] names = {"A", "B"};
String[] copy = names.clone();
copy[0] = "Z";
System.out.println(names[0]); // Output: "Z"

To achieve deep copy, you must manually clone each element.

Article 0 of 0