I'm new to Java, this is my first week and I don't have any homework to do, but I want to practice the language.
I need to get the 5 largest numbers from an array, and store them in another one.
After searching in the forum, I found the Arrays.sort()
method and I'm really surprised about Java. But my code isn't right. I can't find out why... Could you please help me?
package week1;
import java.util.Arrays;
public class Week1 {
public static void main(String[] args) {
int maxValues[] = new int [5];
int array[] = new int [50];
for (int i = 0; i <array.length; i++) {
int x = (int) ( Math.random() * 100 );
array[i]=x;
System.out.println("The Number is: " + array[i]);
}
Arrays.sort(array);
for (int i = 0; i < maxValues.length; i++) {
for (int j =array.length; j > (array.length)-5; j--) {
maxValues[i]=array[j];
}
System.out.println( (i+1) +"Max is: " + maxValues[i] );
}
}
}