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.
Interfaces cannot have constructors in Kotlin. Interfaces can have: declarations of abstract methods. method implementations.
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? }
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With