Stack.Push 方法
将对象插入 Stack 的顶部。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overridable Sub Push ( _
obj As Object _
)
用法
Dim instance As Stack
Dim obj As Object
instance.Push(obj)
public virtual void Push (
Object obj
)
public:
virtual void Push (
Object^ obj
)
public void Push (
Object obj
)
public function Push (
obj : Object
)
参数
备注
将 Stack 实现为循环缓冲区。
如果 Count 已经等于容量,则在添加新元素之前,会通过自动重新分配内部数组并将现有元素复制到新数组中,来增大 Stack 的容量。
如果需要,可以将 空引用(在 Visual Basic 中为 Nothing) 推入到 Stack 上作为占位符。它占用堆栈中的一个槽,并且被当作任意对象一样处理。
如果 Count 小于堆栈的容量,则 Push 为 O(1) 操作。如果需要增加容量以容纳新元素,则 Push 成为 O(n) 操作,其中 n 为 Count。
示例
下面的示例演示如何向 Stack 中添加元素、如何从 Stack 中移除元素以及如何查看 Stack 顶部的元素。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesStack
Public Shared Sub Main()
' Creates and initializes a new Stack.
Dim myStack As New Stack()
myStack.Push("The")
myStack.Push("quick")
myStack.Push("brown")
myStack.Push("fox")
' Displays the Stack.
Console.Write("Stack values:")
PrintValues(myStack, ControlChars.Tab)
' Removes an element from the Stack.
Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
"{0}", myStack.Pop())
' Displays the Stack.
Console.Write("Stack values:")
PrintValues(myStack, ControlChars.Tab)
' Removes another element from the Stack.
Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
"{0}", myStack.Pop())
' Displays the Stack.
Console.Write("Stack values:")
PrintValues(myStack, ControlChars.Tab)
' Views the first element in the Stack but does not remove it.
Console.WriteLine("(Peek)" & ControlChars.Tab & ControlChars.Tab & _
"{0}", myStack.Peek())
' Displays the Stack.
Console.Write("Stack values:")
PrintValues(myStack, ControlChars.Tab)
End Sub
Public Shared Sub PrintValues(myCollection As IEnumerable, mySeparator As Char)
Dim obj As [Object]
For Each obj In myCollection
Console.Write("{0}{1}", mySeparator, obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues
End Class 'SamplesStack
' This code produces the following output.
'
' Stack values: fox brown quick The
' (Pop) fox
' Stack values: brown quick The
' (Pop) brown
' Stack values: quick The
' (Peek) quick
' Stack values: quick The
using System;
using System.Collections;
public class SamplesStack {
public static void Main() {
// Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push( "The" );
myStack.Push( "quick" );
myStack.Push( "brown" );
myStack.Push( "fox" );
// Displays the Stack.
Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Removes an element from the Stack.
Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
// Displays the Stack.
Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Removes another element from the Stack.
Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );
// Displays the Stack.
Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Views the first element in the Stack but does not remove it.
Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );
// Displays the Stack.
Console.Write( "Stack values:" );
PrintValues( myStack, '\t' );
}
public static void PrintValues( IEnumerable myCollection, char mySeparator ) {
foreach ( Object obj in myCollection )
Console.Write( "{0}{1}", mySeparator, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
Stack values: fox brown quick The
(Pop) fox
Stack values: brown quick The
(Pop) brown
Stack values: quick The
(Peek) quick
Stack values: quick The
*/
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection, char mySeparator );
int main()
{
// Creates and initializes a new Stack.
Stack^ myStack = gcnew Stack;
myStack->Push( "The" );
myStack->Push( "quick" );
myStack->Push( "brown" );
myStack->Push( "fox" );
// Displays the Stack.
Console::Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Removes an element from the Stack.
Console::WriteLine( "(Pop)\t\t{0}", myStack->Pop() );
// Displays the Stack.
Console::Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Removes another element from the Stack.
Console::WriteLine( "(Pop)\t\t{0}", myStack->Pop() );
// Displays the Stack.
Console::Write( "Stack values:" );
PrintValues( myStack, '\t' );
// Views the first element in the Stack but does not remove it.
Console::WriteLine( "(Peek)\t\t{0}", myStack->Peek() );
// Displays the Stack.
Console::Write( "Stack values:" );
PrintValues( myStack, '\t' );
}
void PrintValues( IEnumerable^ myCollection, char mySeparator )
{
IEnumerator^ myEnum = myCollection->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( "{0}{1}", mySeparator, obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
Stack values: fox brown quick The
(Pop) fox
Stack values: brown quick The
(Pop) brown
Stack values: quick The
(Peek) quick
Stack values: quick The
*/
import System.*;
import System.Collections.*;
public class SamplesStack
{
public static void main(String[] args)
{
// Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push("The");
myStack.Push("quick");
myStack.Push("brown");
myStack.Push("fox");
// Displays the Stack.
Console.Write("Stack values:");
PrintValues(myStack, '\t');
// Removes an element from the Stack.
Console.WriteLine("(Pop)\t\t{0}", myStack.Pop());
// Displays the Stack.
Console.Write("Stack values:");
PrintValues(myStack, '\t');
// Removes another element from the Stack.
Console.WriteLine("(Pop)\t\t{0}", myStack.Pop());
// Displays the Stack.
Console.Write("Stack values:");
PrintValues(myStack, '\t');
// Views the first element in the Stack but does not remove it.
Console.WriteLine("(Peek)\t\t{0}", myStack.Peek());
// Displays the Stack.
Console.Write("Stack values:");
PrintValues(myStack, '\t');
} //main
public static void PrintValues(IEnumerable myCollection, char mySeparator)
{
IEnumerator l_objmyEnum = myCollection.GetEnumerator();
while (l_objmyEnum.MoveNext()) {
Console.Write("{0}{1}",
System.Convert.ToString(mySeparator),
System.Convert.ToString(l_objmyEnum.get_Current()));
}
Console.WriteLine();
} //PrintValues
} //SamplesStack
/*
This code produces the following output.
Stack values: fox brown quick The
(Pop) fox
Stack values: brown quick The
(Pop) brown
Stack values: quick The
(Peek) quick
Stack values: quick The
*/
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0