• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between LinkedList and ArrayList ?

 
Fox Hu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between LinkedList and ArrayList ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check out the following article by Thomas Paul published in the Javaranch newsletter: Collections in Java Part 1 - The List Interface
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,Thanks
I am clear now .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic