Java Program to copy elements of one array into another array
CopyArray.java
/* Code From Learnoset - Learn Coding Online.
Download Learnoset App from PlayStore to learn coding,
projects, algorithms, error handling
*/
// Java Program to copy elements of one array into another array
public class CopyArray {
public static void main(String[] args) {
//Initialize array
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8};
/* declare new array variable.
we are copying every element of 'arr1' so we used
'arr1.length' into 'arr2'*/
int[] arr2 = new int[arr1.length];
//Copying all elements of 'arr1' into 'arr2'
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
//Displaying elements of 'arr1'
System.out.println("Elements of original array: ");
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
//Displaying elements of 'arr2'
System.out.println("Elements of new array: ");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Simple Music Player App (Version 2) for Android (Offline Version)
Login and Register Activity Material UI design in Android Studio
Programming Quiz App for Android (Offline Version)
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.