C# 程式列印列表中的所有子列表
首先,建立一個列表 -
List list = new List();
此處的字串為“xyz”,我們將為其查詢子列表。迴圈時,我們將宣告另一個列表,該列表將在每次真正的迭代中生成子列表 -
for (int i = 1; i < str.Length; i++) {
list.Add(str[i - 1].ToString());
List newlist = new List();
for (int j = 0; j < list.Count; j++) {
string list2 = list[j] + str[i];
newlist.Add(list2);
}
list.AddRange(newlist);
}以下是完整程式碼 -
舉例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
string str = "xyz";
List list = new List();
for (int i = 1; i < str.Length; i++) {
list.Add(str[i - 1].ToString());
List newlist = new List();
for (int j = 0; j < list.Count; j++) {
string list2 = list[j] + str[i];
newlist.Add(list2);
}
list.AddRange(newlist);
}
list.Add(str[str.Length - 1].ToString());
list.Sort();
Console.WriteLine(string.Join(Environment.NewLine, list));
}
}
}輸出
x xy xyz xz y yz z
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP