Every element of an array != null?

I'm trying to see if every single entry in an array is not equal to null, thus disabling the addition of another Account into my array, or in my construction's case, .getClientID != 0. (This because every Account object has a standard constructor with clientID, a field, set to 0).

boolean full = true; for (Account acc: accounts) { if (acc.getClientID() != 0) { full = false; } } if (!full) { Do stuff }

This does not work, how do I go about this?



  • edited April 2018 Answer ✓

    that will check all of the accounts, all the time, overwriting full each time. effectively, this just returns the value for the last element.

    you should break out the loop the first time the value is false. and then check the value using if() afterwards (question: why?)

  • if every single entry in an array is not equal to null

    if you put it in the function boolean allEntriesAreNot0() { you could also use return instead of break;

    remember null is not 0, there is a special word null

  • @Koogs, thank you so much. I just realised that checking if every entry is not null is stupid, better to see if at least one element is null. Think first, ask later, as my father said. Thank you!

Sign In or Register to comment.