Java ProgramsArraysCopy Array

Copy Array in Java

beginner·  Arrays  ·  Array Manipulation

Problem

Copying an array means creating a brand-new array holding the same values, so changes to one array never affect the other.

Given an array of integers, create a copy of it.

Input
[3, 6, 9, 12]
Output
Copy: [3, 6, 9, 12]

Java Program

Java
public class CopyArray { public static void main(String[] args) { int[] original = {3, 6, 9, 12}; int[] copy = new int[original.length]; for (int i = 0; i < original.length; i++) { copy[i] = original[i]; // copies the value, not the array reference } System.out.println("Copy: " + java.util.Arrays.toString(copy)); } }

Output

Copy: [3, 6, 9, 12]

Core Logic

Allocating a brand-new array of the same size, then copying each element across one at a time, produces an independent duplicate.

How It Works
  1. 1int[] copy = new int[original.length] allocates a new array sized to match the original.
  2. 2A loop visits every index from 0 to original.length - 1.
  3. 3copy[i] = original[i] copies each value across into the new array.
  4. 4The two arrays now hold identical values, but are separate objects in memory.
For [3, 6, 9, 12], each of the four values is copied into copy at the same index it held in original.
💡

Key Point: Assigning copy = original directly would NOT create a copy — both variables would point to the same array, so changing one would change the other. Only copying element by element (or using a copying method) produces a truly independent array.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Every element is visited once, and the new array holds its own copy of all n values.

Key Concepts

for loopnew array allocation

Approach 2: Using Arrays.copyOf()

Java
import java.util.Arrays; public class CopyArrayUsingCopyOf { public static void main(String[] args) { int[] original = {3, 6, 9, 12}; // copyOf() allocates a new array of the given length and copies into it int[] copy = Arrays.copyOf(original, original.length); System.out.println("Copy: " + Arrays.toString(copy)); } }

Output

Copy: [3, 6, 9, 12]

Core Logic

In real code, there's no reason to loop manually — Arrays.copyOf() already builds a correctly sized copy in one call.

How It Works
  1. 1Arrays.copyOf(original, original.length) takes the source array and the length of the new array to create.
  2. 2It allocates a new array of that length and copies as many elements as will fit.
  3. 3Passing original.length as the target length copies every element with none left out.
Arrays.copyOf(new int[]{3, 6, 9, 12}, 4) returns a brand-new array holding the same four values.
💡

Key Point: Passing a length shorter than the original truncates the copy, and passing a longer length pads the extra slots with 0 — the target length doesn't have to match the source exactly.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Arrays.copyOf() still copies every element into a newly allocated array, the same underlying work as the manual loop.

Key Concepts

Arrays.copyOf()

Approach 3: Using System.arraycopy()

Java
public class CopyArrayUsingArraycopy { public static void main(String[] args) { int[] original = {3, 6, 9, 12}; int[] copy = new int[original.length]; // Copies original.length elements from original[0] into copy[0] System.arraycopy(original, 0, copy, 0, original.length); System.out.println("Copy: " + java.util.Arrays.toString(copy)); } }

Output

Copy: [3, 6, 9, 12]

Core Logic

System.arraycopy() is the low-level method Arrays.copyOf() itself uses internally — calling it directly skips one layer of indirection.

How It Works
  1. 1A new array copy is still allocated first, sized to match original.
  2. 2System.arraycopy(original, 0, copy, 0, original.length) copies elements from original starting at index 0, into copy starting at index 0, for original.length elements.
  3. 3This is a native method, implemented outside plain Java code for speed.
System.arraycopy({3, 6, 9, 12}, 0, copy, 0, 4) fills copy with the same four values.
💡

Key Point: System.arraycopy() is what both Arrays.copyOf() and Object.clone() on arrays ultimately call under the hood — it's the fastest way to copy array data in Java.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The native copy still has to move every element into the newly allocated array, the same total work as the other two versions.

Key Concepts

System.arraycopy()

Related Programs