13

I was looking through the code for an old Android application of mine, and I saw one thing I did to the effect of this:

        boolean emptyArray = true;
        for (int i = 0; i < array.size(); i++)
        {
            if (array.get(i) != null)
            {
                    emptyArray = false;
                    break;
            }
        }
        if (emptyArray == true)
        {
            return true;
        }
        return false;

There has to be a more efficient way of doing this -- but what is it?

emptyArray is defined as an ArrayList of Integers, which are inserted with a random number of null values (And later in the code, actual integer values).

Thanks!

1
  • By the way, you just use if (emptyArray) instead of if (emptyArray == true) Commented Jun 5, 2011 at 15:58

5 Answers 5

25

Well, you could use a lot less code for starters:

public boolean isAllNulls(Iterable<?> array) {
    for (Object element : array)
        if (element != null) return false;
    return true;
}

With this code, you can pass in a much wider variety of collections too.


Java 8 update:

public static boolean isAllNulls(Iterable<?> array) {
    return StreamSupport.stream(array.spliterator(), true).allMatch(o -> o == null);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I did edit it to swap them soon after posting, so yeah, but not any more. the final return should be true for a method called "isAllNulls" if it didn't find any nulls in the loop
Thanks! Actually, this same project I had planned on going through to convert all the "for" loops to the "for each" loop syntax you use above.
Ughh ... unblocked while containing unblocked if. Didn't your lecturers tell about how { }-s protect you against misleading indentation?
@Stephen: in this case is not that bad that isn't in block, since it is a small function that won't change and and communicates the purpose well. I would just make it more general to use List<?>, so that will be useful for any List, and also declare it static
6

There is no more efficient way. The only thing is you can do, is write it in more elegant way:

List<Something> l;

boolean nonNullElemExist= false;
for (Something s: l) {
  if (s != null) {
     nonNullElemExist = true;
     break;
  }
}

// use of nonNullElemExist;

Actually, it is possible that this is more efficient, since it uses Iterator and the Hotspot compiler has more info to optimize instead using size() and get().

1 Comment

I will comment to my own answer: putting it in functions as Bohemian did, it is much more useful
1

It's not detection of contains only null values but it maybe be enough to use just contains(null) method on your list.

Comments

0

Simply Check it worked for me. Hope will work fine for you too!

    if (arrayListSubQues!=null){
return true;}
else {
return false }

Comments

-1

I use to do something like this :

// Simple loop to remove all 'null' from the list or a copy of the list
while array.remove(null) {
    array.remove(null);
}

if (CollectionUtils.isEmpty(array)) {
    // the list contained only nulls
}

2 Comments

I said to "remove all null from a copy of the list", since you want to work on it. You can also need to sort it or count stuff, without impacting the original list.
The question was not about removing (and I don't know why my previous comment has disappeared)

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.