Swap the array data and display it in reverse. JAVA

1

I have this code that at first asks me for 3 numbers that are saved in an array, what I want to do is copy the contents of that array and print it upside down.

EJ. You capture 1, 2, 3 and what you should see first the first arrangement with the capture: [1, 2, 3] and then inverse: [3, 2, 1]

It works fine, the problem is when I want to print the arrangement backwards. If you do it in intercambiar() with the for , I save it in the 2nd array with nums2[i]=nums1[i]

In fact, if you look at System.out.println(nums2[i]); to see if I made the investment correctly. And yes.

When I run the program, I capture the data, print the FIRST fix (returning to the example) [1, 2, 3] and then the SECOND but exactly the same and in println if you print it in the console as: 3 two 1

Here the code:

import javax.swing.JOptionPane;

public class Arreglo7
{
private int numeros1[]; // = new int[3];
private int numeros2[];

public Arreglo7()
{
//  numeros = new int[3];
//  numeros = new int[] {40,20,50};
numeros1 = new int[3]; 

numeros2 = new int[3];

}

private void obtenerDatos( int nums[])
{
    for(int i =0; i<nums.length;i++)
    {
        nums[i]= Integer.parseInt(JOptionPane.showInputDialog("Numeros["+i+"] =")); 
    }
}

private void desplegarDatos(int numbers[])
{
    String str="[";
    int i=0;

    for(i=0;i<numbers.length-1;i++)
        str = str + numbers[i] + ",";       
        str = str +numbers[i] + "]";
        JOptionPane.showMessageDialog(null,str);        
}

private void intercambiar(int nums1[], int nums2[])
{
//  for(int i=0;i<nums1.length;i++)
    for(int i=nums1.length-1;i>=0;i--)
    {   
        nums2[i]=nums1[i];
        System.out.println(nums2[i]);               
    }


}

private void principal()
{
    obtenerDatos(numeros1);
    desplegarDatos(numeros1);   
    intercambiar(numeros1, numeros2);   
    desplegarDatos(numeros2);

}

public static void main(String args[])
{
    Arreglo7 objeto = new Arreglo7();
//  int numeros[] = new int[3];     
    objeto.principal();
}

}
    
asked by Cruzhn 13.08.2018 в 03:05
source

2 answers

1

Your problem is logic ..

You are copying an array exactly like the one you had, because even if you go backwards, when you do nums2[i]=nums1[i]; the positions where each item is going to stop are the same.

Change your function by:

private void intercambiar(int nums1[], int nums2[])
{
    int max = nums1.length-1;
    for(int i=nums1.length-1;i>=0;i--)
    {   
        nums2[max-i]=nums1[i];  
    }
}

In this way, you go through the array nums1 to the back, but copy to the reverse position you're looking for when you go to the array nums2.

    
answered by 13.08.2018 / 04:00
source
0

Your error is in the exchange function

nums2[i]=nums1[i];

You must change that for;

nums2[]+=nums1[i];

Because you are adding the value in the wrong place.

For example with the code so if in the array num1 you have the data [1,2,3] and you say that the data of the position 2 you enter it in the position 2 of the array num2 would be placed in the same place num2=[null,null,3] in the other round of the cycle will be added the data found in the array num1 in position 1 that is (2) and will enter it in position 1 of the array num2 so it will remain num2=[null,2,3] and on the other return you would get the value num2=[1,2,3] that means it will not be inverted.

    
answered by 13.08.2018 в 04:04