Question:
What is a queue in Java?
theguyman
2010-10-23 12:10:39 UTC
I know the basic concept of it ( FIFO) but i wanted to know if it was like an array? Is it like na array in that it has indexes for each cell like an integer array, only instead of integers, the data stored in them are objects?

So Basically all im asking is if they function as arrays, but use objects as their primary data instead of integers like a regular array?
Three answers:
Zarn
2010-10-23 12:12:53 UTC
It can be implemented as an array, or a linked list for that matter. The exact implementation isn't important, as long as it presents an API and behaviour that is consistent with a stack (FILO) or a queue (FIFO).
?
2010-10-23 19:18:31 UTC
When putting things into or taking things out of an array, you need to use an integer index to indicate the place. When using a queue, that concept no longer applies: You can only add to the end, and take from the front. Behind the scenes, the queue might work like an array, but that's not guaranteed and irrelevant anyway. I'm not sure what you mean by "primary data," but you can have arrays of objects just as easily as you can have arrays of integers. Queues are no different.
deonejuan
2010-10-23 19:22:48 UTC
Your right, it is a memory store under one location. But unlike an array which you reference by number call-outs, eg myArray[ 9 ], a Queue we can only peek(), pop(), push(); A Queue destructs as you access.



The advantage is: the data structure tracks the variable instead of the coder having to contrive some clever way of remembering the varName. Non-spaghetti.



The Collections in Java can only use Objects. If you need a scalar, use the Wrappers that make primitive values into an Object, ie, q.push( new Integer( 44 ) );


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...