VB.Net Tutorial/Collections/Queue

Материал из VB Эксперт
Версия от 12:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Enqueue, Dequeue and Peek

Imports System
 Imports System.Collections
 Class Tester
     Public Sub Run( )
     End Sub "Run
     Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
         Dim myEnumerator As IEnumerator = myCollection.GetEnumerator( )
         While myEnumerator.MoveNext( )
             Console.WriteLine("{0} ", myEnumerator.Current)
         End While
         Console.WriteLine( )
     End Sub "DisplayValues
     Shared Sub Main( )
         Dim intQueue As New Queue( )
         " populate the array
         Dim i As Integer
         For i = 0 To 4
             intQueue.Enqueue((i * 5))
         Next i
         " Display the Queue.
         Console.WriteLine("intQueue values:")
         DisplayValues(intQueue)
         " Remove an element from the queue.
         Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( ))
         " Display the Queue.
         Console.WriteLine("intQueue values:")
         DisplayValues(intQueue)
         " Remove another element from the queue.
         Console.WriteLine("(Dequeue) {0}", intQueue.Dequeue( ))
         " Display the Queue.
         Console.WriteLine("intQueue values:")
         DisplayValues(intQueue)
         " View the first element in the
         " Queue but do not remove.
         Console.WriteLine("(Peek)   {0}", intQueue.Peek( ))
         " Display the Queue.
         Console.WriteLine("intQueue values:")
         DisplayValues(intQueue)
     End Sub "Main
 End Class "Tester
intQueue values:
0
5
10
15
20
(Dequeue) 0
intQueue values:
5
10
15
20
(Dequeue) 5
intQueue values:
10
15
20
(Peek)   10
intQueue values:
10
15
20

Queue

Option Strict On
Imports System.Collections
Public Module modMain
   Public Sub Main()
       Dim waitingPassengers As New Queue
       
       Console.WriteLine(waitingPassengers.Count)
    
       waitingPassengers.Enqueue("asdf")
       Console.WriteLine(waitingPassengers.Peek())
       Console.WriteLine(waitingPassengers.Count)
       Console.WriteLine(waitingPassengers.Dequeue())
   End Sub
End Module
0
asdf
1
asdf

Queue.Enqueue and ToArray

Imports System.Collections
public class Test
   Private Shared m_Queue As New Queue
   public Shared Sub Main
        
        m_Queue.Enqueue("A")
        DisplayQueue()
        m_Queue.Enqueue("B")
        DisplayQueue()
        m_Queue.Enqueue("C")
        DisplayQueue()
        Dim txt As String = DirectCast(m_Queue.Dequeue(), String)
        Console.WriteLine(txt)
        DisplayQueue()
   End Sub
    Private Shared Sub DisplayQueue()
        For Each str As String In m_Queue.ToArray()
            Console.WriteLine(str)
        Next str
    End Sub
End class
A
A
B
A
B
C
A
B
C