本文整理汇总了C#中System.Collections.Stack.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.ToArray方法的具体用法?C# Stack.ToArray怎么用?C# Stack.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Stack
的用法示例。
在下文中一共展示了Stack.ToArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections;
public class SamplesStack {
public static void Main() {
// Creates and initializes the source Stack.
Stack mySourceQ = new Stack();
mySourceQ.Push( "barn" );
mySourceQ.Push( "the" );
mySourceQ.Push( "in" );
mySourceQ.Push( "cats" );
mySourceQ.Push( "napping" );
mySourceQ.Push( "three" );
// Creates and initializes the one-dimensional target Array.
Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
myTargetArray.SetValue( "The", 0 );
myTargetArray.SetValue( "quick", 1 );
myTargetArray.SetValue( "brown", 2 );
myTargetArray.SetValue( "fox", 3 );
myTargetArray.SetValue( "jumps", 4 );
myTargetArray.SetValue( "over", 5 );
myTargetArray.SetValue( "the", 6 );
myTargetArray.SetValue( "lazy", 7 );
myTargetArray.SetValue( "dog", 8 );
// Displays the values of the target Array.
Console.WriteLine( "The target Array contains the following (before and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the entire source Stack to the target Array, starting at index 6.
mySourceQ.CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source Stack to a new standard array.
Object[] myStandardArray = mySourceQ.ToArray();
// Displays the values of the new standard array.
Console.WriteLine( "The new standard array contains the following:" );
PrintValues( myStandardArray, ' ' );
}
public static void PrintValues( Array myArr, char mySeparator ) {
foreach ( Object myObj in myArr ) {
Console.Write( "{0}{1}", mySeparator, myObj );
}
Console.WriteLine();
}
}
输出:
The target Array contains the following (before and after copying): The quick brown fox jumps over the lazy dog The quick brown fox jumps over three napping cats in the barn The new standard array contains the following: three napping cats in the barn
示例2: Stack.ToArray()
//引入命名空间
using System;
using System.Collections;
public class TesterStackArray
{
public void Run()
{
Stack intStack = new Stack();
for (int i = 1;i<5;i++){
intStack.Push(i*5);
}
const int arraySize = 10;
int[] testArray = new int[arraySize];
for (int i = 1; i < arraySize; i++)
{
testArray[i] = i * 100;
}
Console.WriteLine("\nContents of the test array");
DisplayValues( testArray );
intStack.CopyTo( testArray, 3 );
Console.WriteLine( "\nTestArray after copy: ");
DisplayValues( testArray );
Object[] myArray = intStack.ToArray();
Console.WriteLine( "\nThe new array:" );
DisplayValues( myArray );
}
public static void DisplayValues(IEnumerable myCollection ){
foreach (object o in myCollection){
Console.WriteLine(o);
}
}
static void Main()
{
TesterStackArray t = new TesterStackArray();
t.Run();
}
}