EN
Java - min value in array
3 points
This article will show you how to find the minimum value in an array in Java.
Quick solution:
xxxxxxxxxx
1
Integer[] numbersArray = { 1, 5, 2, 16, -5};
2
3
int min = Collections.min( Arrays.asList(numbersArray) );
4
System.out.println( min ); // -5
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
Integer[] numbersArray = { 1, 5, 2, 16, -5};
5
6
int min = Collections.min( Arrays.asList(numbersArray) );
7
System.out.println( min ); // -5
8
}
9
}
xxxxxxxxxx
1
public class MathExample {
2
3
static double min(double[] array) {
4
double result = Double.POSITIVE_INFINITY;
5
for (double currentNumber : array) {
6
if (result > currentNumber) result = currentNumber;
7
}
8
return result;
9
}
10
11
public static void main(String[] args) {
12
double[] numbersArray = {1.4, 5.0, 2.2, 16.0, -5.0};
13
14
System.out.println( min(numbersArray) ); // -5.0
15
}
16
}