| Author |
How to declare an ArrayList
|
albert kao
Ranch Hand
Joined: Feb 04, 2010
Posts: 224
|
|
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: 32638
|
|
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: 32638
|
|
|
You can use different kinds of List; you might reassign the reference like this
|
 |
 |
|
|
subject: How to declare an ArrayList
|
|
|