I'm trying to figure how to find the largest number in an array of random numbers.
So far I cant manage to get it right.
Its an array[50]
and the random, numbers are between 0-100
.
Thanks!
I'm trying to figure how to find the largest number in an array of random numbers.
So far I cant manage to get it right.
Its an array[50]
and the random, numbers are between 0-100
.
Thanks!
Loop through the array and keep track of the largest number found already in an int variable.
public int findMax(int[] numbers)
{
int max = 0;
for (int i = 0; i < numbers.length; ++i)
if (numbers[i] > max) max = numbers[i];
return max;
}
(You can also initialize max
to int.MIN_VALUE
or something if it helps behave more suitably in the case where an empty array is passed in.)
Use:
java.utils.Arrays.sort(yours_array);
int largest = yours_array[yours_array.length - 1] ;
Collections.max and you can use it as follows with a raw array:
List<Integer> triedArray = new ArrayList<Integer>();
ArrayUtils.addAll(intArray);
This does add a dependency on commons-lang, though.
Assuming the int[]
is named array
Try to use a loop like the following:
int biggest = array[0]
for(int a = 1; a < array.length; a++){
if(array[a] > biggest){
biggest = array[a]
}
}
At the end, your variable biggest will hold the largest value in the array.