Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin : Interface Queue does not have constructors

Tags:

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.

like image 624
Vicky Avatar asked Jun 06 '17 12:06

Vicky


People also ask

CAN interface have constructor in Kotlin?

Interfaces cannot have constructors in Kotlin. Interfaces can have: declarations of abstract methods. method implementations.

How do I create a queue in Kotlin?

In the base package, create a file named Queue. kt and add the following code defining the Queue interface. interface Queue<T> { fun enqueue(element: T): Boolean fun dequeue(): T? val count: Int get val isEmpty: Boolean get() = count == 0 fun peek(): T? }

Is there a queue in Kotlin?

In kotlin, the language queue is one of the collection interfaces, and it is used for to store and remove the datas using the FIFO concept. The datas are stored on the last side, that is, the rear position of the array index, and the datas are removed using the front position.


2 Answers

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.

like image 99
Kevin Robatel Avatar answered Oct 02 '22 00:10

Kevin Robatel


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

like image 33
Silvestr Avatar answered Oct 02 '22 02:10

Silvestr