my dog learned polymorphism*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 245
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: 37907
    
  22
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: 37907
    
  22
You can use different kinds of List; you might reassign the reference like this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to declare an ArrayList
 
Similar Threads
help with magic square
getting compile error ,if try to compile with jdk 1.6.
JTable
Simple ArrayList conundrum?
List with a sublist of Integers