Add Null/Empty check for param in average method (#4185)

This commit is contained in:
Anirudh Pathak 2023-05-10 17:30:41 +01:00 committed by GitHub
parent 122f5e5556
commit de2696d0c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;