• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

benefits

 
johnathan smith
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey

just wondering if anyone could tell me the benefits of using an ArrayList as oppose to a stack or a queue etc?

thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a bit like asking for the benefits of drinking wine instead of beer.

ArrayList Stack and Queue are different from one another.

ArrayList is a random-access collection; you can insert a value at point x and retrieve the value from point y. It is based on an array; you can access object no 1 and object no 1000000 in just about the same time. Object no 0 might be a bit faster; not sure.
A Queue is like a LinkedList, where the value objects form a chain; it is implemented to work in a FIFO (first in-first out) order. Just like the queue at Morrison's for the till.
A Stack works in a LIFO (last in-first out) manner; you push objects onto it and "peek" (inspect) or "pop" (remove and retrieve) whichever object was last pushed into the stack. Like a pile of plates in the kitchen; you put plates on top of the pile and take the top plate to eat from.

So you use the different collection types depending which order you want the values back.

[edit]I mistakenly said FIFO was first in last out: corrected[/edit]
[ April 23, 2007: Message edited by: Campbell Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic