mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Fix empty input handling in FindMax (#4206)
This commit is contained in:
@ -24,16 +24,20 @@ public class FindMax {
|
||||
}
|
||||
|
||||
/**
|
||||
* find max of array
|
||||
* @brief finds the maximum value stored in the input array
|
||||
*
|
||||
* @param array the array contains element
|
||||
* @return max value of given array
|
||||
* @param array the input array
|
||||
* @exception IllegalArgumentException input array is empty
|
||||
* @return the maximum value stored in the input array
|
||||
*/
|
||||
public static int findMax(int[] array) {
|
||||
int max = array[0];
|
||||
for (int i = 1; i < array.length; ++i) {
|
||||
if (array[i] > max) {
|
||||
max = array[i];
|
||||
if (array.length == 0) {
|
||||
throw new IllegalArgumentException("array must be non-empty.");
|
||||
}
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (final var value : array) {
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
|
Reference in New Issue
Block a user