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
热门推荐
10 写字生日祝福语简短独特
11 最美的留言祝福语简短
12 生日哥哥祝福语简短独特
13 毕业祝福语简短英语小学
14 亲戚送行祝福语简短精辟
15 公司年终晚会祝福语简短
16 工人生日祝福语简短
17 圣诞简短祝福语给父母
18 拜新年的祝福语简短