What is DoubleLinkedQueue.clear() method in Dart?
The dart:collection library provides advanced collection support for the Dart language. It also contains the DoubleLinkedQueue<E> class which implements the abstract Queue<E> class using a doubly-linked list.
The clear() method
The DoubleLinkedQueue<E> class contains the clear() method which removes all the elements of the queue. The queue size becomes zero after calling the clear() method.
A queue is a FIFO (First In First Out) data structure. In a queue, the element that is added first will be deleted first.
The clear() method modifies the state of the queue. The illustration below shows how the clear() method works.
Syntax
The prototype of the clear() method is shown below.
void clear()
Arguments
The clear() method does not take any arguments.
Return values
- The
clear()method does not return any value. - The
clear()method is a constant-time operation.
Code
The code below shows how the clear() method can be used in Dart.
import 'dart:collection';void main() {var quadNations = DoubleLinkedQueue<String>();quadNations.add("India");quadNations.add("US");quadNations.add("Australia");quadNations.add("Japan");print('Printing Queue Elements');print(quadNations);quadNations.clear();print('Removed all elementes using clear() method');print('Printing Queue Elements');print(quadNations);}
Explanation
The code above performs the following actions:
-
An instance of the
DoubleLinkedQueueclass of typeStringis initialized in line 3. -
Next, a few strings,
"India","US","Australia", and"Japan", are added to the queue using theadd()method. -
The queue elements are displayed as
{India, US, Australia, Japan}. All the elements are added to the end of the queue, as the queue is a FIFO data structure. -
The
clear()method in line 12 removes all elements of the queue. -
Upon printing all the queue elements again, it can be observed that the queue has become empty and there are no elements in the queue.
Free Resources
- undefined by undefined