如何將列表集合複製到陣列?
要將 C# 列表集合複製到陣列,首先設定一個列表 −
List <string> list1 = new List <string> ();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");現在宣告一個字串陣列並使用 CopyTo() 方法來複制 −
string[] arr = new string[20]; list1.CopyTo(arr);
讓我們檢視將列表複製到一維陣列中的完整程式碼。
示例
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List <string> list1 = new List <string> ();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
Console.WriteLine("First list...");
foreach(string value in list1) {
Console.WriteLine(value);
}
string[] arr = new string[20];
list1.CopyTo(arr);
Console.WriteLine("After copy...");
foreach(string value in arr) {
Console.WriteLine(value);
}
}
}
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP