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
– Jefferson Quesado
If the numbers can come in arbitrary order, call the
sort
won’t help you– Jefferson Quesado
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.
– João Laurent
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.
– João Laurent
my answer follows the idea of this "gambiarra", only that generalizing and making the exchange ;-)
– Jefferson Quesado