Reverse

Asked

Viewed 413 times

1

 package pag1;

   import java.util.Arrays;
   import java.util.Scanner;

   public class ex3 {
   public static void main (String[] args){
    Scanner x = new Scanner (System.in);

    int posicao = 0;
    int[] numeros = new int [5];
    System.out.println("Digite 5 números: ");

    while (posicao < numeros.length){
        numeros [posicao] = x.nextInt();
        posicao++;          
    }
    System.out.println(Arrays.toString(numeros));
    Arrays.sort(numeros, 0, numeros.length);
    System.out.print("[" + numeros[4] + ", " + numeros[3] + ", " + numeros[2] + ", " + numeros[1] + ", " + numeros[0] + "]");

  }

}

How can I show the items of the reverted array without using this gambiarra I did at the end?

  • Related: https://answall.com/q/217632/64969

  • If the numbers can come in arbitrary order, call the sort won’t help you

  • Actually, they’re very similar, I hadn’t seen his point. But I wanted to know if there is a command to reverse the positions of the array, if it reads for example from 0 to 4, if it would have some command to read from 4 to 0.

  • 1

    a simple way that comes to mind is to put at the end something like, System.out.print("[" + numbers[4] + ", " + numbers[3] + ", " + numbers[2] + ", " + numbers[1] + ", " + numbers[0] + "]); but it seems an ugly gambiarra to me.

  • my answer follows the idea of this "gambiarra", only that generalizing and making the exchange ;-)

2 answers

2


To swap two integer numbers of variables, we can do the following:

int a = 2;
int b = 4;

int swapAux = a;
a = b;
b = swapAux;

We can use the same logic to swap integers at vector positions. Let i a position in a vector of n elements, indexed by 0, the element opposite to i is n - i - 1.

Make the check for the i as the first element i = 0 and as the last element i = n - 1 if not convinced of the formula

To ensure that we exchange the elements only once for position, we can iterate only halfway. Therefore, the inversion of a vector can be done thus:

public void inverteVetor(int[] vet) {
    for (int i = 0; i < vet.length / 2; i++) {
        int reverso = vet.length - i - 1;

        int swapAux = vet[i];
        vet[i] = vet[reverso];
        vet[reverso] = swapAux;
    }
}

2

Using the new Java 8 and Ambdas stream features just do:

int[] numerosInvertidos = IntStream.range(0, numeros.length)
                                   .map(i->numeros[numeros.length-i-1]).toArray();

System.out.println(Arrays.toString(numerosInvertidos)); //mostrar o array todo

In which the map maps each value to its inverse through the array positions. From 0 to the 4, 1 to the 3, and so on.

  • 1

    I have the impression that NEVER I’ll get used to this functional paradigm in Java. It’s beautiful and strange all at the same time

  • 1

    It’s a matter of habit. What is certain is that it helps to have much shorter code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.