• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a certain element in a linked list  RSS feed

 
Alex Almani
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i access a certain element in a linked list. I see there is a get() function but that doesnt seem to work. I need to basically traverse the linked list, node by node.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out the description of the linked list datastructure:
http://www.nist.gov/dads/HTML/linkedList.html

basically in a linked list each element has a link to the next elmement. supposed to be a "fast" datastructure for sequential access but "slow" for random access since you have to traverse the whole list. If possible code to the interface (java.util.List) not the implementation (ArrayList, LinkedList, ...)

btw: there is also a double linked list

pascal
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What doesn't work about get()? In any case, what you want to do is use an Iterator:This will perform fine with either a LinkedList (which, by the way, implements a doubly linked list) or an ArrayList.

- Peter
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!