Help coderanch get a
new server
by contributing to the fundraiser
  • 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

ArrayList Vs LinkedList

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

In one of the whizzlabs exams, there is this question:

Which collection implementation is suitable for maintaining an ordered sequence of objects when objects are frequently inserted and removed from the middle of the sequence?

Please select:
a. TreeMap
b. Vector
c. ArrayList
d. LinkedList





The correct answer is 'd. LinkedList' and the explanation for the answer says:

"When objects are frequently inserted and deleted from the middle of the sequence, LinkedList gives the best performance."


I am a little puzzled by this explanation. In the Kathy Sierra book it is mentioned:

"LinkedList: Good for adding elements to the ends, i.e., stacks and queues."



Why this difference? Hope someone can help. Thanks.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Paterson wrote:
The correct answer is 'd. LinkedList' and the explanation for the answer says:

"When objects are frequently inserted and deleted from the middle of the sequence, LinkedList gives the best performance."


I am a little puzzled by this explanation. In the Kathy Sierra book it is mentioned:

"LinkedList: Good for adding elements to the ends, i.e., stacks and queues."



Why this difference? Hope someone can help. Thanks.



Based on the context of the question, the explanation is correct. Linked lists are good for inserting and deleting (anywhere) in the list. However, it is not very good at getting to a particular member in the middle of the list -- which the question carefully avoided mentioning. The question did not mention that you need to have an iterator at the point that you want to insert or delete.

As for the book, that is correct too. A linked list do work great for stacks and queues. And it is also easy to add elements to the ends.

Henry
 
John Paterson
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry Wong,

Thanks, appreciate it.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic