We can use the clear method to remove all the elements present in the ArrayDeque object.
fun clear()
This method doesn’t take any arguments.
This method doesn’t return any value.
The code below demonstrates how to use the clear method to remove all elements present in the ArrayDeque:
fun main() {// create a new empty ArrayDeque which can have int elementsvar deque = ArrayDeque<Int>()// add four elements to dequedeque.add(1)deque.add(2)deque.add(3)deque.add(4)println("\nThe Arraydeque is $deque")// get the last element of dequedeque.clear();println("After calling clear the dequeu is $deque");}
In the code above, we see the following:
Line 3: We create a new ArrayDeque with the name deque. The deque contains no element [].
Lines 6–9: We add four elements to deque using the add method. Now deque is [1,2,3,4].
Line 13: We use the clear method to remove all elements present in deque. After calling the clear method, deque becomes empty.