Java

arraycopy Method of System Class in Java

Consider the following two arrays:

    P = {'A', 'B', 'C', 'D', 'E'}

and

    Q = {'F', 'G', 'H', 'I', 'J'}

If the sub-array from index 2 to the end of array P is copied to array Q, beginning from index, 1, then the new array Q would be:

    {'F', 'C', 'D', 'E', 'J'}

The elements ‘G’, ‘H’ and ‘I’ of array Q have been overwritten. Remember that the length of an array can neither be increased nor decreased after definition. So, overwriting has to take place under such copying. The aim here is not to insert. The aim is to copy. The array is not a normal data structure to allow insertion. With insertion for data structures, all the elements on the right are shifted to the right in many places. Do not forget that when an array in Java does not have practical values, it has default values. In other words, a non-zero length empty array has default values.

The System class of the java.lang.* package has the arraycopy() method. The purpose of this method is to copy a portion of one array into another array. A portion can be the whole array!

Syntax

Before considering the syntax of the method, it is good to consider a program with the use of the arraycopy() method, for the above array copying example. The program is:

    public class TheClass {
        public static void main(String[] args) {
            char[] P = {'A', 'B', 'C', 'D', 'E'};
            char[] Q = {'F', 'G', 'H', 'I', 'J'};
            System.arraycopy(P, 2, Q, 1, 3);
            System.out.println(Q);
        }
    }

The output is FCDEJ, better written as, F C D E J. The output is as expected, though not well-formatted. Notice that the java.lang.* package has not been imported. The java.lang.* package does not have to be imported by the programmer for any class, such as System, that it has. The syntax is:

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

The method is static. This means that the System class does not have to be instantiated to use the method. The first argument is the array from which elements are to be copied from. It is called the source, and in the above case, it is array P. The second argument is the start index for copying from the source. In the above case, it is 2. The third argument is the array to which elements are to be copied. It is called the destination, and in the above case, it is array Q. The fourth argument is the start index for copy receiving by the destination array. In the above case, it is 1. The fifth argument is the number of elements to be copied from the source array, which is the same as the number of elements to be replaced in the destination array. The counting of this number begins from the index.

Note: the lengths of the source and destination arrays do not have to be the same.

Incompatibility

Once the implementations of the two arrays and the arraycopy() statements are correct, the program will compile, everything being equal. If there is any incompatibility, the program will not run, and the corresponding exception will be thrown.

Two Data not of the same Type

In English, the plural of datum is data. If one of the arrays has chars and the other has bytes, the program will still compile, everything being equal. However, an ArrayStoreException will be issued at runtime, and the program will not execute. The exception error message may be:

    Exception in thread "main" java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy char[] into byte[]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Two Data not of the same Kind

If one of the arrays has chars and the other has Characters, the program will still compile, everything being equal. However, an ArrayStoreException will be issued at runtime, and the program will not execute. The exception error message may be:

    Exception in thread "main" java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy char[] into object array[]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Destination Array too short

If the length (fifth argument) goes beyond the destination array, an ArrayIndexOutOfBoundsException would be thrown at runtime, e.g.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 4 out of bounds for char[3]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Source Array too short

If the length (fifth argument) goes beyond the source array, an ArrayIndexOutOfBoundsException would be thrown at runtime, e.g.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 6 out of bounds for char[5]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Destination Array is null

The destination array can be null. For example,

            char[] Q = null;

In this case, a NullPointerException will be thrown by the System.arraycopy() method; e.g.

    Exception in thread "main" java.lang.NullPointerException
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Note,

            char[] Q = {};

does not mean that Q is null. It means that Q is empty. With this, it is an ArrayIndexOutOfBoundsException that will be thrown; e.g.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 4 out of bounds for char[0]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Source Array is null

The source array can be null. For example,

            char[] P = null;

In this case, a NullPointerException will be thrown by the System.arraycopy() method; e.g.

    Exception in thread "main" java.lang.NullPointerException
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Note,

            char[] P = {};

does not mean that P is null. It means that P is empty. With this, it is an ArrayIndexOutOfBoundsException that will be thrown; e.g.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 5 out of bounds for char[0]
    at java.base/java.lang.System.arraycopy(Native Method)
    at TheClass.main(TheClass.java:10)

Conclusion

The System class of the java.lang.* package, has the arraycopy() method. The purpose of this method is to copy a portion of one array into another array. A portion can be the whole array! This is a static method, so the System class does not have to be instantiated for the method to be used. The method returns void. The syntax for the method is:

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

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.