C# ArrayList CopyTo(Array, Int32)
Description
ArrayList CopyTo(Array, Int32)
copies the entire ArrayList
to a compatible one-dimensional Array, starting at the specified index
of the target array.
Syntax
ArrayList.CopyTo(Array, Int32)
has the following syntax.
public virtual void CopyTo(
Array array,
int arrayIndex
)
Parameters
ArrayList.CopyTo(Array, Int32)
has the following parameters.
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.
Returns
ArrayList.CopyTo(Array, Int32)
method returns
Example
The following code example shows how to copy an ArrayList into a one-dimensional System.Array.
//w ww. j ava2 s .c o 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.