2012-07-10 73 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。代碼沒有受到任何影響,但我沒有編譯過我的文字,希望能夠澄清一些事情。隨意問的問題。

相關問題