📄 ex-09-16
字号:
// Example 09-16: Working with a Stack
namespace Programming_CSharp
{
using System;
using System.Collections;
// a simple class to store in the array
public class Tester
{
static void Main()
{
Stack intStack = new Stack();
// populate the array
for (int i = 0;i<8;i++)
{
intStack.Push(i*5);
}
// Display the Queue.
Console.Write( "intStack values:\t" );
PrintValues( intStack );
// Remove an element from the queue.
Console.WriteLine( "\n(Pop)\t{0}",
intStack.Pop() );
// Display the Queue.
Console.Write( "intStack values:\t" );
PrintValues( intStack );
// Remove another element from the queue.
Console.WriteLine( "\n(Pop)\t{0}",
intStack.Pop() );
// Display the Queue.
Console.Write( "intStack values:\t" );
PrintValues( intStack );
// View the first element in the
// Queue but do not remove.
Console.WriteLine( "\n(Peek) \t{0}",
intStack.Peek() );
// Display the Queue.
Console.Write( "intStack values:\t" );
PrintValues( intStack );
// declare an array object which will
// hold 12 integers
Array targetArray=Array.CreateInstance(
typeof(int), 12 );
targetArray.SetValue( 100, 0 );
targetArray.SetValue( 200, 1 );
targetArray.SetValue( 300, 2 );
targetArray.SetValue( 400, 3 );
targetArray.SetValue( 500, 4 );
targetArray.SetValue( 600, 5 );
targetArray.SetValue( 700, 6 );
targetArray.SetValue( 800, 7 );
targetArray.SetValue( 900, 8 );
// Display the values of the target Array instance.
Console.WriteLine( "\nTarget array: ");
PrintValues( targetArray );
// Copy the entire source Stack to the
// target Array instance, starting at index 6.
intStack.CopyTo( targetArray, 6 );
// Display the values of the target Array instance.
Console.WriteLine( "\nTarget array after copy: ");
PrintValues( targetArray );
// Copy the entire source Stack
// to a new standard array.
Object[] myArray = intStack.ToArray();
// Display the values of the new standard array.
Console.WriteLine( "\nThe new array:" );
PrintValues( myArray );
}
public static void PrintValues(
IEnumerable myCollection )
{
System.Collections.IEnumerator enumerator =
myCollection.GetEnumerator();
while ( enumerator.MoveNext() )
Console.Write( "{0} ",enumerator.Current );
Console.WriteLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -