C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First-In-First-Out: The element that is added first (with Enqueue) is also the first one that is removed (with Dequeue).
Note: For Each is implemented in a special way so that it shows all the internal elements in the Queue.
For Each, ForVB.NET program that uses Queue generic type
Module Module1
Sub Main()
' Add integers to Queue.
Dim q As Queue(Of Integer) = New Queue(Of Integer)()
q.Enqueue(5)
q.Enqueue(10)
q.Enqueue(15)
q.Enqueue(20)
' Loop over the Queue.
For Each element As Integer In q
Console.WriteLine(element)
Next
End Sub
End Module
Output
5
10
15
20
Enum: Requests (represented by the RequestType enum) can be read with Dequeue after testing them with Peek.
EnumImportant: With a Queue, the oldest (first added) help requests will be the first to be handled.
Stack: With a Stack, the newest help requests would be the first to be handled. You would always provide help to people who most recently requested it.
VB.NET program that uses Enum Queue
Module Module1
Enum RequestType As Integer
MouseProblem
TextProblem
ScreenProblem
ModemProblem
End Enum
Sub Main()
Dim help As Queue(Of RequestType) = New Queue(Of RequestType)()
' Add some problems to the queue.
help.Enqueue(RequestType.TextProblem)
help.Enqueue(RequestType.ScreenProblem)
' If first problem is not a mouse problem, handle it.
If help.Count > 0 Then
If Not help.Peek = RequestType.MouseProblem Then
' This removes TextProblem.
help.Dequeue()
End If
End If
' Add another problem.
help.Enqueue(RequestType.ModemProblem)
' See all problems.
For Each element As RequestType In help
Console.WriteLine(element.ToString())
Next
End Sub
End Module
Output
ScreenProblem
ModemProblem
Next: Call CopyTo and pass the reference variable to that array as the first argument.
VB.NET program that uses CopyTo
Module Module1
Sub Main()
' New queue.
Dim q As Queue(Of Integer) = New Queue(Of Integer)()
q.Enqueue(5)
q.Enqueue(10)
q.Enqueue(20)
' Create new array of required length.
Dim arr(q.Count - 1) As Integer
' CopyTo.
q.CopyTo(arr, 0)
' Display elements.
For Each element In arr
Console.WriteLine(element)
Next
End Sub
End Module
Output
5
10
20