aspose file tools*
The moose likes Beginning Java and the fly likes How to declare an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to declare an ArrayList" Watch "How to declare an ArrayList" New topic
Author

How to declare an ArrayList

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 249
Which of the following two definitions is better?
Is there a better way than the following two definitions?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
Please don’t add a new question to an old post. Fortunately I can split your question into a new thread.
They are both poor forms. The correct way to do it in Java7 is
In older versions of Java you would writeThe reason is that the declaration does not commit you to using an ArrayList; you could use a LinkedList or other sort of List anywhere. Your outer List could even contain several different List implementations, but they would all contain Integers.
You should only use the constructor argument 5 if you are sure you will be using 5 elements.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
You can use different kinds of List; you might reassign the reference like this
 
jQuery in Action, 2nd edition
 
subject: How to declare an ArrayList