System.arraycopy() Method in Java with Examples

System class in Java is a part of the lang package and comes with many different fields and methods, and System.arraycopy() is among the 28 methods. It copies the source array from a specific index value and pastes it into another array of either the same length or different lengths, it doesn’t matter and copies it till a certain length which is also up to us to decide. Given an array of a fixed length. The task is to add a new element at a specific index of the array.
--> java.lang Package
--> System Class
--> arraycopy() Method
Syntax:Â
public static void arraycopy(Object Source, int sourceStartIndex,
Object Destination, int DestinationStartIndex, int size)
Parameters:Â
- Source: This is the array from which elements are to be copied.
- sourceStartIndex: This is the source start position.
- Destination: This is the array to which elements are supposed to be copied.
- DestinationStartIndex: This is the destination start position.
- size: The number of elements to be copied.
IllustrationÂ
Input: arr[] = { 1, 2, 3, 4, 5 }, index = 2, element = 6
Output: arr[] = { 1, 2, 6, 3, 4, 5 }
Input: arr[] = { 4, 5, 9, 8, 1 }, index = 3, element = 3
Output: arr[] = { 4, 5, 3)
Approach:
- Get the array and the index – p
- Create a new array of size one more than the size of the original array.
- Copy the elements from starting till index from the original array to the other array using System.arraycopy().
- At p, add the new element in the new Array which has the size of 1 more than the actual array
- Copy the elements from p of the original array and paste them from index p+1 in the new array till the end using System.arraycopy().
- Print the new array
Below is the implementation of the above approach.
Java
// Java Program to Illustrate arraycopy() Method// of System classÂ
// Importing required classesimport java.lang.System;import java.util.Arrays;import java.util.Scanner;Â
// Classpublic class GFG {Â
    // Main driver method    public static void main(String[] args)    {        // Original Array        int[] A = { 1, 2, 3, 4, 5, 6 };        System.out.print("Original Array : ");        System.out.print(Arrays.toString(A));Â
        // size of the index        int n = A.length;Â
        // Index at which the element be added        int p = 2;Â
        // element that needs to be added        int x = 10;Â
        // new Array of size +1        // of the original array        int[] B = new int[n + 1];Â
        // Copy the elements from starting till index        // from original array to the other array        System.arraycopy(A, 0, B, 0, p);Â
        // Copy the elements from p till        // end from original array        // and paste it into the new array on p+1 index        System.arraycopy(A, p, B, p + 1, n - p);Â
        // putting the element x on the        // index p of the new array        B[p] = x;Â
        // Printing the new Array        System.out.println();        System.out.print("New Array : "                         + Arrays.toString(B));    }} |
Output
Original Array : [1, 2, 3, 4, 5, 6] New Array : [1, 2, 10, 3, 4, 5, 6]



