使用 C# 程式合併兩個或更多列表
設定三個列表 −
// three lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};
var list3 = new List<int>{2, 5, 6};現在,使用 Concat 方法合併上述列表 −
var res1 = list1.Concat(list2); var res2 = res1.Concat(list3);
下面是完整程式碼 −
示例
using System.Collections.Generic;
using System.Linq;
using System;
public class Demo {
public static void Main() {
// three lists
var list1 = new List<int>{3, 4};
var list2 = new List<int>{1, 2, 3};
var list3 = new List<int>{2, 5, 6};
// concat
var res1 = list1.Concat(list2);
var res2 = res1.Concat(list3);
foreach(int i in res2) {
Console.WriteLine(i);
}
}
}輸出
3 4 1 2 3 2 5 6
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP