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循环来做到这一点?

4

7 回答 7

14

您可以使用 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;
}
于 2012-07-10T09:46:41.757 回答
4

你不能用foreach比你已经在做的更好的方法来做到这一点for——foreach实际上只有在只有一个序列要枚举时才能很好地工作。

但是,您可以使用 LINQ 非常方便地做到这一点:

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

这需要 .NET 4Zip和 .NET 的特定重载string.Join

于 2012-07-10T09:48:09.037 回答
4

另一种方法是使用简单的枚举器:

IEnumerator<string>  enumerator = listB.GetEnumerator(); 
enumerator.MoveNext();
foreach(var stra in listA) {
    text += stra + " and " + enumerator.Current.ToString() + ", ";
    enumerator.MoveNext();
}
于 2012-07-10T09:50:33.377 回答
1

使用 LINQ

string text = listA.Zip(listB, (a, b) => new {A = a, B = b}).Aggregate("", (current, pair) => current + (pair.A + " and " + pair.B));
于 2012-07-10T09:50:35.117 回答
1

如果您不想使用 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 元集合,它总是会返回一个简单的字符串数组——为此你需要创建一个 Enumerator 和一个派生自 IEnumerable 的类:

首先是枚举器:

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;
    }
}

Current方法并没有真正反映给定示例中的名称 - 但它是出于学习目的。

现在上课:

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更好。代码没有优化,但是我没有编译器,所以我的脑袋里写着——希望它能澄清一些事情。随意问的问题。

于 2012-07-10T11:07:03.410 回答
-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);
}
于 2012-07-10T09:53:01.003 回答
-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

于 2012-07-10T09:54:34.147 回答