queue initiate

M

mehmet canayaz

can anyone initiate a queue with the new index ??
I am trying stuff like:

//queue with max 20 int elements
Queue<int> myQ = new Queue<int> (20);

mican
 
J

John C. Bollinger

mehmet said:
can anyone initiate a queue with the new index ??

This would normally be the sort of thing to consult the API docs about.
Nevertheless ...
I am trying stuff like:

//queue with max 20 int elements
Queue<int> myQ = new Queue<int> (20);

Are you talking about java.util.Queue? That's an interface, so you
can't instantiate it directly. You must choose a particular
implementation to instantiate, and the answer to your question then
depends on which implementation is used. According to the API docs,
"known" implementations of Queue are AbstractQueue, ArrayBlockingQueue,
ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList,
PriorityBlockingQueue, PriorityQueue, and SynchronousQueue. Some of
those are abstract classes, which you also cannot instantiate directly.

Furthermore, type parameters must be reference types, not primitive
types (such as int).

You might want something like this:

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

....

Queue<Integer> myQueue = new ArrayBlockingQueue(20);

You should, however, understand all the implications of choosing that
particular Queue implementation. The API docs should help you there.
 
R

Roedy Green

can anyone initiate a queue with the new index ??
I am trying stuff like:

//queue with max 20 int elements
Queue<int> myQ = new Queue<int> (20);

Queue is an interface, not a class. You can thus have a Queue
reference but you cannot directly instantiate a Queue.

look up Queue in the Sun docs to see the many concrete classes that
implement it. Let's presume you decided on an ArrayBlockingQueue<E>.

Then you could say

Queue<int> myQ = new ArrayBlockingQueue<int>(20);
 
R

Roedy Green

Queue<int> myQ = new ArrayBlockingQueue<int>(20);

actually I have not tried that to see how far unboxing goes. You
would normally write that

Queue<Integer> myQ = new ArrayBlockingQueue<Integer>(20);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top