.clear()
Anonymous contributor
Published Jun 9, 2024
Contribute to Docs
In Dart, the .clear()
method removes all elements in the queue. After invoking this method, the queue will be empty, and its size will be zero.
The .clear()
method is a (void
) method that does not return any value and does not accept any arguments.
Syntax
To call the .clear()
method and clear a queue named myQueue
, the syntax is as follows:
myQueue.clear();
Note: To use a queue in a Dart program, it is necessary to import the
dart:collection
module.
Example
In the following example, a queue is created, elements are inserted in the queue and the .clear()
method removes all elements in the queue:
import 'dart:collection';void main(){// Creating a queueQueue<int> myQueue = new Queue<int>();// Inserting elements in the myQueuemyQueue.addAll([1, 2, 3, 4, 5, 6]);print("Queue before clearing: $myQueue");// removing all elements in the myQueuemyQueue.clear();print("Queue after clearing: $myQueue");}
The above code will result in the following output:
Queue before clearing: {1, 2, 3, 4, 5, 6}Queue after clearing: {}
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.