如何在C#中将整个ArrayList复制到一维数组?
要将整个ArrayList复制到一维数组,代码如下-
示例
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList list = new ArrayList();
list.Add("AB");
list.Add("BC");
list.Add("CD");
list.Add("EF");
list.Add("GH");
list.Add("IJ");
list.Add("KL");
list.Add("MN");
String[] strArr = new String[10];
Console.WriteLine("ArrayList...");
foreach(Object obj in list)
Console.WriteLine("{0}", obj);
list.CopyTo(strArr);
Console.WriteLine("\nString Array after copying elements from ArrayList...");
foreach(Object ob in strArr)
Console.WriteLine("{0}", ob);
}
}输出结果
这将产生以下输出-
ArrayList... AB BC CD EF GH IJ KL MN String Array after copying elements from ArrayList... AB BC CD EF GH IJ KL MN
示例
现在让我们来看另一个示例-
using System;
using System.Collections;
public class Demo {
public static void Main(){
ArrayList list = new ArrayList();
list.Add(100);
list.Add(200);
list.Add(300);
list.Add(400);
list.Add(500);
list.Add(600);
list.Add(700);
list.Add(800);
list.Add(900);
list.Add(1000);
int[] intArr = new int[10];
Console.WriteLine("ArrayList...");
foreach(Object obj in list)
Console.WriteLine("{0}", obj);
list.CopyTo(intArr);
Console.WriteLine("\nInteger Array after copying elements from ArrayList...");
foreach(Object ob in intArr)
Console.WriteLine("{0}", ob);
}
}输出结果
这将产生以下输出-
ArrayList... 100 200 300 400 500 600 700 800 900 1000 Integer Array after copying elements from ArrayList... 100 200 300 400 500 600 700 800 900 1000
热门推荐
10 老板餐馆开业祝福语简短
11 用名字做祝福语简短
12 病人的生日祝福语简短
13 新年祝福语给长辈简短
14 画室揭牌仪式祝福语简短
15 地生中考祝福语简短
16 如果不结婚祝福语简短
17 年末祝福语简短小清新
18 蛋糕班级新年祝福语简短