如何將整個 ArrayList 複製到 C# 中的一維陣列?
要將整個 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("
String 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("
Integer 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP