打印列表项

List list = new List(); list.Add("A"); list.Add("B"); List list1 = new List(); list.Add("a"); list.Add("b"); for (int i = 0; i < list.Count; i++) { // print another list items. for (int j = 0; j < list1.Count; j++) { Console.WriteLine("/" + list[i] + "/" + list1[j]); } } 

我想编码像这个string tmpS =+ list[i]; 加入下一个列表项togeter。

然后打印tmpS

但编译错误CS0023:运算符’+’不能应用于’string’类型的操作数。

如何打印下面的所有项目。(任何排序都可以)

A A Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba

(大写号码没有交换。小字符应该被交换。并且始终跟随大写号码附加小字符。)

它花了很长时间我没有在纯算法问题上工作!

这个程序应该做的伎俩:

 class Program { static void Main(string[] args) { List uppers = new List(); uppers.Add("A"); uppers.Add("B"); List lowers = new List(); lowers.Add("a"); lowers.Add("b"); List combinedUppers = GetCombinedItems(uppers); List combinedLowers = GetCombinedItems(lowers); List combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers); foreach (string combo in combinedUppersLowers) { Console.WriteLine(combo); } Console.Read(); } static private List GetCombinedItems(List list) { List combinedItems = new List(); for (int i = 0; i < list.Count; i++) { combinedItems.Add(list[i]); for (int j = 0; j < list.Count; j++) { if (list[i] != list[j]) { combinedItems.Add(String.Format("{0}{1}", list[i], list[j])); } } } return combinedItems; } static private List GetCombinedList(List list1, List list2) { List combinedList = new List(); for (int i = 0; i < list1.Count; i++) { combinedList.Add(list1[i]); for (int j = 0; j < list2.Count; j++) { combinedList.Add(String.Format("{0}{1}", list1[i], list2[j])); } } for (int i = 0; i < list2.Count; i++) { combinedList.Add(list2[i]); for (int j = 0; j < list1.Count; j++) { combinedList.Add(String.Format("{0}{1}", list2[i], list1[j])); } } return combinedList; } } 

问候。


这个程序给你这个输出:

A A Aab Ab Aba AB ABa ABab ABb ABba B Ba Bab Bb Bba BA BAa BAab BAb BAba a aA aA aB aBA ab ab ab ab ab ab ab ab b b b b b b b b ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba

这有点像家庭作业。

 List list = new List(); list.Add("A"); list.Add("B"); List list1 = new List(); list.Add("a"); list.Add("b"); string xxx = ""; for (int i = 0; i < list.Count; i++) { xxx += list[i]; Console.WriteLine(xxx); // print another list items. for (int j = 0; j < list1.Count; j++) { Console.WriteLine("/" + list[i] + "/" + list1[j]); } } 

这是+==+

但无论如何你应该使用StringBuilder。