VB.Net Tutorial/Collections/Stack
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Convert and Copy Elements in a Stack to an Array
Option Strict On
Imports System
Imports System.Collections
Class Tester
Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim o As Object
For Each o In myCollection
Console.WriteLine(o)
Next o
End Sub
Shared Sub Main( )
Dim intStack As New Stack( )
Dim i As Integer
For i = 1 To 4
intStack.Push((i * 5))
Next i
Console.WriteLine("intStack values:")
DisplayValues(intStack)
Const arraySize As Integer = 10
Dim testArray(arraySize) As Integer
For i = 1 To arraySize - 1
testArray(i) = i * 100
Next i
Console.WriteLine("Contents of the test array")
DisplayValues(testArray)
intStack.CopyTo(testArray, 3)
Console.WriteLine("TestArray after copy: ")
DisplayValues(testArray)
Dim myArray As Object( ) = intStack.ToArray( )
Console.WriteLine("The new array:")
DisplayValues(myArray)
End Sub
End Class
intStack values: 20 15 10 5 Contents of the test array 0 100 200 300 400 500 600 700 800 900 0 TestArray after copy: 0 100 200 20 15 10 5 700 800 900 0 The new array: 20 15 10 5
IsSynchronized
Imports System.Threading
Imports System.Collections
public class Test
public Shared Sub Main
Dim S1 As New Stack()
Dim SyncS1 As Stack = Stack.Synchronized(S1)
Console.WriteLine("SyncS1: " & SyncS1.IsSynchronized.ToString())
Console.WriteLine("S1: " & S1.IsSynchronized.ToString())
End Sub
End class
SyncS1: True S1: False
Stack
Imports System.Collections
public class Test
public Shared Sub Main
Dim txt As String = "asdf"
Dim letter_stack As New Stack
For i As Integer = 0 To txt.Length - 1
Console.WriteLine(i)
letter_stack.Push(txt.Substring(i, 1))
Next
Do While letter_stack.Count > 0
Console.WriteLine(DirectCast(letter_stack.Pop(), String))
Loop
End Sub
End class
0 1 2 3 f d s a
Stack: Push, Pop and Peek
Option Strict On
Imports System
Imports System.Collections
Class Tester
Public Shared Sub DisplayValues(ByVal myCollection As IEnumerable)
Dim o As Object
For Each o In myCollection
Console.WriteLine(o)
Next o
End Sub "DisplayValues
Shared Sub Main( )
Dim intStack As New Stack( )
" populate the stack
Dim i As Integer
For i = 0 To 7
intStack.Push((i * 5))
Next i
" Display the Stack.
Console.WriteLine("intStack values:")
DisplayValues(intStack)
" Remove an element from the stack.
Console.WriteLine("(Pop){0}", intStack.Pop( ))
" Display the Stack.
Console.WriteLine("intStack values:")
DisplayValues(intStack)
" Remove another element from the stack.
Console.WriteLine("(Pop){0}", intStack.Pop( ))
" Display the Stack.
Console.WriteLine("intStack values:")
DisplayValues(intStack)
" View the first element in the
" Stack but do not remove.
Console.WriteLine("(Peek) {0}", intStack.Peek( ))
" Display the Stack.
Console.WriteLine("intStack values:")
DisplayValues(intStack)
End Sub "Main
End Class "Tester
intStack values: 35 30 25 20 15 10 5 0 (Pop)35 intStack values: 30 25 20 15 10 5 0 (Pop)30 intStack values: 25 20 15 10 5 0 (Peek) 25 intStack values: 25 20 15 10 5 0