This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Follow Val's link and read that article - it's excellent. However, let me highlight a few things for you... First of all, notice that ArrayList and LinkedList both implement the List interface and extend the AbstractList class. Therefore, the two classes have all of the methods defined within those in common (i.e. add, get, indexOf, etc.). In most cases, it's best to treat a list (no matter what type of list it is, ArrayList or LinkedList, or whatever) as a List object, like this:
As List is an interface, it's impossible to actually create a List object, so we know that l isn't a List, but we know that it implements that interface. Therefore, we know that we can invoke any methods on it that aer defined within that interface. However, as LinkedList and ArrayList are different classes, they do have some differences. First and foremost, they are implemented differently - this is done behind the scenes, but it's important. In that article Val pointed you to, take a look at the table that shows the amount of time it takes to perform various operations on individual list types. Notice that the ArrayList is much faster when it comes to retrieving an item, while a LinkedList is much faster when adding and removing an item. The reason for this is based on the implementation of the classes. Therefore, based on the info given there, if you have a more or less static list that you need only retrieve items from, an ArrayList is a great choice but, if you need to add and remove items often, it would be more efficient to go with a LinkedList. Okay, I've babbled longer than I planned, but I hope that helps. Corey [ March 12, 2003: Message edited by: Corey McGlone ]