Meaningless Drivel is fun!*
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

How to declare an ArrayList

albert kao
Ranch Hand

Joined: Feb 04, 2010
Posts: 239
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: 36501
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: 36501
You can use different kinds of List; you might reassign the reference like this
I agree. Here's the link:
subject: How to declare an ArrayList
Similar Threads
Simple ArrayList conundrum?
getting compile error ,if try to compile with jdk 1.6.
List with a sublist of Integers
help with magic square