I got my main task to work (rough translation):
Write a method, which returns the smallest number from a 1d array of integers.
Now i have to do a side task (rough translation):
Add an if statement to check if the array is null or empty, and if it is, return 0.
method:
public static int smallestNumber (int[] array1) {
int smallest = array1[0];
if (array1 == null || array1.length == -1) {
return 0;
}
else {
for (int element : array1) {
if (element < smallest) {
smallest = element;
}
}
}
return smallest;
}
main:
public static void main(String[] args) {
int[] tab1 = {}; //empty array?
int smallestNumber = smallestNumber(tab1);
System.out.printf("The smallest number is: %d", smallestNumber);
}
The method works if i only check for null. But i'm confused why it wont work on empty array.
int[] tab1 = {};
EDIT: I also tried with array1.length == 0;
3 Answers 3
Firstly, Arrays are of non-negative size so array1.length
cannot be -1, instead make the comparison with 0
.
Secondly, the assignment int smallest = array1[0];
tries to access 0th position of an empty array which will result in java.lang.ArrayIndexOutOfBoundsException
.
So in conclusion move the assignment to smallest
in the else block and check the condition for empty or null array before you try to access any array value.
public static int smallestNumber (int[] array1) {
int smallest;
if (array1 == null || array1.length == 0) {
return 0;
}
else {
smallest = array1[0];
for (int element : array1) {
if (element < smallest) {
smallest = element;
}
}
}
return smallest;
}
-
1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 this is what i get when i do == 0Captain Apple– Captain Apple2019年03月31日 18:05:52 +00:00Commented Mar 31, 2019 at 18:05
-
move the assignment for smallest below the conditionjonathan Heindl– jonathan Heindl2019年03月31日 18:07:36 +00:00Commented Mar 31, 2019 at 18:07
-
@CaptainApple now try this code, the line where you assigned the initial value of
smallest
was incorrect move it inside theelse
block and also change the comparison to -1.Amardeep Bhowmick– Amardeep Bhowmick2019年03月31日 18:11:07 +00:00Commented Mar 31, 2019 at 18:11
Try checking for null first, as it is safer to check for that first. You also can use the method isEmpty() for the length.
public static int smallestNumber (int[] array1) {
if (array1 == null) {
return 0;
}
if (array1.isEmpty()) {
return 0;
}
int smallest = array1[0];
for (int element : array1) {
if (element < smallest) {
smallest = element;
}
}
return smallest;
}
or add this as an alt:
public static int smallestNumber (int[] array1) {
if (array1 != null) {
if (array1.isEmpty()) {
return 0;
}
int smallest = array1[0];
for (int element : array1) {
if (element < smallest) {
smallest = element;
}
}
return smallest;
} else {
return 0;
}
}
public static int smallestNumber (int[] array1) {
if (array1 == null || array1.length == 0) {
return 0;
}
int smallest = array1[0];
for (int element : array1) {
if (element < smallest) {
smallest = element;
}
}
return smallest;
}
-
Thank you, yes wasnt sure if it was true or not but i went with == 0 first before becoming desperate to even try -1. Issue solved aboveCaptain Apple– Captain Apple2019年03月31日 18:10:48 +00:00Commented Mar 31, 2019 at 18:10
-
I updated my answer. First we need check
array1 == null || array1.length == 0
before array operation, ex:array1[0]
.Winner– Winner2019年03月31日 18:13:19 +00:00Commented Mar 31, 2019 at 18:13
int[] intArray = new int[];