[英]using foreach loop for two lists in c# for geting lists values
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];
}
How can I do this using a foreach
loop ? 如何使用
foreach
循环执行此操作?
You cannot do this with a foreach
any better than you are already doing it with for
-- foreach
is really only designed to work well when there is only one sequence to enumerate. 你不能用一个做到这一点
foreach
任何比你已经有这样做for
- foreach
是真的只设计很好地工作时,只有一个序列枚举。
However, you can do it very conveniently with LINQ: 但是,使用LINQ可以非常方便地完成:
text = string.Join("", listA.Zip(listB, (a, b) => a + " and " + b));
This requires .NET 4 both for Zip
and for the specific overload of string.Join
. 这需要.NET 4和
Zip
以及string.Join
的特定重载 。
Another way of doing this is doing that with simple Enumerator: 另一种方法是使用简单的Enumerator:
IEnumerator<string> enumerator = listB.GetEnumerator();
enumerator.MoveNext();
foreach(var stra in listA) {
text += stra + " and " + enumerator.Current.ToString() + ", ";
enumerator.MoveNext();
}
使用LINQ
string text = listA.Zip(listB, (a, b) => new {A = a, B = b}).Aggregate("", (current, pair) => current + (pair.A + " and " + pair.B));
And if you do not want to use LINQ and you want them to iterate parallel you have few options - with new classes etc. like below OR you can use foreach , but only for one of the lists, like this: 如果您不想使用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);
or make it a bit nicer using GetEnumerator: 或使用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);
also you can create yourself an Enumberable metacollection which will return out of that always a simple string array - for that you will need to create an Enumerator and a class which is derieves from IEnumerable: 你也可以创建一个Enumberable metacollection,它将返回总是一个简单的字符串数组 - 为此你需要创建一个Enumerator和一个来自IEnumerable的类:
First the Enumerator: 首先是调查员:
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;
}
}
The Current method does not really reflects it's name in the given example - but it is for learning purposes. Current方法并没有真正反映给定示例中的名称 - 但它是出于学习目的。
Now the class: 现在上课:
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 };
}
}
And the code itself: 代码本身:
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);
Of course still better to use LINQ. 当然还是更好地使用LINQ。 The code is not optimsied, but I had no compiler with me so writting from my head - hope it will clarify few things.
代码没有被激活,但我没有编译器,所以写下我的头 - 希望它能澄清一些事情。 Feel free to ask questions.
随意问的问题。
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);
}
Here is the solution using foreach: 以下是使用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++;
}
Thanks & Regards, 感谢和问候,
Subhankar Subhankar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.