Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is the difference between LinkedList and ArrayList ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is the difference between LinkedList and ArrayList ?" Watch "What is the difference between LinkedList and ArrayList ?" New topic
Author

What is the difference between LinkedList and ArrayList ?

Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
What is the difference between LinkedList and ArrayList ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please check out the following article by Thomas Paul published in the Javaranch newsletter: Collections in Java Part 1 - The List Interface


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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 ]

SCJP Tipline, etc.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Notice that the ArrayList is must faster when it comes to retrieving an item, while an ArrayList is much faster when adding and removing an item.

I am assuming that in the above quote the bold should be LinkedList. Please correct me if I am wrong.
[ March 12, 2003: Message edited by: Sarma Lolla ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Sarma Lolla:

I am assuming that in the above quote the bold should be LinkedList. Please correct me if I am wrong.
[ March 12, 2003: Message edited by: Sarma Lolla ]

Indeed - it should have been. Sorry about that. I've fixed it now.
[ March 12, 2003: Message edited by: Corey McGlone ]
Fox Hu
Ranch Hand

Joined: Jan 23, 2003
Posts: 49
Well,Thanks
I am clear now .
 
GeeCON Prague 2014
 
subject: What is the difference between LinkedList and ArrayList ?