Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

kotlin
communitycreator

How to check if the ArrayDeque is empty in Kotlin

Programming Bytes

Overview

We can use the isEmpty method to check if the ArrayDeque object has any elements.

Syntax

fun isEmpty(): Boolean

Parameters

This method does not take any arguments.

Return value

This method returns true if the arrayDeque object has no elements. Otherwise, it returns false.

Example

The code below demonstrates how to use the isEmpty method.

fun main() {
  // Create a new ArrayDeque object
  val deque = ArrayDeque<String>()

  println("deque is :  " + deque)
  // check if the deque is empty or not
  println("deque.isEmpty() : " + deque.isEmpty())
  
  // add some elements 
  deque.add("one")
  deque.add("two")  
  deque.add("three")      
  
  println("\ndeque is :  " + deque);
  // check if the deque is empty or not
  println("deque.isEmpty() : " + deque.isEmpty())
}
Using the isEmpty method

Explanation

  • Line 3: We create an ArrayDeque object with the name arrayDeque. This object doesn’t have any elements in it.

  • Line 7: We call the isEmpty method on the arrayDeque object. This method will return true because the arrayDeque object is empty.

  • Lines 10–12: We use the add() method of the arrayDeque object to add three elements to it.

  • Line 16: We call the isEmpty method on the arrayDeque object. This method will return false because the arrayDeque object contains three elements.

RELATED TAGS

kotlin
communitycreator
RELATED COURSES

View all Courses

Keep Exploring