arraysCopy common useCase

Explain how is System.arraycopy( ) used in java arrays

System.arraycopy() is a native, highly optimized method in Java used to copy a chunk of data from one array to another.

Because it is implemented in native C/C++ code under the hood, it bypasses the standard Java loop overhead. It directly copies chunks of memory, making it incredibly fast—significantly faster than using a manual for loop to copy elements one by one.

The Method Anatomy

The method takes 5 specific arguments:

Java
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
  • src: The source array you want to copy from.

  • srcPos: The starting index in the source array where the copying should begin.

  • dest: The destination array you want to copy to.

  • destPos: The starting index in the destination array where the copied data should be pasted.

  • length: The exact number of elements you want to copy.

Visualizing how it works

Imagine you have a source array and you want to copy a sub-section of it into the middle of a destination array:

Here is how you write that exact operation in code:

Java
import java.util.Arrays;

public class ArrayCopyExample {
    public static void main(String[] args) {
        int[] source = {10, 20, 30, 40, 50};
        int[] dest   = {0,  0,  0,  0,  0,  0,  0};

        // Copy 3 elements starting from index 1 of 'source' (20, 30, 40)
        // Paste them into 'dest' starting at index 2
        System.arraycopy(source, 1, dest, 2, 3);

        // Output: [0, 0, 20, 30, 40, 0, 0]
        System.out.println(Arrays.toString(dest));
    }
}

3 Common Use Cases

1. Merging / Appending Arrays

If you want to combine two arrays into a bigger one, you use System.arraycopy twice:

Java
int[] first = {1, 2, 3};
int[] second = {4, 5, 6};
int[] combined = new int[first.length + second.length];

// Copy the first array to the beginning
System.arraycopy(first, 0, combined, 0, first.length);

// Copy the second array right after the first one
System.arraycopy(second, 0, combined, first.length, second.length);
// combined is now: [1, 2, 3, 4, 5, 6]

2. Deleting an Element (Shifting Left)

You can use System.arraycopy on the same array to shift elements over and fill a gap. This is exactly how ArrayList.remove() works internally.

Java
int[] numbers = {10, 20, 30, 40, 50};
int removeIndex = 1; // We want to delete '20'

// Shift everything after '20' one position to the left
System.arraycopy(numbers, removeIndex + 1, numbers, removeIndex, numbers.length - removeIndex - 1);

// numbers becomes: [10, 30, 40, 50, 50] (You'd truncate or resize this later)

3. Cloning / Making a Copy

While Arrays.copyOf() is often preferred for simple clones because it creates the new array for you, System.arraycopy() can achieve the same thing if your destination array already exists:

Java
int[] original = {1, 2, 3};
int[] clone = new int[original.length];

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

Crucial Rules to Keep in Mind

  • IndexOutOfBoundsException: If your srcPos + length exceeds the source array's bounds, or destPos + length exceeds the destination array's bounds, the code will crash instantly.

  • ArrayStoreException: The source and destination arrays must be of compatible types. You cannot copy an int[] into a String[] array.

  • Shallow Copying: If you are copying arrays of Objects (like a custom User[] array), System.arraycopy copies the references to those objects, not the actual object data itself. Modifying an object inside the new array will reflect in the old array



Comments

Popular posts from this blog

Agentic AI Course : Week 1

LLM Engineering course : Day 1

LLM Engineering : Week 2