BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to copy all elements of one array into another array

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to copy all the elements of an array to another array. This can be easily done by using any loop such as for, while or do-while loop. We just need to run a loop from 0 to the array length (size of an array) and at every iteration, read the element of the given array and write the same value in another array.

For example:
array1 is {3, 5, 7, 9}
array2[] is {}
Then after copying all the elements of array1[] to array2[]
the elements in array2[] will be = {3, 5, 7, 9}

Steps to copy all elements from one array to another array

  1. Initialize the first array.
  2. Create another array with the same size as of the first array
  3. Run a loop from 0 till the length of first array
    • Read the element of first array
    • Copy the element to the second array
  4. Repeat the step 3 until the complete array is traversed
  5. Run a loop from 0 to the length of any array
    • Read and print the elements of second array
  6. Repeat the step 5 until the second array is completely traversed.
  7. End of Program.

Java Program to copy all elements of one array into another array

public class JavaExample {
  public static void main(String[] args) {
    //Initializing an array
    int [] firstArray = new int [] {3, 5, 7, 9, 11};
    /* Creating another array secondArray with same size
     * of first array using firstArray.length as it returns
     * the size of array firstArray.
     */
    int secondArray[] = new int[firstArray.length];

    //Displaying elements of first array
    System.out.println("Elements of First array: ");
    for (int i = 0; i < firstArray.length; i++) {
      System.out.print(firstArray[i] + " ");
    }

    //Copying all elements of firstArray to secondArray
    for (int i = 0; i < firstArray.length; i++) {
      secondArray[i] = firstArray[i];
    }

    //Displaying elements of secondArray
    System.out.println();
    System.out.println("Elements of Copied array: ");
    for (int i = 0; i < secondArray.length; i++) {
      System.out.print(secondArray[i] + " ");
    }
  }
}    

Output:

Elements of First array: 
3 5 7 9 11 
Elements of Copied array: 
3 5 7 9 11 

Related Java Programs

  1. Java Program to print the elements of an array present on odd position
  2. Java Program to print the elements of an array present on even position
  3. How to sort an array in Java
  4. How to reverse an array in Java
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap