Issue
Good evening guys! I'm doing the course homework and the teacher gave us an exercise in which we need to sort the Ranking. He orders the first 5 positions, we are in this impasse almost a week :(
Below follows the code:
for (int i = 1; i < valores.size (); i++) {
for (int k = i + 1; k < valores.size () - 1; k++) {
if ((valores.get ( i )).compareTo ( valores.get ( k ) ) > 0) {
Double temp1 = valores.get ( i );
valores.set ( i, valores.get ( k ) );
valores.set ( k, temp1 );
Ouvinte temp = listaSorteio.get ( i );
listaSorteio.set ( i, listaSorteio.get ( k ) );
listaSorteio.set ( k, temp );
}
}
System.out.println ( (i) + " - Valores: " + valores.get ( i ) + "\t\tPessoa: " + listaSorteio.get ( i ));
}
Solution
Why do you do
for (int i = 1; i < valores.size (); i++)
? The error come from that
Always remember that in an array the indice begin by 0 and finish by the length of the array-1...
for (int i = 0; i < valores.size (); i++) {}
Answered By - Yacine
Answer Checked By - Katrina (JavaFixing Volunteer)