Open In App

C# | Copying the entire ArrayList to 1-D Array starting at the specified index

Last Updated : 23 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
ArrayList.CopyTo(Array, Int32) Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax:
public virtual void CopyTo (Array array, int arrayIndex);
Parameters:
array: It is the one-dimensional Array that is the destination of the elements copied from ArrayList. The Array must have zero-based indexing. arrayIndex: It is the zero-based index in array at which copying begins.
Exceptions:
  • ArgumentNullException: If the array is null.
  • ArgumentOutOfRangeException: If the arrayIndex is less than zero.
  • ArgumentException: If the array is multidimensional OR the number of elements in the source ArrayList is greater than the number of elements that the destination array can contain.
  • InvalidCastException: If the type of the source ArrayList cannot be cast automatically to the type of the destination array.
Below programs illustrate the use of above-discussed method: Example 1: CSharp
// C# code to illustrate the
// ArrayList.CopyTo(Array, Int32)
// Method
using System;
using System.Collections;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating an ArrayList
        ArrayList myList = new ArrayList();

        // Adding elements to ArrayList
        myList.Add("A");
        myList.Add("B");
        myList.Add("C");
        myList.Add("D");

        // Creates and initializes the
        // one-dimensional target Array.
        String[] arr = new String[6];

        // adding elements to Array
        arr[0] = "HTML";
        arr[1] = "PHP";
        arr[2] = "Java";
        arr[3] = "Python";
        arr[4] = "C#";
        arr[5] = "OS";

        Console.WriteLine("Before Method: ");

        Console.WriteLine("\nArrayList Contains: ");

        // Displaying the elements in myList
        for (int i = 0; i < myList.Count; i++) {
            Console.WriteLine("myList[{0}] : {1}", i, myList[i]);
        }

        Console.WriteLine("\nArray Contains: ");

        // Displaying the elements in arr
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
        }

        Console.WriteLine("After Method: ");

        // Copying the entire source ArrayList
        // to the target Array starting at
        // index 2.
        myList.CopyTo(arr, 2);

        Console.WriteLine("\nArrayList Contains: ");

        // Displaying the elements in myList
        for (int i = 0; i < myList.Count; i++) {
            Console.WriteLine("myList[{0}] : {1}", i, myList[i]);
        }

        Console.WriteLine("\nArray Contains: ");

        // Displaying the elements in arr
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
        }
    }
}
Output:
Before Method: 

ArrayList Contains: 
myList[0] : A
myList[1] : B
myList[2] : C
myList[3] : D

Array Contains: 
arr[0] : HTML
arr[1] : PHP
arr[2] : Java
arr[3] : Python
arr[4] : C#
arr[5] : OS
After Method: 

ArrayList Contains: 
myList[0] : A
myList[1] : B
myList[2] : C
myList[3] : D

Array Contains: 
arr[0] : HTML
arr[1] : PHP
arr[2] : A
arr[3] : B
arr[4] : C
arr[5] : D
Example 2: CSharp
// C# code to illustrate the
// ArrayList.CopyTo(Array, Int32)
// Method
using System;
using System.Collections;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating an ArrayList
        ArrayList myList = new ArrayList();

        // Adding elements to ArrayList
        myList.Add("A");
        myList.Add("B");
        myList.Add("C");
        myList.Add("D");

        // Creates and initializes the
        // one-dimensional target Array.
        // Here array size is only 2 i.e
        // it can hold only 3 elements.
        String[] arr = new String[2];

        Console.WriteLine("Before Method: ");

        Console.WriteLine("\nArrayList Contains: ");

        // Displaying the elements in myList
        for (int i = 0; i < myList.Count; i++) {
            Console.WriteLine("myList[{0}] : {1}", i, myList[i]);
        }

        Console.WriteLine("\nArray Contains: ");

        // Displaying the elements in arr
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
        }

        Console.WriteLine("After Method: ");

        // using Method but It will give
        // Runtime Error as number of elements
        // in the source ArrayList is greater
        // than the number of elements that
        // the destination array can contain
        myList.CopyTo(arr, 2);

        Console.WriteLine("\nArrayList Contains: ");

        // Displaying the elements in myList
        for (int i = 0; i < myList.Count; i++) {
            Console.WriteLine("myList[{0}] : {1}", i, myList[i]);
        }

        Console.WriteLine("\nArray Contains: ");

        // Displaying the elements in arr
        for (int i = 0; i < arr.Length; i++) {
            Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
        }
    }
}
Runtime Error:
Unhandled Exception: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds Parameter name: destinationArray
Reference:

Next Article

Similar Reads