- 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
How can we copy one array from another in Java
Let us first create a string array −
String[] arr = new String[] { "P","Q", "R", "S", "T"};
Now, calculate the length of the above array and create a new array with the same length −
int len = arr.length; String[] arr2 = new String[len];
Let us copy one array from another −
System.arraycopy(arr, 0, arr2, 0, arr.length);
Example
public class Demo { public static void main(String[] args) { String[] arr = new String[] { "P","Q", "R", "S", "T"}; System.out.println("Initial array..."); for (int i = 0; i < arr.length; i++) System.out.println(arr[i]); int len = arr.length; String[] arr2 = new String[len]; System.arraycopy(arr, 0, arr2, 0, arr.length); System.out.println("New array...copied"); for (int i = 0; i < arr2.length; i++) System.out.println(arr2[i]); } }
Output
Initial array... P Q R S T New array...copied P Q R S T
- Related Articles
- How we can copy Python modules from one system to another?
- Can we implement one interface from another in java?
- Copy values from one array to another in Numpy
- Can i refer an element of one array from another array in java?
- How do you copy an element from one list to another in Java?
- Copy all the elements from one set to another in Java
- Java Program to copy value from one list to another list
- How to write a program to copy characters from one file to another in Java?
- How to copy rows from one table to another in MySQL?
- How to copy a section of one Array to another in C#?
- How to move an array element from one array position to another in Java?
- C# program to copy a range of bytes from one array to another
- How can we call one constructor from another in the same class in C#?
- How to copy a collection from one database to another in MongoDB?
- How can I copy a file from one folder to another folder within a container in Docker?

Advertisements