26

I'm trying to instantiate an object of Queue using below code

var queue: Queue<Int> = Queue()

But I get this

Interface Queue does not have constructors

No idea what's going on, while searching I found this link.

But I don't understand anything. Please help.

3 Answers 3

35

Queue is an interface. So you can't instantiate an interface, you have to implement it or instantiate a class that implement it.

For example, you can do var queue: Queue<Int> = ArrayDeque<Int>(). ArrayDeque implements Queue.

Sign up to request clarification or add additional context in comments.

4 Comments

This will be then a deque not queue I guess?
Deque extends Queue
Hmm, after going through multiple stackoverflow questions, what I figured out is there are various implementations of the Queue interface. Now my problem is which one should I use. I just want a really basic queue. Should I go with ArrayDeque or LinkedList?
ArrayDeque is the choice to go for in doubt, because LinkedLists carry significant overhead that is only justified in rare cases: stackoverflow.com/questions/6163166/…
14

You trying to create instance of interface but don`t override methods for it. You should use something like this:

val queueA = LinkedList<Int>()
val queueB = PriorityQueue<Int>()

Also you can read more about queue implementations here

Comments

3

Queue is an interface. So you can't instantiate an interface. You should use something like this:

val queue: Queue<Int> = LinkedList()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.