How to know that this is the last loop iteration?

Is there a fantastic way to find out that you are in the last loop in a list without using counters

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? } 
+6
source share
9 answers

No, you will need to use a regular for(int i=0; i<myList.Length; i++) { ... } loop for(int i=0; i<myList.Length; i++) { ... } for this.

+8
source

How to use a for loop instead?

 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 is useful, but if you need to keep track of what you are doing, or index things in a row, then just go back to the old old for loop.

+4
source

There are two ways to do this.

First with a for foreach instead of foreach

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

Or, if necessary for working with counters, change the order of things. Normally, MoveNext is executed as control of the while loop, but if we do it right at the beginning of the loop, we can use its return to determine if we are at the end of the list or not.

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

There is no effective way to do this.

Just use the for loop and index. It works faster in any way.

+1
source

No, no, you will need to use a counter if you want to use foreach .

0
source

Use the for statement instead of foreach

0
source

Well ... there is no way to find out. Not as easy as you might have guessed. The list data structure is just a way to arrange items one by one. But you have no way of telling if the element is the last, just using the foreach structure. The best use for a structure with the count property is to find out if you are in the last (or previous) element.

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

hope this helps.

0
source

The "fancy" way is to maintain one element of the look, but why would you like it? Yus maintains the score. Here is a fancy way. There are no counters without checking the Count property. All he uses is an enumerator:

 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
source

Depends on your definition of "fancy." This may qualify:

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

For me, this is similar to what you are looking for. Please note that this is not super high quality, but it is short and fairly readable, at least for my eyes. You can also write like this:

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

Here is another alternative that I find most obvious, but this is probably the fastest way to do this:

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

Source: https://habr.com/ru/post/886817/


All Articles