C# ArrayList CopyTo(Int32, Array, Int32, Int32)

Description

ArrayList CopyTo(Int32, Array, Int32, Int32) copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.

Syntax

ArrayList.CopyTo(Int32, Array, Int32, Int32) has the following syntax.


public virtual void CopyTo(
  int index,//  w w w.j  a  va  2  s .  c  om
  Array array,
  int arrayIndex,
  int count
)

Parameters

ArrayList.CopyTo(Int32, Array, Int32, Int32) has the following parameters.

  • index - The zero-based index in the source ArrayList at which copying begins.
  • array - The one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing.
  • arrayIndex - The zero-based index in array at which copying begins.
  • count - The number of elements to copy.

Returns

ArrayList.CopyTo(Int32, Array, Int32, Int32) method returns

Example

The following code example shows how to copy an ArrayList into a one-dimensional System.Array.


//w ww .  ja v  a2s  .  co m

using System;
using System.Collections;
public class SamplesArrayList  {

    public static void Main()  {
       ArrayList mySourceList = new ArrayList();
       mySourceList.Add( "A" );
       mySourceList.Add( "B" );
       mySourceList.Add( "C" );
       mySourceList.Add( "D" );
       mySourceList.Add( "E" );
       mySourceList.Add( "F" );

       String[] myTargetArray = new String[15];
       myTargetArray[0] = "0";
       myTargetArray[1] = "1";
       myTargetArray[2] = "2";
       myTargetArray[3] = "3";
       myTargetArray[4] = "4";
       myTargetArray[5] = "5";
       myTargetArray[6] = "6";
       myTargetArray[7] = "7";
       myTargetArray[8] = "8";

       // Copies the second element from the source ArrayList to the target Array starting at index 7.
       mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
       // Copies the entire source ArrayList to the target Array starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );

       // Copies the entire source ArrayList to the target Array starting at index 0.
       mySourceList.CopyTo( myTargetArray );

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack