您的位置:首页 > 其它

如何把一个array复制到arrayList里?

2010-06-02 15:06 399 查看
方法一、使用foreach循环,将array数组中的数据逐步放入ArrayList的对象中;
方法二、使用Copy方法,进行数据的复制;
方法三、使用ArrayList的adpater的方法 ,将整个Array对象封装到ArrayList对象中。
// author:renfuming
public static void Main(string[] renargs)
{
int[] arrayInt=new int[]{1,2,3,4};
ArrayList arrlistInt=new ArrayList();
//方法一
foreach(int a in arrayInt)
{
arrlistInt.Add(a);
}
Console.WriteLine(arrlistInt[2].ToString());//输出3
//方法二:
ArrayList arrlistInt2=new ArrayList();
arrlistInt2=ArrayList.Adapter(arrayInt);
Console.WriteLine(arrlistInt2[2].ToString());//输出3
//逆向转换
Array resultArr=(int[])arrlistInt2.ToArray(typeof(int));
Console.WriteLine(resultArr.GetValue(2));//输出3
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: