C#如何将整个ArrayList复制到一维数组

ArrayList.CopyTo方法用于复制整个数组列表到兼容的一维数组, 从目标数组的开头开始。
语法如下:

public virtual void CopyTo (Array array);

这里, Array是一维Array, 它是从ArrayList复制的元素的目的地。数组必须具有从零开始的索引。
例外情况:
  • ArgumentNullException:如果Array一片空白。
  • ArgumentException:如果Array是多维的OR源ArrayList中的元素数大于目标ArrayList中的元素数Array可以包含。
  • InvalidCastException:如果源ArrayList的类型不能自动转换为目标的类型Array.
下面的程序说明了上述方法的使用:
示例1:
//C# code to illustrate the //ArrayList.CopyTo 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" ); myList.Add( "E" ); myList.Add( "F" ); myList.Add( "G" ); myList.Add( "H" ); //Creates and initializes the //one-dimensional target Array. String[] arr = new String[9]; arr[0] = "C" ; arr[1] = "C++" ; arr[2] = "Java" ; arr[3] = "Python" ; arr[4] = "C#" ; arr[5] = "HTML" ; arr[6] = "CSS" ; arr[7] = "PHP" ; arr[8] = "DBMS" ; Console.WriteLine( "Before CopyTo Method: " ); Console.WriteLine( "\nArrayList Contains: " ); //printing ArrayList elements foreach (Object obj in myList) Console.WriteLine( "{0}" , obj); Console.WriteLine( "\nString Array Contains: " ); //printing String elements foreach (Object obj1 in arr) Console.WriteLine( "{0}" , obj1); Console.WriteLine( "After CopyTo Method: " ); //using CopyTo Method to copy //the entire source ArrayList //to the target Array starting //at index 0 myList.CopyTo(arr); Console.WriteLine( "\nArrayList Contains: " ); //printing ArrayList elements foreach (Object obj in myList) Console.WriteLine( "{0}" , obj); Console.WriteLine( "\nString Array Contains: " ); //printing String elements foreach (Object obj1 in arr) Console.WriteLine( "{0}" , obj1); } }

输出如下:
Before CopyTo Method: ArrayList Contains: ABCDEFGHString Array Contains: CC++JavaPythonC#HTMLCSSPHPDBMSAfter CopyTo Method: ArrayList Contains: ABCDEFGHString Array Contains: ABCDEFGHDBMS

示例2:
//C# code to illustrate the //ArrayList.CopyTo 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( "HTML" ); myList.Add( "CSS" ); myList.Add( "PHP" ); myList.Add( "DBMS" ); //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 CopyTo Method: " ); Console.WriteLine( "\nArrayList Contains: " ); //printing ArrayList elements foreach (Object obj in myList) Console.WriteLine( "{0}" , obj); Console.WriteLine( "\nString Array Contains: " ); //printing String elements foreach (Object obj1 in arr) Console.WriteLine( "{0}" , obj1); Console.WriteLine( "After CopyTo Method: " ); //using CopyTo 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); Console.WriteLine( "\nArrayList Contains: " ); //printing ArrayList elements foreach (Object obj in myList) Console.WriteLine( "{0}" , obj); Console.WriteLine( "\nString Array Contains: " ); //printing String elements foreach (Object obj1 in arr) Console.WriteLine( "{0}" , obj1); } }

运行时错误:
未处理的异常:System.ArgumentException:目标数组不够长。检查destIndex和length, 以及数组的下限参数名称:destinationArray
【C#如何将整个ArrayList复制到一维数组】参考:
  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.copyto?view=netframework-4.7.2#System_Collections_ArrayList_CopyTo_System_Array_

    推荐阅读