- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to copy an array from the specified source array
Use arraycopy() method in Java to copy an array from the specified source array.
Here, we have two arrays −
int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30};
Now, we will use the arraycopy() method to copy the first two elements of the 1st array to the 2nd array −
System.arraycopy(arr1, 0, arr2, 2, 2);
The following is an example −
Example
import java.lang.*; public class Demo { public static void main(String[] args) { int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30}; System.arraycopy(arr1, 0, arr2, 2, 2); System.out.print("New Array = "); System.out.print(arr2[0] + " "); System.out.print(arr2[1] + " "); System.out.print(arr2[2] + " "); System.out.print(arr2[3] + " "); } }
Output
New Array = 3 7 10 20
- Related Articles
- Copy StringDictionary to Array at the specified index in C#
- C# Program to return specified number of elements from the end of an array
- Copy ListDictionary to Array instance at the specified index in C#
- Copy StringCollection at the specified index of array in C#
- Array Copy in Java
- C++ Program to get the subarray from an array using a specified range of indices
- Golang Program to get the subarray from an array using a specified range of indices
- Swift Program to get the subarray from an array using a specified range of indices
- Java Program to Remove Duplicates from an Array List
- Copy OrderedDictionary elements to Array instance at the specified index in C#
- Can form target array from source array JavaScript
- Copy all elements of Java HashSet to an Object Array
- Copy all elements of Java LinkedHashSet to an Object Array
- Copy all elements in Java TreeSet to an Object Array
- Copy characters from string into char Array in Java

Advertisements