当前位置: 首页>>代码示例>>C#>>正文


C# Stack.ToArray方法代码示例

本文整理汇总了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();
   }
}
开发者ID:.NET开发者,项目名称:System.Collections,代码行数:53,代码来源:Stack.ToArray

输出:

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();
       }
    }
开发者ID:C#程序员,项目名称:System.Collections,代码行数:41,代码来源:Stack.ToArray


注:本文中的System.Collections.Stack.ToArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。