Java System arraycopy() example

In this guide, you will learn about the System arraycopy() method in Java programming and how to use it with an example.

1. System arraycopy() Method Overview

Definition:

The arraycopy() method is a native method provided by the Java System class to efficiently copy data from one array to another.

Syntax:

public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

Parameters:

Object src: The source array.

int srcPos: The starting position in the source array.

Object dest: The destination array.

int destPos: The starting position in the destination data.

int length: The number of array elements to be copied.

Key Points:

- It provides a faster way to copy array content compared to manual looping since it's a native method.

- This method throws IndexOutOfBoundsException if copying would cause accessing of data outside array bounds.

- It throws ArrayStoreException if an element in the src array cannot be stored in the dest array due to a type mismatch.

- It throws NullPointerException if either src or dest is null.

- The source and destination arrays can be the same array, in which case the copying is performed as if the data is first copied into a temporary array and then into the destination array.

2. System arraycopy() Method Example 1

public class ArrayCopyExample {
    public static void main(String[] args) {
        int[] sourceArray = {10, 20, 30, 40, 50};
        int[] destinationArray = new int[10];

        System.arraycopy(sourceArray, 1, destinationArray, 2, 3);

        for(int value : destinationArray) {
            System.out.print(value + " ");
        }
    }
}

Output:

0 0 20 30 40 0 0 0 0 0

Explanation:

In the above example, we have a source array sourceArray with 5 elements. We intend to copy elements from index 1 (inclusive) to index 4 (exclusive) of sourceArray into the destinationArray starting at index 2. The output displays the destinationArray after the copy operation. As you can see, values 20, 30, and 40 have been copied from the sourceArray to positions 2, 3, and 4 of the destinationArray respectively.

3. System arraycopy() Method Example 2

public class ArrayCopyExample {

    public static void main(String[] args) {

        // Convert the "Java Guides" string to char array
        char[] srcArray = "Java Guides".toCharArray();

        // Create a destination array with a length of 4 characters
        char[] destArray = new char[4];

        // Use the arraycopy() method to copy the word "Java" from the source to destination
        System.arraycopy(srcArray, 0, destArray, 0, 4);

        // Display the contents of the destination array
        System.out.println(new String(destArray));  // Expected Output: Java
    }
}

Output:

Java

Explanation:

In the given example, we copied the first 4 characters ("Java") from the srcArray (which represents the "Java Guides" string) into destArray. The output displays the word "Java" as expected.

Comments