![]() |
VOOZH | about |
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:
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)Below is the implementation of the above approach.
Original Array : [1, 2, 3, 4, 5, 6] New Array : [1, 2, 10, 3, 4, 5, 6]