Stack<T>.Push(T) Stack<T>.Push(T) Stack<T>.Push(T) Stack<T>.Push(T) Method

定義

Stack<T> の先頭にオブジェクトを挿入します。Inserts an object at the top of the Stack<T>.

public:
 void Push(T item);
public void Push (T item);
member this.Push : 'T -> unit
Public Sub Push (item As T)

パラメーター

item
T T T T

Stack<T> にプッシュするオブジェクト。The object to push onto the Stack<T>. 参照型の場合は null の値を使用できます。The value can be null for reference types.

次のコード例をいくつかの方法を示します、Stack<T>ジェネリック クラスなど、Pushメソッド。The following code example demonstrates several methods of the Stack<T> generic class, including the Push method.

コード例は、既定の容量を持つ文字列のスタックを作成し、使用して、 Push 5 つの文字列をスタックにプッシュするメソッド。The code example creates a stack of strings with default capacity and uses the Push method to push five strings onto the stack. スタックの要素が列挙されますが、スタックの状態は変化しません。The elements of the stack are enumerated, which does not change the state of the stack. Popメソッドを使用して、最初の文字列、スタックからポップします。The Pop method is used to pop the first string off the stack. Peekメソッドを使用して、スタックの次の項目を確認し、Popポップしてメソッドを使用します。The Peek method is used to look at the next item on the stack, and then the Pop method is used to pop it off.

ToArrayメソッドは、配列を作成して、スタック要素のコピーを使用し、配列に渡されます、Stack<T>を受け取るコンス トラクター IEnumerable<T>、逆に、要素の順序でスタックのコピーを作成します。The ToArray method is used to create an array and copy the stack elements to it, then the array is passed to the Stack<T> constructor that takes IEnumerable<T>, creating a copy of the stack with the order of the elements reversed. コピーの要素が表示されます。The elements of the copy are displayed.

配列 2 回、スタックのサイズを作成、およびCopyToメソッドを使用して、配列の真中で始まる配列の要素をコピーします。An array twice the size of the stack is created, and the CopyTo method is used to copy the array elements beginning at the middle of the array. Stack<T>は元に戻すの要素の順序でスタックのコピーを作成するコンス トラクターをもう一度使用されます。 そのため、3 つの null 要素が最後には。The Stack<T> constructor is used again to create a copy of the stack with the order of elements reversed; thus, the three null elements are at the end.

Contains 「4」文字列がその後が、スタックの最初のコピーのことを示すメソッドを使用、Clearメソッドは、コピーをクリアし、Countプロパティがスタックが空であることを示しています。The Contains method is used to show that the string "four" is in the first copy of the stack, after which the Clear method clears the copy and the Count property shows that the stack is empty.

using System;
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Stack<string> numbers = new Stack<string>();
        numbers.Push("one");
        numbers.Push("two");
        numbers.Push("three");
        numbers.Push("four");
        numbers.Push("five");

        // A stack can be enumerated without disturbing its contents.
        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nPopping '{0}'", numbers.Pop());
        Console.WriteLine("Peek at next item to destack: {0}", 
            numbers.Peek());
        Console.WriteLine("Popping '{0}'", numbers.Pop());

        // Create a copy of the stack, using the ToArray method and the
        // constructor that accepts an IEnumerable<T>.
        Stack<string> stack2 = new Stack<string>(numbers.ToArray());

        Console.WriteLine("\nContents of the first copy:");
        foreach( string number in stack2 )
        {
            Console.WriteLine(number);
        }
        
        // Create an array twice the size of the stack and copy the
        // elements of the stack, starting at the middle of the 
        // array. 
        string[] array2 = new string[numbers.Count * 2];
        numbers.CopyTo(array2, numbers.Count);
        
        // Create a second stack, using the constructor that accepts an
        // IEnumerable(Of T).
        Stack<string> stack3 = new Stack<string>(array2);

        Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
        foreach( string number in stack3 )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine("\nstack2.Contains(\"four\") = {0}", 
            stack2.Contains("four"));

        Console.WriteLine("\nstack2.Clear()");
        stack2.Clear();
        Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
    }
}

/* This code example produces the following output:

five
four
three
two
one

Popping 'five'
Peek at next item to destack: four
Popping 'four'

Contents of the first copy:
one
two
three

Contents of the second copy, with duplicates and nulls:
one
two
three




stack2.Contains("four") = False

stack2.Clear()

stack2.Count = 0
 */
Imports System
Imports System.Collections.Generic

Module Example

    Sub Main

        Dim numbers As New Stack(Of String)
        numbers.Push("one")
        numbers.Push("two")
        numbers.Push("three")
        numbers.Push("four")
        numbers.Push("five")

        ' A stack can be enumerated without disturbing its contents.
        For Each number As String In numbers
            Console.WriteLine(number)
        Next

        Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop())
        Console.WriteLine("Peek at next item to pop: {0}", _
            numbers.Peek())    
        Console.WriteLine("Popping '{0}'", numbers.Pop())

        ' Create another stack, using the ToArray method and the
        ' constructor that accepts an IEnumerable(Of T). Note that
        ' the order of items on the new stack is reversed.
        Dim stack2 As New Stack(Of String)(numbers.ToArray())

        Console.WriteLine(vbLf & "Contents of the first copy:")
        For Each number As String In stack2
            Console.WriteLine(number)
        Next
        
        ' Create an array twice the size of the stack, compensating
        ' for the fact that Visual Basic allocates an extra array 
        ' element. Copy the elements of the stack, starting at the
        ' middle of the array. 
        Dim array2((numbers.Count * 2) - 1) As String
        numbers.CopyTo(array2, numbers.Count)
        
        ' Create a second stack, using the constructor that accepts an
        ' IEnumerable(Of T). The elements are reversed, with the null
        ' elements appearing at the end of the stack when enumerated.
        Dim stack3 As New Stack(Of String)(array2)

        Console.WriteLine(vbLf & _
            "Contents of the second copy, with duplicates and nulls:")
        For Each number As String In stack3
            Console.WriteLine(number)
        Next

        Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _
            stack2.Contains("four"))

        Console.WriteLine(vbLf & "stack2.Clear()")
        stack2.Clear()
        Console.WriteLine(vbLf & "stack2.Count = {0}", _
            stack2.Count)
    End Sub
End Module

' This code example produces the following output:
'
'five
'four
'three
'two
'one
'
'Popping 'five'
'Peek at next item to pop: four
'Popping 'four'
'
'Contents of the first copy:
'one
'two
'three
'
'Contents of the second copy, with duplicates and nulls:
'one
'two
'three
'
'
'
'
'stack2.Contains("four") = False
'
'stack2.Clear()
'
'stack2.Count = 0

注釈

Stack<T> 配列として実装されます。Stack<T> is implemented as an array.

場合Count既に、容量の容量に等しい、Stack<T>は内部の配列を自動的に再割り当てによって向上し、新しい要素を追加する前に、既存の要素が新しい配列にコピーされます。If Count already equals the capacity, the capacity of the Stack<T> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

T が参照型の場合、必要に応じて null をプレースホルダーとして Stack<T> にプッシュすることができます。If type T is a reference type, null can be pushed onto the Stack<T> as a placeholder, if needed. スタックのスロットを占有し、任意のオブジェクトと同様に扱われます。It occupies a slot in the stack and is treated like any object.

場合Countがスタックの容量より小さいPushは o (1) 操作です。If Count is less than the capacity of the stack, Push is an O(1) operation. 容量は、新しい要素を格納するために必要がある場合PushO になります (n) 操作、nCountします。If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count.

適用対象

こちらもご覧ください