Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
data structures
communitycreator

What is ArrayDeque.addLast() method in Kotlin?

Siddharth Singh

Overview

The ArrayDeque class contains the addLast() method, which adds the specified element to the end of the queue.

Note: ArrayDeque is 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 dayQueue to store the strings.

  • Lines 4 to 6: We add a few days’ names to the dayQueue object using the add() method.

  • Lines 8 and 9: We print all the elements present in the queue.

  • Lines 11: We call the addLast() method and add Thursday to the end of the queue.

  • Line 12 and 13: We print the elements of dayQueue after calling the addLast() method. Observe that Thursday is 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.

RELATED TAGS

kotlin
data structures
communitycreator
RELATED COURSES

View all Courses

Keep Exploring