2011-04-27 125 views
6

有没有知道你是在列表中的最后一环奇特的方式,而不使用计数器如何知道这是循环的最后一次迭代?

List<string> myList = new List<string>() {"are", "we", "there", "yet"}; 

foreach(string myString in myList) { 
    // Is there a fancy way to find out here if this is the last time through? 
} 
+1

你有什么做到最后一个元素?你是否想要建立一个句子并在最后添加标点符号?如果是这样,你可以尝试'string.Join(“”,myList.ToArray())+“。”' – hunter 2011-04-27 17:27:47

+0

发现你的解决方案waaaaaay在底部... – hunter 2011-04-27 17:44:45

回答

8

不,你将不得不使用普通for(int i=0; i<myList.Length; i++) { ... }环路这一点。

+1

谢谢,这是我倒退,但我想确保我没有错过任何东西。 – Terco 2011-04-27 18:34:46

0

没有,没有,你有,如果你想使用foreach使用计数器。

1

没有有效的方法来做到这一点。

只需使用for循环和索引即可。它以任何方式运行得更快。

0

使用for操盘的foreach

4

如何使用for循环代替?

List<string> myList = new List<string>() {"are", "we", "there", "yet"}; 

for (var i=0; i<myList.Count; i++) 
{ 
    var myString = myList[i]; 
    if (i==myList.Count-1) 
    { 
     // this is the last item in the list 
    } 
} 

foreach是有用的 - 但如果你需要计数,以保持自己在做什么数,或指数的东西,然后就恢复到一个很好的旧for循环。

0

那么......没有办法知道这一点。没有你想象的那么容易。列表数据结构只是一种接一个地订购商品的方式。但是,如果某个项目是仅使用foreach结构的最后一项,则无法说明该方法。最好使用count属性的结构来知道你是否击中最后一个(或前一个)项目。

var length = list.Count; 

for (var idx = 0; idx < list.Count; idx++) 
{ 
    if (idx == (length - 1)) 
    { 
     // The last item. Do something 
    } 
} 

希望它有帮助。

3

有两种方法我会这样做。

首先,用for循环,而不是foreach

for(int i = 0; i < myList.Count; i++) 
{ 
    string myString = myList[i]; 
    bool isLast = i == myList.Count - 1; 

    ... 
} 

或者,如果需要与普查员的工作,改变事物的秩序。通常,MoveNext是作为while循环的控制完成的,但是如果我们在循环的开始处正确执行它,我们可以使用它的返回来确定我们是否在列表的末尾。

IEnumerator<string> enumerator = myList.GetEnumerator(); 
bool isLast = !enumerator.MoveNext(); 
while(!isLast) 
{ 
    string myString = enumerator.Current; 
    isLast = !enumerator.MoveNext(); 

    ... 
} 
0

“花哨”的方式是保持前瞻的1个元素,但为什么你要在地球上?朱斯保持计数。这是奇特的方式。没有计数器,不检查计数属性。它使用的是一个调查员:

using System; 
using System.Collections.Generic; 

namespace Sandbox 
{ 
    class Program 
    { 

    enum ListPosition : byte 
    { 
     First  = 0x01  , 
     Only  = First|Last , 
     Middle = 0x02  , 
     Last  = 0x04  , 
     Exhausted = 0x00  , 
    } 

    private static void WalkList(List<int> numbers) 
    { 
     List<int>.Enumerator numberWalker = numbers.GetEnumerator(); 
     bool     currFetched = numberWalker.MoveNext(); 
     int     currValue = currFetched ? numberWalker.Current : default(int); 
     bool     nextFetched = numberWalker.MoveNext(); 
     int     nextValue = nextFetched ? numberWalker.Current : default(int); 
     ListPosition   position  ; 

     if  ( currFetched && nextFetched) position = ListPosition.First  ; 
     else if ( currFetched && ! nextFetched) position = ListPosition.Only  ; 
     else if (! currFetched     ) position = ListPosition.Exhausted ; 
     else throw new InvalidOperationException("Reached Unreachable Code. Hmmm...that doesn't seem quite right"); 

     while (position != ListPosition.Exhausted) 
     { 
     string article = (position==ListPosition.Middle?"a":"the"); 

     Console.WriteLine(" {0} is {1} {2} item in the list" , currValue , article , position); 

     currFetched = nextFetched ; 
     currValue = nextValue ; 

     nextFetched = numberWalker.MoveNext()       ; 
     nextValue = nextFetched?numberWalker.Current:default(int) ; 

     if  ( currFetched && nextFetched) position = ListPosition.Middle ; 
     else if ( currFetched && ! nextFetched) position = ListPosition.Last  ; 
     else if (! currFetched     ) position = ListPosition.Exhausted ; 
     else throw new InvalidOperationException("Reached Unreachable Code. Hmmm...that doesn't seem quite right"); 

     } 

     Console.WriteLine() ; 
     return ; 
    } 

    static void Main(string[] args) 
    { 
     List<int> list1 = new List<int>(new []{ 1 ,    }) ; 
     List<int> list2 = new List<int>(new []{ 1 , 2 ,   }) ; 
     List<int> list3 = new List<int>(new []{ 1 , 2 , 3 ,  }) ; 
     List<int> list4 = new List<int>(new []{ 1 , 2 , 3 , 4 , }) ; 

     Console.WriteLine("List 1:") ; WalkList(list1) ; 
     Console.WriteLine("List 2:") ; WalkList(list2) ; 
     Console.WriteLine("List 3:") ; WalkList(list3) ; 
     Console.WriteLine("List 4:") ; WalkList(list4) ; 

     return ; 
    } 

    } 
} 
0

取决于你对“花哨”的定义。这可能有资格:

if (myList.Count > 0) 
{ 
    myList.Take(myList.Count - 1)).ForEach(myString => doSomething(myString)); 
    doSomethingElse(myList.Last()); 
} 

对我来说,这似乎接近你在找什么。请注意,它并不是超高性能,但至少在我看来,它很短并且非常可读。你也可以写这样说:

if (myList.Count > 0) 
{ 
    foreach (string myString in myList.Take(myList.Count - 1)) 
    { 
     doSomething(myString); 
    } 

    doSomethingElse(myList.Last()); 
} 

这里的另一种选择,我会考虑的最明显的,但它可能是这样做的最快方法:

if (myList.Count > 0) 
{ 
    for (int i = 0; i < myList.Count - 1; i++) 
    { 
     doSomething(myList[i]); 
    } 

    doSomethingElse(myList.Count - 1); 
}