473,698 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterating an array from set position

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<numberOfPlaye rs; 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 (numberOfPlayer s-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
3 1701
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<numberOfPlaye rs; 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 (numberOfPlayer s-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
"Daniel" <Da*****@vestry online.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
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.c om> wrote in
message news:uP******** ******@TK2MSFTN GP03.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.co m
"Daniel" <Da*****@vestry online.com> wrote in message
news:uY******** ******@TK2MSFTN GP04.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<numberOfPlaye rs; 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 (numberOfPlayer s-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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
12344
by: Mr.Clean | last post by:
I have a string. I'd like to take this string and make each char change until it gets to another char at the same position of the string. Example: Original String: " NEW YORK " Final String: "SAN FRANSISCO" I'd like each char to loop through the chars on a timer
6
6049
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I try to modify the objects in the array while iterating through it. I marked the bugs in this code: // Loop through all previously added accounts foreach(Account a in a1) // a1 is an ArrayList { // If name and context is the same if(a.Name ==...
3
2411
by: DevNull | last post by:
I have a program where we load a mapfile, comprised of a .csv with numbers which represent object types at a given position. Each position is in the map is called a Cell, a cell contains only a position struct with it's own x,y and an int called CellType which determines if the Cell is a solid i.e. a wall, or a nonsolid, i.e. empty space. Given... <code>
4
1695
by: Ronald Raygun | last post by:
I want to store objects in ana array and then iterate through the array, retrieving a (reference) to each object in the array, and calling a method on the array. I am not sure how to do it, but this is pseudocode of what I want to do: <?php $m_fieldItems = createObjects(); $outstr = ''; for($i=0; $i < count($m_fieldItems); $i++)
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.