• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
I love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic