Ticker

6/recent/ticker-posts

Header Ads Widget

Program to copy one array to another are

Program to copy one array into another






Introduction
In this tutorial we will learn how to copy one array into another in this we have array which is predefined by us



Let's see the logic


You firstly create or initialise a r a and then copy the all element into another array called as B arrays . Where we use for loop for this we copy one array element to other using this loop for more understanding you can see the code below one by one copy one array to another by using the size of array.



After initialisation of array, we will display it on  and after that we perform for apply logic of for loop to copy a array into B and then give B array as output.


Q) Java program to copy one array into another.
Sol

public class Main {    
    public static void main(String[] args) {        
                
        int A[]= new int [] {10, 20, 30, 40, 50};     
         
        int B[] = new int[A.length];    
       
        for (int i = 0; i < A.length; i++) {     
            B[i] = A[i];     
        }      

        System.out.println("Elements of array A: ");    
        for (int i = 0; i < A.length; i++) {     
           System.out.print(A[i] + " ");    
        }     
            
        System.out.println();    
      
        System.out.println("Elements of array B are following");    
        for (int i = 0; i < B.length; i++) {     
           System.out.print(B[i] + " ");    
        }     
    }    
}    




Output:

Elements of array A:
10 20 30 40 50
Elements of array B are following
10 20 30 40 50




Post a Comment

0 Comments