aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList" Watch "ArrayList" New topic
Author

ArrayList

thomas davis
Ranch Hand

Joined: Feb 01, 2003
Posts: 207
List lst = new ArrayList();

do I need to check for null ? like if (lst!=null),if if I have not added values to this array list .please confirm.?

If intialize List as follows

List lst = null; or ArrayList lst =null;

In this case I need to check for null ,if I have not added values to this array list .please confirm.

If I use enhanced for loop ,od I need to check for null and size()>0 checking?

List<String> lst = new ArrayList<String>();
if( lst !=null&&(lst.size()>0)){
for(String strParam:lst){
strParam
}

}


Do I need to use the if( lst !=null&&(lst.size()>0)) as shown above for enhanced for loop?
Please explain.


What are the good practces for arraylist and enhanced for loop together?

do I need to intialize ArrayList always ?
do I need to check for null always?
do I need to check for size() always?

Thanks

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
No, it's obvious



you can be sure: lst is not null

If you instead write the following statement



you can be sure: lst is null

In both cases there's no use for checks


If I use enhanced for loop ,do I need to check for null and size()>0 checking?


Try the following example and see what happens. Does it compile? Does it run without complaining?



Bye,
Nicola
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList