System.arraycopy() Method in Java with Examples
Last Updated :
24 May, 2022
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 classes
import java.lang.System;
import java.util.Arrays;
import java.util.Scanner;
// Class
public 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));
}
}
OutputOriginal Array : [1, 2, 3, 4, 5, 6]
New Array : [1, 2, 10, 3, 4, 5, 6]
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read