tags:

views:

231

answers:

5

I am looping through an array of strings, such as (1/12/1992 apple truck 12/10/10 orange bicycle). The array's length will always be divisible by 3. I need to loop through the array and grab the first 3 items (I'm going to insert them into a DB) and then grab the next 3 and so on and so forth until all of them have been gone through.

//iterate the array
for (int i = 0; i < theData.Length; i++)
{
    //grab 3 items at a time and do db insert, continue until all items are gone. 'theData' will always be divisible by 3.
}
+6  A: 

i++ is the standard use of a loop, but not the only way. Try incrementing by 3 each time:

 for (int i = 0; i < theData.Length - 2; i+=3) 
    { 

        // use theData[i], theData[i+1], theData[i+2]

    } 
froadie
You're the only one so far who won't get an out-of-bounds error on the last iteration; instead, you stop one iteration short. Try `i < theData.Length - 2`.
Michael Myers
@mmyers - great minds think alike. :) Changed it already. But I don't think the check is necessary anyway, only put it in because it seemed logical...
froadie
+1 for bound checking on length!
FrustratedWithFormsDesigner
The question states the length will be a multiple of 3, no need for the -2
Henk Holterman
@Henk: And right you are; if I'd downvoted you I'd take it back. Might want to draw attention to that point in your answer, though.
Michael Myers
Any reason for the downvotes...? It seems that every answer to this post received a downvote.
froadie
+14  A: 

Just increment 'i' by 3 in each step:

  for (int i = 0; i < theData.Length; i += 3)
  {
       //grab 3 items at a time and do db insert, 
       // continue until all items are gone. 'theData' will always be divisible by 3.
       string item1 = theData[i+0];
       string item2 = theData[i+1];
       string item3 = theData[i+2];
       // ...
  }

To answer some comments, it is given that theData.Length is a multiple of 3 so there is no need to check for theData.Length-2 as an upperbound. Besides, if that contract is broken at least one of the theData[i+n] expressions will fail.

Henk Holterman
good point to comment on your edition
Javier Morillo
+2  A: 

Not too difficult. Just increment the counter of the for loop by 3 each iteration and then offset the indexer to get the batch of 3 at a time:

for(int i=0; i < theData.Length; i+=3)
{
    var item1 = theData[i];
    var item2 = theData[i+1];
    var item3 = theData[i+2];
}

If the length of the array wasn't garuanteed to be a multiple of three, you would need to check the upper bound with theData.Length - 2 instead.

Justin Niessner
+2  A: 

Your for loop doesn't need to just add one. You can loop by three.

for(int i = 0; i < theData.Length; i+=3)
{
  string value1 = theData[i];
  string value2 = theData[i+1];
  string value3 = theData[i+2];
}

Basically, you are just using indexes to grab the values in your array. One point to note here, I am not checking to see if you go past the end of your array. Make sure you are doing bounds checking!

JasCav
Downvote? What for? (I noticed all answers except the first were downvoted. Lame.)
JasCav
@Jason - the first was originally, but seems to have been taken off. Strange.
froadie
+1  A: 

This should work:

//iterate the array
for (int i = 0; i < theData.Length; i+=3)
{
    //grab 3 items at a time and do db insert, continue until all items are gone. 'theData' will always be divisible by 3.
    var a = theData[i];
    var b = theData[i + 1];
    var c = theData[i + 2];
}

I've been downvoted for this answer once. I'm pretty sure it is related to the use of theData.Length for the upperbound. The code as is works fine because array is guaranteed to be a multiple of three as the question states. If this guarantee wasn't in place, you would need to check the upper bound with theData.Length - 2 instead.

Ben Griswold