Stack<(Of <(T>)>) the ToArray method. : Stack « Data Structure « VB.Net






Stack<(Of <(T>)>) the ToArray method.

  

'Use 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
'The ToArray method is used to create an array and copy the stack elements to it

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")

        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())

        Dim stack2 As New Stack(Of String)(numbers.ToArray())

        For Each number As String In stack2
            Console.WriteLine(number)
        Next

        Dim array2((numbers.Count * 2) - 1) As String
        numbers.CopyTo(array2, numbers.Count)

        Dim stack3 As New Stack(Of String)(array2)

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

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

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

   
    
  








Related examples in the same category

1.Push Integer into a StackPush Integer into a Stack
2.Simple Demo for Stack: Push, Pop and PeekSimple Demo for Stack: Push, Pop and Peek
3.Stack to ArrayStack to Array
4.Stack Demo: push, pop and peekStack Demo: push, pop and peek
5.Stack Class represents a simple last-in-first-out (LIFO) non-generic collection of objects.