my dog learned polymorphism
The moose likes Beginning Java and the fly likes benefits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "benefits" Watch "benefits" New topic


johnathan smith

Joined: Mar 30, 2007
Posts: 21

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

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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 ]
I agree. Here's the link:
subject: benefits
jQuery in Action, 3rd edition