C# looping through an array

后端 未结 7 2052
一生所求
一生所求 2021-01-07 20:16

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

相关标签:
7条回答
  • 2021-01-07 20:24

    Here is a more general solution:

    int increment = 3;
    for(int i = 0; i < theData.Length; i += increment)
    {
       for(int j = 0; j < increment; j++)
       {
          if(i+j < theData.Length) {
             //theData[i + j] for the current index
          }
       }
    
    }
    
    0 讨论(0)
  • 2021-01-07 20:27

    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!

    0 讨论(0)
  • 2021-01-07 20:32

    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.

    0 讨论(0)
  • 2021-01-07 20:33

    Just increment i by 3 in each step:

      Debug.Assert((theData.Length % 3) == 0);  // 'theData' will always be divisible by 3
    
      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..
           string item1 = theData[i+0];
           string item2 = theData[i+1];
           string item3 = theData[i+2];
           // use the items
      }
    

    To answer some comments, it is a given that theData.Length is a multiple of 3 so there is no need to check for theData.Length-2 as an upperbound. That would only mask errors in the preconditions.

    0 讨论(0)
  • 2021-01-07 20:36

    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.

    0 讨论(0)
  • 2021-01-07 20:39
    string[] friends = new string[4];
    friends[0]= "ali";
    friends[1]= "Mike";
    friends[2]= "jan";
    friends[3]= "hamid";
    
    for (int i = 0; i < friends.Length; i++)
    {
        Console.WriteLine(friends[i]);
    }Console.ReadLine();
    
    0 讨论(0)
提交回复
热议问题