0
In Java is there a way to bring out the highest number in an array?
I wanted to write a program in Java that can arrange numbers in descending and ascending order. How do I use arrays to do that or is there another way?
2 odpowiedzi
+ 5
If you are lazy:
import java.util.Arrays;
import java.util.Collections;
Arrays.sort(yourArray);
Collections.reverse(Arrays.asList(yourArray));
if not: learn a sorting algorithm. I think Bubble Sort is the easiest one.
you can write your own reverse function if you want
public int[] reverse(int[] arr){
int n = arr.length - 1;
int[] reverse = new int[arr.length];
for(int i = 0; i < reverse.length; i++){
reverse[i] = arr[n];
n--;
}
return reverse;
}