C# 打印列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1960758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print list items
提问by Nano HE
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
List<string> list1 = new List<string>();
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]);
}
}
I want to code like this string tmpS =+ list[i];
to Join the next list item togeter.
我想这样编码string tmpS =+ list[i];
以加入下一个列表项。
then print tmpS
然后打印 tmpS
but compile error CS0023: Operator '+' cannot be applied to operand of type 'string'.
但编译错误 CS0023:运算符“+”不能应用于“字符串”类型的操作数。
How to print all the items below.(any sort is ok)
如何打印下面的所有项目。(任何类型都可以)
A Aa Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba
A Aa Ab Aab Aba AB ABa ABb ABab ABba B Ba Bb Bab Bba
(The Caps number No swap. the small characters should be swaped. and always follow Caps Numbers Append small characters.)
(大写数字不交换。小字符应该交换。并始终遵循大写数字附加小字符。)
采纳答案by Ucodia
it makes a long time I did not worked on a pure algorithmic problem!
这让我很长时间没有解决纯算法问题!
This program should do the trick:
这个程序应该可以解决问题:
class Program
{
static void Main(string[] args)
{
List<string> uppers = new List<string>();
uppers.Add("A");
uppers.Add("B");
List<string> lowers = new List<string>();
lowers.Add("a");
lowers.Add("b");
List<string> combinedUppers = GetCombinedItems(uppers);
List<string> combinedLowers = GetCombinedItems(lowers);
List<string> combinedUppersLowers = GetCombinedList(combinedUppers, combinedLowers);
foreach (string combo in combinedUppersLowers)
{
Console.WriteLine(combo);
}
Console.Read();
}
static private List<string> GetCombinedItems(List<string> list)
{
List<string> combinedItems = new List<string>();
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<string> GetCombinedList(List<string> list1, List<string> list2)
{
List<string> combinedList = new List<string>();
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;
}
}
Regards.
问候。
This program gives you this output:
该程序为您提供以下输出:
A Aa Aab Ab Aba AB ABa ABab ABb ABba B Ba Bab Bb Bba BA BAa BAab BAb BAba a aA aAB aB aBA ab abA abAB abB abBA b bA bAB bB bBA ba baA baAB baB baBA
A Aa Aab Ab Aba AB ABa ABab ABb ABBa B Ba Bab Bb Bba BA BAa BAab BAb BAba a aA aAB aB aba
回答by slugster
This smells like homework.
这闻起来像家庭作业。
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
List<string> list1 = new List<string>();
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]);
}
}
回答by helium
It's +=
not =+
.
这是+=
不是=+
。
But You should use a StringBuilder anyway.
但是无论如何您都应该使用 StringBuilder 。