This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Easy way to find whether all items of an ArrayList is null? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Easy way to find whether all items of an ArrayList is null?" Watch "Easy way to find whether all items of an ArrayList is null?" New topic
Author

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

jeevan stanzilous
Greenhorn

Joined: Jun 16, 2009
Posts: 9
Is there any method, or easy way to check whether the items of the ArrayList is null?
Brendon Randall
Greenhorn

Joined: Mar 13, 2009
Posts: 10
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

Joined: Jul 26, 2007
Posts: 10489
    
    9

Iterate through the list and check null. Since a List allows null, the size, when the list contains one null is one.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Prashant Hurria
Ranch Hand

Joined: Mar 23, 2009
Posts: 40
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

Joined: Mar 23, 2009
Posts: 40
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Mar 23, 2009
Posts: 40
Got it.Thanks for making me aware.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Easy way to find whether all items of an ArrayList is null?