Iterating an array from set position

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hey guys

i have a array of daya that i wish to iterate through. the only catch is i
need to start from a certain position but on reaching the end of the array
go back to the start and do the rest so that on ever loop every element is
checked in the right order.

For example

int totalCount=0; //to internally track total elements parsed

for(int i=startPos; i<numberOfPlayers; i++)
{
//loop through checking for condition and breaking loop if found

totalCount++;

if(totalCount >= numberOfPlayers) //if we have checked them all stop
looping
break;
else if( startPos.Equals(numberOfPlayers-1) ) //if we reached the
end but have not checked them all restart from beginning
startPos = 0; //to reset to start
}


There must be a better way than the above solution?

thanks
 
Hi,
Hey guys

i have a array of daya that i wish to iterate through. the only catch is i
need to start from a certain position but on reaching the end of the array
go back to the start and do the rest so that on ever loop every element is
checked in the right order.

For example

int totalCount=0; //to internally track total elements parsed

for(int i=startPos; i<numberOfPlayers; i++)
{
//loop through checking for condition and breaking loop if found

totalCount++;

if(totalCount >= numberOfPlayers) //if we have checked them all stop
looping
break;
else if( startPos.Equals(numberOfPlayers-1) ) //if we reached the
end but have not checked them all restart from beginning
startPos = 0; //to reset to start
}


There must be a better way than the above solution?

thanks

try to use an offset:

int offset = startPos;
for (int idx = 0; idx < numberOfPlayers; ++idx) {
int indexToUse = (idx + offset) % numberOfPlayers;
}

This should work, though I haven't tested it

Tobi
 
Daniel said:
i have a array of daya that i wish to iterate through. the only catch is i
need to start from a certain position but on reaching the end of the array
go back to the start and do the rest so that on ever loop every element is
checked in the right order.

If I have you right, the easiest way is to write two loops:

for (int i = certainPosition; i < count; ++i)
// visit

for (int i = 0; i < certainPosition; ++i)
// visit
There must be a better way than the above solution?

The solution you suggest will not work - changing the startPos will not
reset the loop.

-- Barry
 
Well spotted Bary, i noticed that bug after oops. the second and last
startPos whould have been 'i' . Guess you all realised that?

I can use the two loops but once i find the one i want i want to break. The
two loops would almost certainly result in the same elements being checked
at some point, a bit inefficient? Though i am being picky.

For the enumeration class methodology.

Does that allow for me to iterate through an array o any type and it will
check every element starting at my start pos, going to the end and then if
it doesnt find it going back to the start and checking the final ones.
Breaking the moment it finds the one i need. So i can put my conditional
part inside the braces of the foreach statement?

I am going to try it. Althought it uses two for loops it does make for very
neat code which i like. Thanks for such a detailed answer, i have never seen
the 'yield' term before, how does this differ from return?

Thanks!



Nicholas Paldino said:
Daniel,

I think the best solution is to have a class do this for you. It
actually becomes VERY easy using generics and anonymous iterators. I've
attached a sample class for you to use (and a little demo program) which
will show you how you can make it much easier.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Daniel said:
Hey guys

i have a array of daya that i wish to iterate through. the only catch is
i
need to start from a certain position but on reaching the end of the
array
go back to the start and do the rest so that on ever loop every element
is
checked in the right order.

For example

int totalCount=0; //to internally track total elements parsed

for(int i=startPos; i<numberOfPlayers; i++)
{
//loop through checking for condition and breaking loop if found

totalCount++;

if(totalCount >= numberOfPlayers) //if we have checked them all
stop
looping
break;
else if( startPos.Equals(numberOfPlayers-1) ) //if we reached the
end but have not checked them all restart from beginning
startPos = 0; //to reset to start
}


There must be a better way than the above solution?

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top