How To Get The Minimum,maximum Value Of An Array?
Here's my code. I need to get the minimum,maximum value of my array to be able for me get the range, whenever I input numbers the minimum value is 0. Please help me. Thank you:) fi
Solution 1:
Use Collections
with your code using it you can find minimum and maximum .
following is the example code for that:
List<Integer> list = Arrays.asList(100,2,3,4,5,6,7,67,2,32);
intmin = Collections.min(list);
intmax = Collections.max(list);
System.out.println(min);
System.out.println(max);
Output:
2
100
Solution 2:
int[] convertedValues = new int[10];
intmax = convertedValues[0];
for (int i = 1; i < convertedValues.length; i++) {
if (convertedValues[i] > max) {
max = convertedValues[i];
}
}
Similarly find for the minimum value by changing lesser symbol.
Solution 3:
int minIndex = list.indexOf(Collections.min(list));
or
publicclassMinMaxValue {
publicstaticvoidmain(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};
List b = Arrays.asList(ArrayUtils.toObject(a));
System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}
Solution 4:
You could sort the array and get the positions 0
and length-1
:
Arrays.sort(convertedValues);
intmin = convertedValues[0];
intmax = convertedValues[convertedValues.length - 1];
Sorts the specified array of ints into ascending numerical order.
So, after sorting, the first element is the minimum, the last is the maximum.
Solution 5:
1. Sort the array.
2. Minimum is the First element.
3. Maximum is the last element of the array.
Post a Comment for "How To Get The Minimum,maximum Value Of An Array?"