在 C# 中将整个 ArrayList 复制到从指定索引开始的一维数组

csharpserver side programmingprogramming更新于 2024/10/7 2:21:00

要将整个 ArrayList 复制到从指定索引开始的一维数组,代码如下 −

示例

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      ArrayList list = new ArrayList();
      list.Add("PQ");
      list.Add("RS");
      list.Add("TU");
      list.Add("UV");
      list.Add("WX");
      list.Add("YZ");
      Console.WriteLine("ArrayList elements...");
      for (int i = 0; i < list.Count; i++) {
         Console.WriteLine(list[i]);
      }
      String[] strArr = new String[6] {"One", "Two", "Three", "Four", "Five", "Six"};
      Console.WriteLine("
Array elements...");       for (int i = 0; i < strArr.Length; i++) {          Console.WriteLine(strArr[i]);       }       list.CopyTo(strArr, 0);       Console.WriteLine("
Array elements (updated)...");       for (int i = 0; i < strArr.Length; i++) {          Console.WriteLine(strArr[i]);       }    } }

输出

这将产生以下输出 −

ArrayList elements...
PQ
RS
TU
UV
WX
YZ

数组元素...
One
Two
Three
Four
Five
Six

Array elements (updated)...
PQ
RS
TU
UV
WX
YZ

示例

现在让我们看另一个例子 −

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      ArrayList list = new ArrayList();
      list.Add(100);
      list.Add(200);
      Console.WriteLine("ArrayList elements...");
      for (int i = 0; i < list.Count; i++) {
         Console.WriteLine(list[i]);
      }
      int[] intArr = new int[5] {10, 20, 30, 40, 50};
      Console.WriteLine("
Array elements...");       for (int i = 0; i < intArr.Length; i++) {          Console.WriteLine(intArr[i]);       }       list.CopyTo(intArr, 0);       Console.WriteLine("
Array elements (updated)...");       for (int i = 0; i < intArr.Length; i++) {          Console.WriteLine(intArr[i]);       }    } }

输出

这将产生以下输出 −

ArrayList elements...
100
200

数组元素...
10
20
30
40
50

Array elements (updated)...
100
200
30
40
50

相关文章