Search code examples
arraysnetbeansreversecopying

copying and reversing arrays?


1.The two copies should be done in a single for loop. 2.The output should be made in a separate loop.The output should display 5 lines for each number on each one of the arrays as follows. “ARRAY1[index]= xx ARRAY2[index] = XX ARRAY3[index] = XX” Array3 should contain the first arrays numbers but reversed (5-1).

public static void main(String[] args) 
 {
        // Constants Section
            final int FIVE = 5;                          
            final int ONE = 1;                          

            // Variable Declaration Section
            int[] firstArray = {1,2,3,4,5};             
            int[] secondArray;                                                          
            int[] finalArray;                                                       
            int i;                                       


// Variable Initialization Section
            secondArray = new int[FIVE];                
            finalArray = new int[FIVE];                 

// Code Section
              for (i = 0; i <FIVE; i++)       
            {  
                secondArray = firstArray;  
                finalArray = firstArray;
            }

            for (i = FIVE - 1; i >= 0; i--)   
            {

                System.out.println("Array1 = " + firstArray[i] + " Array2= " +   secondArray[i] + " Array3= " + finalArray [i]);
            }




   }
}

PLEASE HELP, IM A HIGH SCHOOL STUDENT WHO IS COMPLETELY CLUELESS ABOUT PROGRAMMING. (THE SIMPLER THE BETTER)


Solution

  • I think this is what you need!

    public static void main(String[] args) 
     {
            // Constants Section
                final int FIVE = 5;                          
                final int ONE = 1;                          
    
                // Variable Declaration Section
                int[] firstArray = {1,2,3,4,5};             
                int[] secondArray;                                                          
                int[] finalArray;                                                       
                int i;                                       
    
    
    // Variable Initialization Section
                secondArray = new int[FIVE];                
                finalArray = new int[FIVE];                 
    
    // Code Section
                  for (i = 0; i <FIVE; i++)       
                {  
                    secondArray[i] = firstArray[i];  
                    finalArray[i] = firstArray[FIVE-i-1];
                }
    //For Printing you can loose any logic. This would print in the reverse order but if you want your could change the loop
                for (i = FIVE - 1; i >= 0; i--)   
                {
    
                    System.out.println("Array1 = " + firstArray[i] + " Array2= " +   secondArray[i] + " Array3= " + finalArray [i]);
                }
    
    
    
    
       }
    }
    

    I think this should do it, Correct me if i misunderstood..