Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Easy way to find whether all items of an ArrayList is null?

 
jeevan stanzilous
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method, or easy way to check whether the items of the ArrayList is null?
 
Brendon Randall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well why do you not loop through your ArrayList and test to see if that index is null or not? What type is your ArrayList?
 
Maneesh Godbole
Saloon Keeper
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iterate through the list and check null. Since a List allows null, the size, when the list contains one null is one.
 
Prashant Hurria
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is posted in the beginner forum i will go in bit of a detail (Code Examples etc.)

I don't think there is any way other than looping and checking each element for null.
The only other supposedly "Easy" way could have been by checking out the size but that wont work because if we add null to an arraylist it increments its size.
Code Example :- In the below mentioned code we add null twice to the arrayList aList and it prints out its size as '2'.



Here is a method for finding if the arraylist is null

 
Prashant Hurria
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Campbell Ritchie. Why was the code example deleted.Was it a little more than what the answer required.
Posted it as it was a beginners forum.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashant Hurria wrote:Since this is posted in the beginner forum i will go in bit of a detail (Code Examples etc.)
This is exactly what we discourage. Look at the heading for the Beginners' Forum and you read this:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
By giving a simple answer like that, you are preventing jeevan stanzilous from learning Java. Please don't post answers like that.
 
Prashant Hurria
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.Thanks for making me aware.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic