What is ArrayDeque.addLast() method in Kotlin?
Overview
The ArrayDeque class contains the addLast() method, which adds the specified element to the end of the queue.
Note:
ArrayDequeis an implementation of the deque data structure using a resizable array. Deque (pronounced as deck) stands for the double-ended queue. This class provides methods to access both ends of the queue.
The kotlin.collections package is part of Kotlin’s standard library. It contains all collection types, such as map, list, set, etc.
The package provides the ArrayDeque class, a mutable double-ended queue, and uses a dynamic, resizable array as backing storage.
Syntax
fun addLast (element: E)
Arguments
This method takes an element of type E as input.
Return value
This method does not return any value.
The illustration below shows the function of the addLast() method of the ArrayDeque class:
Code
fun main() {val dayQueue = ArrayDeque<String>()dayQueue.add("Monday")dayQueue.add("Tuesdy")dayQueue.add("Wednesday")println("Printing Queue elements --")println(dayQueue)dayQueue.addLast("Thursday")println("Printing Queue elements after calling addLast() --")println(dayQueue)}
Explanation
-
Line 2: We create an empty queue named
dayQueueto store the strings. -
Lines 4 to 6: We add a few days’ names to the
dayQueueobject using theadd()method. -
Lines 8 and 9: We print all the elements present in the queue.
-
Lines 11: We call the
addLast()method and addThursdayto the end of the queue. -
Line 12 and 13: We print the elements of
dayQueueafter calling theaddLast()method. Observe thatThursdayis present at the end of the queue.
The ArrayDeque elements before and after calling addLast() method are displayed using the println() function of the kotlin.io package.