📌  相关文章
📜  C#|从指定的索引开始将整个ArrayList复制到1-D Array

📅  最后修改于: 2021-05-29 16:21:37             🧑  作者: Mango

ArrayList.CopyTo(Array,Int32)方法用于从目标数组的指定索引处开始,将整个ArrayList复制到兼容的一维Array。

句法:

public virtual void CopyTo (Array array, int arrayIndex);

参数:

例外情况:

  • ArgumentNullException:如果数组为null。
  • ArgumentOutOfRangeException:如果arrayIndex小于零。
  • ArgumentException:如果数组是多维数组,或者源ArrayList中的元素数大于目标数组可以包含的元素数。
  • InvalidCastException:如果无法将源ArrayList的类型强制转换为目标array的类型。

下面的程序说明了上面讨论的方法的使用:

范例1:

// 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]);
        }
    }
}

输出:

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

范例2:

// 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]);
        }
    }
}

运行时错误:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.copyto?view=netframework-4.7.2#System_Collections_ArrayList_CopyTo_System_Array_System_Int32_