permaculture playing cards*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to declare an ArrayList" Watch "How to declare an ArrayList" New topic

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

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

Joined: Oct 13, 2005
Posts: 38107
You can use different kinds of List; you might reassign the reference like this
Don't get me started about those stupid light bulbs.
subject: How to declare an ArrayList
Similar Threads
help with magic square
getting compile error ,if try to compile with jdk 1.6.
Simple ArrayList conundrum?
List with a sublist of Integers