DekGenius.com
Team LiB   Previous Section   Next Section

16.5 Copying from a Collection Type to an Array

The ArrayList, Queue, and Stack types contain overloaded CopyTo() and ToArray() methods for copying their elements to an array. The CopyTo() method copies its elements to an existing one-dimensional array, overwriting the contents of the array beginning at the index you specify. The ToArray() method returns a new array with the contents of the type's elements.

For example, in the case of a stack, ToArray() returns a new array containing the elements in the stack, and CopyTo() copies the stack over a pre-existing array. Example 16-5 modifies Example 16-4 to demonstrate both methods. The listing is followed by a complete analysis.

Example 16-5. Copying from a Stack type to an array
using System;
using System.Collections;

namespace StackDemo
{
   class Tester
   {
      public void Run()
      {
          Stack intStack = new Stack();

          // populate the array
          for (int i = 1;i<5;i++)
          {
              intStack.Push(i*5);
          }

          // Display the Stack.
          Console.WriteLine( "intStack values:" );
          DisplayValues( intStack );


          const int arraySize = 10;
          int[] testArray = new int[arraySize];

          // populate the array
          for (int i = 1; i < arraySize; i++)
          {
              testArray[i] = i * 100;
          }
          Console.WriteLine("\nContents of the test array");
          DisplayValues( testArray );

          // Copy the intStack into the new array, start offset 3
          intStack.CopyTo( testArray, 3 );
          Console.WriteLine( "\nTestArray after copy:  ");
          DisplayValues( testArray );


          // 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:" );
          DisplayValues( myArray );

      }
       public static void DisplayValues( 
           IEnumerable myCollection )  
       {
           foreach (object o in myCollection)
           {
               Console.WriteLine(o);
           }
       }

      [STAThread]
      static void Main()
      {
         Tester t = new Tester();
         t.Run();
      }
   }
}


Output:
intStack values:
20
15
10
5

Contents of the test array
0
100
200
300
400
500
600
700
800
900

TestArray after copy:
0
100
200
20
15
10
5
700
800
900

The new array:
20
15
10
5

You begin again by creating the stack (intStack), populating it with integers and displaying its contents using WriteLine():

Stack intStack = new Stack();

for (int i = 1;i<5;i++)
{
    intStack.Push(i*5);
}

Console.WriteLine( "intStack values:" );
DisplayValues( intStack );

Next create an array and populate the array and display its values:

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

You are ready to copy the stack over the array. You do so with the CopyTo() method, passing in the array name and the offset at which to begin the copy:

intStack.CopyTo( testArray, 3 );

This copies the four values from the stack over the array, starting at offset 3 (the fourth element in the array).

0
100
200
20
15
10
5
700
800
900

Rather than copying to an existing array, you are free to copy to a new array using the ToArray() method, which generates a properly sized new array to hold the contents of the stack:

Object[] myArray = intStack.ToArray();
    Team LiB   Previous Section   Next Section