28

I am downloading some data into a String array. Let's say ImageLinks. How do I check if a item in array exist?

I am trying

if(ImageLinks[5] != null){}

but it gives me ArrayIndexOutOfBoundsException. (Because there are really no 5 link in the array)

1
  • if (ImageLinks.length > 5) ?? Commented Oct 15, 2012 at 14:20

6 Answers 6

38

To prevent the ArrayIndexOutOfBoundsException, you can use the following:

if(ImageLinks.length > 5 && ImageLinks[5] != null)
{
    // do something
}

As the statements in the if are checked from left to right, you won't reach the null check if the array doesn't have the correct size.

It's quite easy to generalise for any scenario.

Sign up to request clarification or add additional context in comments.

3 Comments

What about an array where you set the indexes? Like in a[0] = 20; a[5] = 30;. If I check if(a.length > 4 && a[4] != null) it throws a null exception, doesn't?
@sdlins To be able to set index 5, the array has to have a length of at least 6 before you get to the if statement. At that point it'll check if the index exists and whatever it is null. There won't be any exception.
Yeah! Sorry, you are right! I tried by code it and it works fine. I was probably mixing different languages concepts. Thanx!
8

Write a static function

public static boolean indexInBound(String[] data, int index){
    return data != null && index >= 0 && index < data.length;
}

Now, give it a call in your code

if(indexInBound(ImageLinks, 5) && ImageLinks[5] != null){
   //Your Code
}

7 Comments

This will not take care of the null case.
@Baz This is addressing the ArrayIndexOutOfBoundsException, if we want to handle Null Condition we can anytime add one more catch block for NullPointerException
Just saying that OP wants to achieve both.
That is also right, there isn't any statement in the code throwing a NPE.
I got a few issues with this answer... 1) Exceptions should be used for exceptional behavior; using exceptions to handle expected behavior, such as in a true or false check, is very bad. 2) is...Exists is bad naming.
|
6

Make sure array is of that length before doing lookup

if(ImageLinks.length > 5 && ImageLinks[5] != null){}

Comments

1

The reason its fails is the array has less than 6 elements.

Check first the array has correct number of elements, and then check the element exists in array.

 if (ImageLinks.length > 5 && ImageLinks[5] != null) {
     // do something
 }

3 Comments

Your answer is not correct. Refer to the "short-circuiting" logic on the following Java documentation page: docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
if the first boolean statement is false and there is an && operator after it, there is no reason to continue evaluation as the entire statement must at that point be false regardless of whether or not the 2nd operand is true or false
Corrected the answer
0
if (ImageLinks != null && Stream.of(ImageLinks).anyMatch(imageLink-> imageLink != null)) {
//An item in array exist
}

Comments

-1

yes there are less than 6 elements ImageLinks[5] refers to 6th element as array index in java starts from 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.