System.arraycopy() in Java

System.arraycopy(): This method is available in java.lang.System class of Java.

Syntax:

void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

This method five arguments two of type Object and the rest three are of type int as its parameters. This method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components is copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

Parameters: Five parameters are required for this method.

src: the source array.

srcPos: starting position in the source array.

dest: the destination array.

destPos: starting position in the destination data.

length: the number of array elements to be copied.

Throws:

1. IndexOutOfBoundsException - if copying would cause access of data outside array bounds.

2. ArrayStoreException - if an element in the src array could not be stored into the dest array because of a type mismatch.

3. NullPointerException - if either src or dest is null.

Approach

Java

import java.util.Arrays;

public class Systemarraycopy {
    public static void main(String[] args) {

        Object src[] = { 1234 };

        int srcPos = 0;
        Object dest[] = { 10203050 };

        System.out.println("Dest before copy " + Arrays.toString(dest));
        int destPos = 0, length = 3;
        System.arraycopy(src, srcPos, dest, destPos, length);

        System.out.println("Dest After copy " + Arrays.toString(dest));
    }
}

Output:

Dest before copy [10, 20, 30, 50]

Dest After copy [1, 2, 3, 50]


Approach 2: IndexOutOfBoundsException 

Java

import java.util.Arrays;

public class Systemarraycopy {
    public static void main(String[] args) {

        Object src[] = { 1234 };

        int srcPos = 0;
        Object dest[] = { 10203050 };

        System.out.println("Dest before copy " + Arrays.toString(dest));
        int destPos = 2, length = 3;
        System.arraycopy(src, srcPos, dest, destPos, length);

        System.out.println("Dest After copy " + Arrays.toString(dest));
    }
}


Output:

Dest before copy [10, 20, 30, 50] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 5 out of bounds for object array[4] at java.base/java.lang.System.arraycopy(Native Method)


Approach 3: ArrayStoreException 

Java

import java.util.Arrays;

public class Systemarraycopy {
    public static void main(String[] args) {

        Object src[] = { 1234 };

        int srcPos = 0;
        char dest[] = { '1''2''3''4' };

        System.out.println("Dest before copy " + Arrays.toString(dest));
        int destPos = 0, length = 3;
        System.arraycopy(src, srcPos, dest, destPos, length);

        System.out.println("Dest After copy " + Arrays.toString(dest));
    }
}

Output:

Dest before copy [1, 2, 3, 4] Exception in thread "main" java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy object array[] into char[] at java.base/java.lang.System.arraycopy(Native Method)


Approach 4: NullPointerException 

Java

import java.util.Arrays;

public class Systemarraycopy {
    public static void main(String[] args) {

        Object src[] = { 1234 };

        int srcPos = 0;
        Object dest[] = null;

        System.out.println("Dest before copy " + Arrays.toString(dest));
        int destPos = 0, length = 3;
        System.arraycopy(src, srcPos, dest, destPos, length);

        System.out.println("Dest After copy " + Arrays.toString(dest));
    }
}


Output:

Dest before copy null Exception in thread "main" java.lang.NullPointerException at java.base/java.lang.System.arraycopy(Native Method)



No comments:

Post a Comment