Home Java Programming Language / Programs / Program to copy all elements of one array into another array | Java
🚀 Programming Example

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

👁 636 Views
💻 Practical Program
📘 Step Learning
In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.

💻 Program Code

public class CopyArray {    
    public static void main(String[] args) {        
             //Initialize array     
        int [] arr1 = new int [] {1, 2, 3, 4, 5};     
         //Create another array arr2 with size of arr1    
        int arr2[] = new int[arr1.length];    
        //Copying all elements of one array into another    
        for (int i = 0; i < arr1.length; i++) {     
            arr2[i] = arr1[i];     
        }      
         //Displaying elements of array 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 array arr2     
        System.out.println("Elements of new array: ");    
        for (int i = 0; i < arr2.length; i++) {     
           System.out.print(arr2[i] + " ");    
        }     
    }    
}    
                        

🖥 Program Output

 Elements of original array
1 2 3 4 5
Elements of new array:
1 2 3 4 5
                            

📘 Explanation

Algorithm

  • STEP 1:?START
  • STEP 2:?INITIALIZE arr1[] ={1, 2, 3, 4, 5}
  • STEP 3:?CREATE arr2[] of size arr1[].
  • STEP 4:?COPY elements of arr1[] to arr2[]
  • STEP 5:?REPEAT STEP 6 UNTIL (i
  • STEP 6:?arr2[i] =arr1[i]
  • STEP 7:?DISPLAY elements of arr1[].
  • STEP 8:?REPEAT STEP 9 UNTIL (i
  • STEP 9:?PRINT arr1[i]
  • STEP 10:?DISPLAY elements of arr2[].
  • STEP 11:?REPEAT STEP 12 UNTIL (i
  • STEP 12:?PRINT arr2[i].
  • STEP 13:?END
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.