By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,764 Members | 1,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,764 IT Pros & Developers. It's quick & easy.

Iterating an array from set position

P: n/a
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
May 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Daniel schrieb:
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
May 16 '06 #2

P: n/a
"Daniel" <Da*****@vestryonline.com> wrote:
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
May 16 '06 #3

P: n/a
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uP**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Daniel" <Da*****@vestryonline.com> wrote in message
news:uY**************@TK2MSFTNGP04.phx.gbl...
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


May 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.