在 C# 中將 ArrayList 轉換成陣列
要在 C# 中將 ArrayList 轉換成陣列,請使用 ToArray() 方法。
首先,建立一個 ArrayList −
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");轉換時,請使用 ToArray() 方法 −
arrList.ToArray(typeof(string)) as string[];
我們來看看完整的程式碼 −
示例
using System;
using System.Collections;
public class Program {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("one");
arrList.Add("two");
arrList.Add("three");
string[] arr = arrList.ToArray(typeof(string)) as string[];
foreach (string res in arr) {
Console.WriteLine(res);
}
}
}輸出
one two three
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP