Stack(Of T) Constructor (IEnumerable(Of T))
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Initializes a new instance of the Stack(Of T) class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
Assembly: System (in System.dll)
Parameters
- collection
- Type: System.Collections.Generic.IEnumerable(Of T)
The collection to copy elements from.
| Exception | Condition |
|---|---|
| ArgumentNullException | collection is Nothing. |
The capacity of a Stack(Of T) is the number of elements that the Stack(Of T) can hold. As elements are added to a Stack(Of T), the capacity is automatically increased as required by reallocating the internal array.
If the size of the collection can be estimated, specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the Stack(Of T).
The capacity can be decreased by calling TrimExcess.
The elements are copied onto the Stack(Of T) in the same order they are read by the IEnumerator(Of T) of the collection.
This constructor is an O(n) operation, where n is the number of elements in collection.
The following code example demonstrates this constructor and several methods of the Stack(Of T) generic class.
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. The Pop method is used to pop the first string off the stack. 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.
The ToArray method is used to create an array and copy the stack elements to it, then the array is passed to the Stack(Of T) constructor that takes IEnumerable(Of T), creating a copy of the stack with the order of the elements reversed. The elements of the copy are displayed.
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. The Stack(Of 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.
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.
Imports System.Collections.Generic Module Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 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 outputBlock.Text &= number & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "Popping '{0}'", numbers.Pop()) & vbCrLf outputBlock.Text &= String.Format("Peek at next item to pop: {0}", _ numbers.Peek()) & vbCrLf outputBlock.Text &= String.Format("Popping '{0}'", numbers.Pop()) & vbCrLf ' 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()) outputBlock.Text &= vbLf & "Contents of the first copy:" & vbCrLf For Each number As String In stack2 outputBlock.Text &= number & vbCrLf 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) outputBlock.Text &= String.Format(vbLf & _ "Contents of the second copy, with duplicates and nulls:") & vbCrLf For Each number As String In stack3 outputBlock.Text &= number & vbCrLf Next outputBlock.Text &= String.Format(vbLf & "stack2.Contains(""four"" & vbCrLf = {0}", _ stack2.Contains("four")) outputBlock.Text &= vbLf & "stack2.Clear()" & vbCrLf stack2.Clear() outputBlock.Text &= String.Format(vbLf & "stack2.Count = {0}", _ stack2.Count) & vbCrLf 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