• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LinkedList vs ArrayList

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

What are the differences between these two Classes that I need to understand and know for the exam?

I know ArrayList is ordered by index and that it is automatically growable.

From what I gather, LinkedList is just like ArrayList but its elements are 'doubly linked'. What is meant by doubly linked and what is the signifigance of it?

Thanks
 
Greenhorn
Posts: 22
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both ArrayList and LinkedList implements List interface so both of them are ordered by index and dynamically update its size

The difference is in the underlying implementation of these classes. ArrayList under the hood is simply an array and when you do

List a = new ArrayList();
a.add(4);

then the add method creates an array and add 4 to its 1st position

later on if you do

a.add(5)

then the add method creates another array of size = previous size + 1 and copy over the old values and write 5 to the 2nd position

However, LinkedList is all about pointers. It keeps a reference to the next element as well as the reference to the previous element (if it is doubly linked list...by default it is singly linked list)

Doubly linked list are useful if you want to access the previous element from the current position rather than iterating it forward until you wrap back to the previous element

It is better to use Linked List over Array List if your code requires lot of insertion and deletions. Please see this link and hope you find it helpful

http://en.wikipedia.org/wiki/Linked_list
 
Glen Iris
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Prabhjot Jassal
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A very interesting and enlightening discussion of the pros and cons of each implementation:

http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic