Skip to content

Check if arraylist contains only nulls

I think there has to be a better way of doing this.. I have a call to a function that returns an ArrayList. If that ArrayList only returns 10 null items (the default), is there a way of checking this without iterating through all 10 items to see if they are null?

Answer

Generally, no; there is no other way to tell that an arbitrary ArrayList contains ten instances of null than to loop over it and make sure each element is null. You can forgo this, of course, if the size() of the list isn’t equal to ten.