Add Null/Empty check for param in average method (#4185)
This commit is contained in:
parent
122f5e5556
commit
de2696d0c5
@ -12,6 +12,9 @@ public class Average {
|
||||
* @return mean of given numbers
|
||||
*/
|
||||
public static double average(double[] numbers) {
|
||||
if (numbers == null || numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Numbers array cannot be empty or null");
|
||||
}
|
||||
double sum = 0;
|
||||
for (double number : numbers) {
|
||||
sum += number;
|
||||
@ -27,6 +30,9 @@ public class Average {
|
||||
* @return average value
|
||||
*/
|
||||
public static int average(int[] numbers) {
|
||||
if (numbers == null || numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Numbers array cannot be empty or null");
|
||||
}
|
||||
long sum = 0;
|
||||
for (int number : numbers) {
|
||||
sum += number;
|
||||
|
Loading…
Reference in New Issue
Block a user