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();
}
}