• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 23919
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.
 
I can't take it! You are too smart for me! Here is the tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic