-1
My problem is in the second if, what I was actually trying to do was to determine that at the time the loop was passing through position 11 of the array it would add the " n", but the way I did it, it does it in all loops, not just in the position I want. I understand my mistake, but I don’t know how to do it properly.
package gerarOrganizar;
import java.util.*;
//import java.util.Arrays;
//import java.util.Collections;
//import java.util.Random;
import static javax.swing.JOptionPane.*;
public class GerarOrganizar {
public static void main(String[] args) {
Integer[] array = new Integer[20];
Random gerador=new Random();
String aux="";
for(int i=0;i<20;i++){
array[i]=gerador.nextInt(50);
if(array[i]==0)//descarta números = 0
i--;
}//for de geração de valores
aux=String.format("Antes \n");
for(int i:array){//percorre todo o array retornando o valor para i
if(array[11]!=null)//este aqui é o problema
aux=String.format(aux+"\n");
aux=String.format(aux+"\t %d",i);
}//fim do for
showMessageDialog(null,aux);//imprime a ordem gerada
Arrays.sort(array,Collections.reverseOrder());
/*sort decide a ordem númerica em ordem decrescente devido ao Collections.reverseOrder*/
aux=String.format("Depois");
for(int i:array){/*percorre array retornando os valores para i*/
aux=String.format(aux+"\n %d",i);/*copia os valor do array na string*/
}//fim do for
showMessageDialog(null,aux);//imprime a ordem
}//main
}//class
Hi, Adrian, next time enter the line with a comment
// <----------- LINHA 22
. Putting the numbering here makes the copy/Paste for testing complicated.– brasofilo