List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
for(int i = 0; i < listA.Count; i++)
{
text += listA[i] + " and " + listB[i];
}
如何使用foreach
循环执行此操作?使用foreach循环为c#中的两个列表创建列表值
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
for(int i = 0; i < listA.Count; i++)
{
text += listA[i] + " and " + listB[i];
}
如何使用foreach
循环执行此操作?使用foreach循环为c#中的两个列表创建列表值
您可以使用LINQ和Zip
方法:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
foreach (var pair in listA.Zip(listB, (a,b) => new {A = a, B = b}))
{
text += pair.A + " and " + pair.B;
}
+1击败我。 – 2012-07-10 09:47:14
出于好奇,如果listB比listA短,会发生什么? – Alex 2012-07-10 09:50:18
@Alex我相信它停止迭代最短的枚举。上面的链接文件在评论中证实了这一点。 – 2012-07-10 09:51:22
你不能用一个foreach
任何比你已经与for
做它做到这一点 - foreach
真的仅设计很好地工作时,有只列举一个序列。
然而,你可以使用LINQ做到这一点非常方便:
text = string.Join("", listA.Zip(listB, (a, b) => a + " and " + b));
这就要求既为Zip
,为specific overload of string.Join
.NET 4。
zip不在C#中,我该怎么办? – 2012-07-10 10:03:19
这样做的另一种方法就是干这个用简单枚举:
IEnumerator<string> enumerator = listB.GetEnumerator();
enumerator.MoveNext();
foreach(var stra in listA) {
text += stra + " and " + enumerator.Current.ToString() + ", ";
enumerator.MoveNext();
}
非常感谢Tigran其真正有用 – 2012-07-10 10:10:42
使用LINQ
string text = listA.Zip(listB, (a, b) => new {A = a, B = b}).Aggregate("", (current, pair) => current + (pair.A + " and " + pair.B));
List<int> = new [] { 1, 2, 3, 4 };
List<String> words = new [] { "one", "two", "three" };
var numbersAndWords = numbers.Zip(words, (n, w) => new { Number = n, Word = w });
foreach(var nw in numbersAndWords)
{
Console.WriteLine(nw.Number + nw.Word);
}
这只是对[重复问题中提到的重复问题](http://stackoverflow.com/a/1955780/358221)的重复。 – 2012-07-10 09:53:51
多数民众赞成在那... +1为此.. – 2012-07-10 09:54:50
这里使用的foreach的解决方案:
string text = null;
int cnt = 0;
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
foreach (string i in listA)
{
text += listA[cnt] + " and " + listB[cnt];
cnt++;
}
个感谢&问候,
Subhankar
如果你不想使用LINQ,你希望他们重复平行你有几个选择 - 与新类等类似下面或者你可以使用的foreach,但只适用于列表中的一个,像这样:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
string text = "";
int i = 0;
foreach (string s in listA) {
text += s + " and " + listB [i++] + "\n";
}
Console.WriteLine (text);
或使用的GetEnumerator它更好一点:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
string text = "";
List<string>.Enumerator e = listB.GetEnumerator();
foreach (string s in listA) {
e.MoveNext();
text += s + " and " + e.Current + "\n";
}
Console.WriteLine (text);
你也可以自己创建一个Enumberable metacollection将返回指出,只有一个简单的字符串数组 - 为您将需要创建一个枚举和类,这是自IEnumerable derieves:
首先枚举:
private class DoubleStringEnumerator : IEnumerator
{
private DoubleString _elemList;
private int _index;
public DoubleStringEnumerator(DoubleString doubleStringObjt)
{
_elemList = doubleStringObjt;
_index = -1;
}
public void Reset()
{
_index = -1;
}
public object Current {
get {
return _elemList.getNext();
}
}
public bool MoveNext()
{
_index++;
if (_index >= _elemList.Length)
return false;
else
return true;
}
}
该当前方法并没有真正反映它在给定的例子中的名称 - 但它是用于学习的目的。
现在等级:
public class DoubleString : IEnumerable
{
public int Length;
List<String> listA;
List<String> listB;
List<string>.Enumerator eA,eB;
public DoubleString(List<String> newA,List<String> newB)
{
if(newA.Count != newB.Count) {
throw new Exception("Lists lengths must be the same");
}
listA = newA;
listB = newB;
eA = listA.GetEnumerator();
eB = listB.GetEnumerator();
Length = listA.Count;
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)new DoubleStringEnumerator(this);
}
public string[] getNext()
{
eA.MoveNext();
eB.MoveNext();
return new string[] {eA.Current ,eB.Current };
}
}
和代码本身:
List<String> listA = new List<string> { "A1", "A2" };
List<String> listB = new List<string> { "B1", "B2" };
DoubleString newDoubleString = new DoubleString (listA, listB);
string text = "";
foreach (string[] s in newDoubleString) {
text += s[0] + " and " + s[1] + "\n";
}
Console.WriteLine (text);
当然最好还是使用LINQ。代码没有受到任何影响,但我没有编译过我的文字,希望能够澄清一些事情。随意问的问题。
你为什么要这么做? – Samson 2012-07-10 09:46:20
http://stackoverflow.com/questions/1955766/iterate-two-lists-or-arrays-with-one-foreach-statment-in-c-sharp – Samson 2012-07-10 09:46:57
你不能以简单而有效的方式做到这一点。 for循环看起来不错,但使用stringbuilder – 2012-07-10 09:48:01