2012-07-10 76 views
3
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#中的两个列表创建列表值

+0

你为什么要这么做? – Samson 2012-07-10 09:46:20

+2

http://stackoverflow.com/questions/1955766/iterate-two-lists-or-arrays-with-one-foreach-statment-in-c-sharp – Samson 2012-07-10 09:46:57

+1

你不能以简单而有效的方式做到这一点。 for循环看起来不错,但使用stringbuilder – 2012-07-10 09:48:01

回答

11

您可以使用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; 
} 
+0

+1击败我。 – 2012-07-10 09:47:14

+0

出于好奇,如果listB比listA短,会发生什么? – Alex 2012-07-10 09:50:18

+0

@Alex我相信它停止迭代最短的枚举。上面的链接文件在评论中证实了这一点。 – 2012-07-10 09:51:22

3

你不能用一个foreach任何比你已经与for做它做到这一点 - foreach真的仅设计很好地工作时,有只列举一个序列。

然而,你可以使用LINQ做到这一点非常方便:

text = string.Join("", listA.Zip(listB, (a, b) => a + " and " + b)); 

这就要求既为Zip,为specific overload of string.Join .NET 4。

+0

zip不在C#中,我该怎么办? – 2012-07-10 10:03:19

3

这样做的另一种方法就是干这个用简单枚举:

IEnumerator<string> enumerator = listB.GetEnumerator(); 
enumerator.MoveNext(); 
foreach(var stra in listA) { 
    text += stra + " and " + enumerator.Current.ToString() + ", "; 
    enumerator.MoveNext(); 
} 
+0

非常感谢Tigran其真正有用 – 2012-07-10 10:10:42

0

使用LINQ

string text = listA.Zip(listB, (a, b) => new {A = a, B = b}).Aggregate("", (current, pair) => current + (pair.A + " and " + pair.B)); 
-2
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); 
} 
+3

这只是对[重复问题中提到的重复问题](http://stackoverflow.com/a/195578​​0/358221)的重复。 – 2012-07-10 09:53:51

+0

多数民众赞成在那... +1为此.. – 2012-07-10 09:54:50

-3

这里使用的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

+2

你为什么用listA [cnt]而不是我? – 2012-07-10 10:12:53

+0

@Francesco你没有看到我是一个字符串变量,我们不能使用字符串访问数组索引。 – Subhankar 2012-07-10 10:45:52

+0

@FrancescoBaruchelli我们可以使用字符串访问数组索引吗? – Subhankar 2012-07-10 11:02:03

0

如果你不想使用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。代码没有受到任何影响,但我没有编译过我的文字,希望能够澄清一些事情。随意问的问题。

相关问题