-
-
Save sendelufa/5c50e55056b5b85f5e23a78314d237f0 to your computer and use it in GitHub Desktop.
Поиск минимального значение int в массиве int[]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FindMin { | |
public static void main(String[] args) { | |
System.out.println(findMin(new int[]{1, 2, 3})); // ожидается 1 | |
System.out.println(findMin(new int[]{-1, -2, -3})); // ожидается -3 | |
System.out.println(findMin(new int[]{5, 2, 34, 67})); // ожидается -2 | |
System.out.println(findMin(new int[0])); // ожидается null | |
System.out.println(findMin(new int[]{})); // ожидается null | |
} | |
private static Integer findMin(int[] ints) { | |
if (ints.length == 0) { | |
return null; | |
} | |
var min = ints[0]; | |
for (int i = 1; i < ints.length; i++) { | |
var current = ints[i]; | |
if (current < min) { | |
min = current; | |
} | |
} | |
return min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment