There is an array of numbers [1,4, -2,3].
How do I find the minimum value in an array ??
Answer 1, authority 100%
In any way, like this:
var nums = new int [] {1, 4, -2, 3};
var min = Arrays.stream (nums) .min ();
System.out.println (min.isPresent ()? Min.getAsInt (): "empty array");
or like this:
var nums = new int [] {1, 4, -2, 3};
var min = Collections.min (Arrays.stream (nums) .boxed (). collect (Collectors.toList ()));
System.out.println (min);
or like this:
var nums = new int [] {1, 4, -2, 3};
var min = nums [0];
for (int num: nums) {
if (num & lt; min) {
min = num;
}
}
System.out.println (min);
Answer 2
I would do it in a more understandable way:
int min = a [0] + 1;
for (int i = 0; i & lt; mass.length; i ++) {
if (a [i] & lt; min) {
min = a [i];
}
System.out.println (min);
}
We go through the array, each element is compared with the smallest one.
Answer 3
Another method.
int [] mass = {1,2,3-1};
int [] mass2 = mass.clone ();
Arrays.sort (mass2);
System.out.println (mass2 [0]);
It is possible without cloning, if the original array is not needed