Mega Code Archive

 
Categories / VB.Net / Data Structure
 

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