Java Programming Handbook

    Java Copy Arrays

    In Java, sometimes we need to copy an array into another array. This is useful when we want to create a duplicate of an existing array without modifying the original one.

    In this blog, we will cover:

    Different ways to copy an array

    • Using Loop for copying
    • Using arraycopy() method
    • Using clone() method
    • Using Arrays.copyOf() method
    • Using Arrays.copyOfRange() method

    Example programs with expected outputs

    1. Copying an Array Using a Loop#

    The most basic way to copy an array is to use a loop.

    Example: Copying an array using a loop#

    class Main { public static void main(String[] args) { // Original array int[] original = {10, 20, 30, 40, 50}; // New array of the same size int[] copied = new int[original.length]; // Copy elements using a loop for (int i = 0; i < original.length; i++) { copied[i] = original[i]; } // Printing copied array System.out.print("Copied Array: "); for (int num : copied) { System.out.print(num + " "); } } }

    Output:#

    Copied Array: 10 20 30 40 50

    Best For: Small-sized arrays when performance is not a major concern.

    2. Copying an Array Using System.arraycopy()#

    The System.arraycopy() method is an efficient way to copy an array in Java.

    Syntax:#

    System.arraycopy(sourceArray, sourcePosition, destinationArray, destinationPosition, length);
    • sourceArray → The original array
    • sourcePosition → The starting index to copy from
    • destinationArray → The new array where values will be copied
    • destinationPosition → The index where copying starts in the destination array
    • length → Number of elements to copy

    Example: Using System.arraycopy()#

    class Main { public static void main(String[] args) { int[] original = {1, 2, 3, 4, 5}; int[] copied = new int[original.length]; // Copying using System.arraycopy System.arraycopy(original, 0, copied, 0, original.length); // Printing copied array System.out.print("Copied Array: "); for (int num : copied) { System.out.print(num + " "); } } }

    Output:#

    Copied Array: 1 2 3 4 5

    Best For: Fast copying when working with large arrays.

    3. Copying an Array Using clone()#

    The clone() method is used to create a shallow copy of an array. It works only for one-dimensional arrays.

    Example: Using clone()#

    class Main { public static void main(String[] args) { int[] original = {7, 14, 21, 28, 35}; // Using clone() to copy array int[] copied = original.clone(); // Printing copied array System.out.print("Copied Array: "); for (int num : copied) { System.out.print(num + " "); } } }

    Output:#

    Copied Array: 7 14 21 28 35

    Best For: Quick duplication of arrays when dealing with primitive types.

    Limitation: If the array contains objects, clone() performs shallow copying, meaning it only copies object references, not the actual objects.

    4. Copying an Array Using Arrays.copyOf()#

    The Arrays.copyOf() method is a simple way to copy an array while also allowing us to change its size.

    Example: Using Arrays.copyOf()#

    import java.util.Arrays; class Main { public static void main(String[] args) { int[] original = {5, 10, 15, 20, 25}; // Copying entire array int[] copied = Arrays.copyOf(original, original.length); // Printing copied array System.out.print("Copied Array: "); for (int num : copied) { System.out.print(num + " "); } } }

    Expected Output:#

    Copied Array: 5 10 15 20 25

    Best For: Copying an entire array or resizing an array during copying.

    5. Copying a Portion of an Array Using Arrays.copyOfRange()#

    The Arrays.copyOfRange() method is useful when we need to copy only a specific range of elements.

    Syntax:#

    Arrays.copyOfRange(sourceArray, fromIndex, toIndex);
    • fromIndex → Start index (inclusive)
    • toIndex → End index (exclusive)

    Example: Copying a Range of an Array#

    import java.util.Arrays; class Main { public static void main(String[] args) { int[] original = {2, 4, 6, 8, 10, 12}; // Copying elements from index 2 to 5 (excluding 5) int[] copied = Arrays.copyOfRange(original, 2, 5); // Printing copied array System.out.print("Copied Array: "); for (int num : copied) { System.out.print(num + " "); } } }

    Expected Output:#

    Copied Array: 6 8 10

    Best For: Extracting a sub-array from a larger array.

    Choosing the Right Method to Copy an Array#

    MethodWhen to Use?
    Loop MethodFor small arrays, simple approach
    System.arraycopy()For fast, efficient copying
    clone()For shallow copy of one-dimensional arrays
    Arrays.copyOf()When resizing or copying the full array
    Arrays.copyOfRange()When copying a portion of an array

    Conclusion#

    In this blog, we learned:

    • How to copy arrays using loops
    • How to use System.arraycopy() for fast copying
    • How clone() creates a duplicate of an array
    • How to use Arrays.copyOf() and Arrays.copyOfRange()
    • When to use each method

    Each method has its advantages, and the best choice depends on the size of the array and performance considerations.

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more

    Last updated on Apr 09, 2025

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.