• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is the difference between LinkedList and ArrayList ?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between LinkedList and ArrayList ?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,Thanks
I am clear now .
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic