What's the point of parameterizing an ArrayList?
Author |
Message |
FileFantasy
|
Posted: Fri Aug 07, 2009 11:07 am Post subject: What's the point of parameterizing an ArrayList? |
|
|
I've always been taught that, when using ArrayList, you just declare it like so:
ArrayList list = new ArrayList ();
So I installed Eclipse yesterday (was using Ready), and when I load up my code, it underlines my ArrayList in yellow, claiming that it suggests me to parametrize it.
My question is: what's the point? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Fri Aug 07, 2009 11:40 am Post subject: RE:What\'s the point of parameterizing an ArrayList? |
|
|
The point is that in Java you can tell the List (ArrayList, LinkedList, Vector) what type it's allowed to hold. Thereafter, attempting to put any other type into it is an error. This prevents you from accidentally doing stupid things like putting Integers in with your Whatsits, which is usually not intended. It also prevents you from having to cast your return from .get( int index ) to the correct type - it's already the type that you specified.
If you really really want to do it the old way, you can just parametrize it with <Object> - but this is strongly discouraged. You should be able to do it properly.
Here's an example:
Java: |
List<MyObject> list = new ArrayList<MyObject>();
|
|
|
|
|
|
|
chrisbrown
|
Posted: Fri Aug 07, 2009 12:02 pm Post subject: Re: What's the point of parameterizing an ArrayList? |
|
|
Parametrization allows the compiler to catch otherwise tricky bugs in your programs.
Copy the following into Eclipse and run it:
Code: | ArrayList listOfString = new ArrayList();
listOfString.add("Hello");
listOfString.add(5);
String str1 = listOfString.get(0); // Returns an object, not a String
String str2 = (String)listOfString.get(0); // So we have to make a cast
String str3 = (String)listOfString.get(1); // What if we accidentally cast wrong?
|
Now replace that with this,and note the error on the second add():
Code: | ArrayList<String> listOfString = new ArrayList<String>();
listOfString.add("Hello");
listOfString.add(5); // This is not a String, so we can't add it to the list
String str1 = listOfString.get(0); // Returns a String, so no cast is needed
|
If you want to learn more,
http://java.sun.com/docs/books/tutorial/java/generics/index.html |
|
|
|
|
|
Vermette
|
Posted: Sat Aug 08, 2009 2:37 pm Post subject: Re: RE:What\'s the point of parameterizing an ArrayList? |
|
|
DemonWasp @ August 7th 2009, 11:40 wrote:
Java: |
List<MyObject> list = new ArrayList<MyObject>();
|
Thought I'd point out the above as well as a good example of Polymorphism. unless you need to guarantee specific behaviours of ArrayList or Vector etc., it is generally a good practice to declare or return Objects with their interface type. |
|
|
|
|
|
DemonWasp
|
Posted: Sat Aug 08, 2009 3:32 pm Post subject: RE:What\'s the point of parameterizing an ArrayList? |
|
|
True - that's another point that's non-obvious when learning Java:
1. List is an interface. It defines how you can interact with list-like objects (ArrayLists, LinkedLists...).
2. You don't necessarily care whether something is an ArrayList or LinkedList so long as it's a List - so that's what you should declare it as: the most general type that makes sense.
3. In practice, you'll initialize the List object as an ArrayList or LinkedList, depending on your needs (hint: you probably want the ArrayList, unless you're going to be inserting into the middle of the list insanely frequently).
This happens frequently with Collections and other types found in the Java libraries, so always look at the inheritance list for every class in the Java API.
Also, be aware that Vectors are very different from ArrayLists - they behave exactly the same except that Vectors are thread-safe structures (thus being slower for single-threaded programs). |
|
|
|
|
|
|
|