Stack 类的属性和方法
下表列出了Stack类的一些常用属性-
下表列出了Stack类的一些常用方法--
方法 |
描述 |
Public Overridable Sub Clear |
从堆栈中删除所有元素。 |
Public Overridable Function Contains (obj As Object) As Boolean |
确定元素是否在堆栈中。 |
Public Overridable Function Peek As Object |
返回位于堆栈顶部的对象,但不删除它。 |
Public Overridable Function Pop As Object |
删除并返回堆栈顶部的对象。 |
Public Overridable Sub Push (obj As Object) |
在堆栈顶部插入一个对象。 |
Public Overridable Function ToArray As Object() |
将堆栈复制到新阵列。 |
示例:
Imports System.Collections
Module collections
Sub Main()
Dim st As Stack = New Stack()
st.Push("A")
st.Push("M")
st.Push("G")
st.Push("W")
Console.WriteLine("Current stack: ")
Dim c As Char
For Each c In st
Console.Write(c + " ")
Next c
Console.WriteLine()
st.Push("V")
st.Push("H")
Console.WriteLine("The next poppable value in stack: {0}", st.Peek())
Console.WriteLine("Current stack: ")
For Each c In st
Console.Write(c + " ")
Next c
Console.WriteLine()
Console.WriteLine("Removing values ")
st.Pop()
st.Pop()
st.Pop()
Console.WriteLine("Current stack: ")
For Each c In st
Console.Write(c + " ")
Next c
Console.ReadKey()
End Sub
End Module
尝试一下
编译并执行上述代码后,将产生以下结果-
Current stack:
W G M A
The next poppable value in stack: H
Current stack:
H V W G M A
Removing values
Current stack:
G M A