Stack.Push メソッド
Stack の先頭にオブジェクトを挿入します。
Public Overridable Sub Push( _
ByVal obj As Object _)
[C#]
public virtual void Push(objectobj);
[C++]
public: virtual void Push(Object* obj);
[JScript]
public function Push(
obj : Object);
パラメータ
解説
Stack は、循環バッファとして実装されます。
Count がスタックの容量よりも小さい場合、 Push は O(1) 操作になります。新しい要素を格納するために容量を増やす必要がある場合、 Push は O(n) 操作になります。ここで、 n は Count です。
必要に応じて、 null 参照 (Visual Basic では Nothing) をプレースホルダとして Stack にプッシュできます。このプレースホルダは、スタックのスロットを 1 つ占有し、他のオブジェクトと同様に扱われます。
使用例
[Visual Basic, C#, C++] 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 myEnumerator As System.Collections.IEnumerator = _
myCollection.GetEnumerator()
While myEnumerator.MoveNext()
Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' 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
[C#]
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 ) {
System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
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
*/
[C++]
#using <mscorlib.dll>
#using <system.dll>
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable* myCollection, Char mySeparator ) {
System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
while ( myEnumerator->MoveNext() )
Console::Write( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
Console::WriteLine();
}
int main() {
// Creates and initializes a new Stack.
Stack* myStack = new Stack();
myStack->Push( S"The" );
myStack->Push( S"quick" );
myStack->Push( S"brown" );
myStack->Push( S"fox" );
// Displays the Stack.
Console::Write( S"Stack values:" );
PrintValues( myStack, '\t' );
// Removes an element from the Stack.
Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );
// Displays the Stack.
Console::Write( S"Stack values:" );
PrintValues( myStack, '\t' );
// Removes another element from the Stack.
Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );
// Displays the Stack.
Console::Write( S"Stack values:" );
PrintValues( myStack, '\t' );
// Views the first element in the Stack but does not remove it.
Console::WriteLine( S"(Peek)\t\t{0}", myStack->Peek() );
// Displays the Stack.
Console::Write( S"Stack values:" );
PrintValues( myStack, '\t' );
}
/*
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
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
Stack クラス | Stack メンバ | System.Collections 名前空間 | Peek | Pop