Count The Amount Of Data In An Array Including SOME Null

Posted by Josephine on Stack Overflow See other posts from Stack Overflow or by Josephine
Published on 2012-11-06T22:55:15Z Indexed on 2012/11/06 22:59 UTC
Read the original article Hit count: 118

Filed under:

I'm coding in java and I need to create a function that returns the number of Data objects that are currently in an ArrayList. At the moment I have this:

  int count = 0;
    for (int i = 0; i < data.length; i++)
    { 
        if (data[i] != null)
        {
            count ++;
        }
    }
    return count;

But the problem is that an array list that includes null data is acceptable, and I have to count their null data towards this counter. How do I include the null data that's in the middle of this array, and not the null data that's not supposed to be counted for?

For example, I have some tester code that adds (8),null,null,(23),(25) to the array, and this function should return 5 when the initial array size is 10.

Thank you so much

© Stack Overflow or respective owner

Related posts about java