Stack コンストラクタとは? わかりやすく解説

Stack コンストラクタ (Int32)

空で、指定した初期量または既定初期量のうち大きい方の初期量を備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    initialCapacity As Integer _
)
Dim initialCapacity As Integer

Dim instance As New Stack(initialCapacity)
public Stack (
    int initialCapacity
)
public:
Stack (
    int initialCapacity
)
public Stack (
    int initialCapacity
)
public function Stack (
    initialCapacity : int
)

パラメータ

initialCapacity

Stack格納できる要素数の初期値

例外例外
例外種類条件

ArgumentOutOfRangeException

initialCapacity が 0 未満です。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Stack コンストラクタ (ICollection)

指定したコレクションからコピーした要素格納しコピーされる要素の数と同じ初期量を備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    col As ICollection _
)
public Stack (
    ICollection col
)
public:
Stack (
    ICollection^ col
)
public Stack (
    ICollection col
)
public function Stack (
    col : ICollection
)

パラメータ

col

要素コピー元の ICollection。

例外例外
例外種類条件

ArgumentNullException

colnull 参照 (Visual Basic では Nothing) です。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Stack コンストラクタ (ジェネリック IEnumerable)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

指定したコレクションからコピーした要素格納しコピーされる要素の数を格納できるだけの容量備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Public Sub New ( _
    collection As IEnumerable(Of T) _
)
public Stack (
    IEnumerable<T> collection
)
public:
Stack (
    IEnumerable<T>^ collection
)
public Stack (
    IEnumerable<T> collection
)
public function Stack (
    collection : IEnumerable<T>
)

パラメータ

collection

要素コピー元のコレクション

例外例外
例外種類条件

ArgumentNullException

collectionnull 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

このコンストラクタおよび Stack ジェネリック クラスいくつかのメソッド使用したコード例次に示します

このコード例では、既定容量文字列スタック作成しPush メソッド使用して 5 つ文字列スタックプッシュます。スタック要素列挙されますが、スタックの状態は変化しません。Pop メソッド使用して最初文字列スタックからポップます。Peek メソッド使用してスタック内の次の項目を調べてから、Pop メソッド使用してその項目をスタックからポップます。

ToArray メソッド使用して配列作成し、その配列スタック要素コピーしてから、IEnumerable を受け取Stack コンストラクタ配列渡して要素順序反転したスタックコピー作成しますコピー要素表示されます。

スタックの 2 倍のサイズ配列作成されCopyTo メソッド使用して配列中央部で始まる配列要素コピーしますStack コンストラクタ再度使用して要素順序反転したスタックコピー作成しますその結果3 つの null 要素末尾配置されます。

Contains メソッド使用して文字列 "four" がスタック最初コピーにあることを示しますその後で、Clear メソッドコピークリアすると、Count プロパティによってスタックが空であることが示されます。

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
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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Stack コンストラクタ (Int32)

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

空で、指定した初期量または既定初期量のうち大きい方の初期量を備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

capacity が 0 未満です。

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Stack コンストラクタ


Stack コンストラクタ


Stack コンストラクタ ()

空で、既定初期量を備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Stack コンストラクタ ()

メモ : このコンストラクタは、.NET Framework version 2.0新しく追加されたものです。

空で、既定初期量を備えたStack クラス新しインスタンス初期化します。

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

解説解説
使用例使用例

このコンストラクタおよび Stack ジェネリック クラスいくつかのメソッド使用したコード例次に示します

このコード例では、既定容量文字列スタック作成しPush メソッド使用して 5 つ文字列スタックプッシュます。スタック要素列挙されますが、スタックの状態は変化しません。Pop メソッド使用して最初文字列スタックからポップます。Peek メソッド使用してスタック内の次の項目を調べてから、Pop メソッド使用してその項目をスタックからポップます。

ToArray メソッド使用して配列作成し、その配列スタック要素コピーしてから、IEnumerable を受け取Stack コンストラクタ配列渡して要素順序反転したスタックコピー作成しますコピー要素表示されます。

スタックの 2 倍のサイズ配列作成されCopyTo メソッド使用して配列中央部で始まる配列要素コピーしますStack コンストラクタ再度使用して要素順序反転したスタックコピー作成しますその結果3 つの null 要素末尾配置されます。

Contains メソッド使用して文字列 "four" がスタック最初コピーにあることを示しますその後で、Clear メソッドコピークリアすると、Count プロパティによってスタックが空であることが示されます。

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
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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Stack コンストラクタ」の関連用語

Stack コンストラクタのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Stack コンストラクタのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS