【问题标题】:C# looping through an arrayC#循环遍历数组
【发布时间】:2011-01-25 14:53:04
【问题描述】:

我正在循环遍历一系列字符串,例如 (1/12/1992 apple truck 12/10/10 orange bike)。数组的长度总是可以被 3 整除。我需要遍历数组并抓取前 3 个项目(我将把它们插入数据库),然后抓取下一个 3,依此类推,直到所有他们已经通过了。

//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.
}

【问题讨论】:

  • 5 个答案在 40 秒内提交,所有答案都具有相同(几乎准确)的信息。哈哈。不错。
  • 我觉得有趣的是,在我写我的时候发布了五个答案......并且没有一个得到了正确的上边界。
  • 正如@Mahesh 所说:你没有问问题。问题通常以问号 (?) 结尾。您没有描述您尝试过什么、失败了什么以及遇到困难的原因。
  • 有人会因为投反对票而感到高兴吗...?
  • @Matt - 在每次迭代中(递增 3 时),theData[i] 应该是您的日期,theData[i+1] 应该是您的水果类型,theData[i+2] 应该是您的汽车。有什么问题?

标签: c# arrays loops


【解决方案1】:

只需在每一步中将i 增加 3:

  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
  }

为了回答一些 cmets,给定 theData.Length 是 3 的倍数,因此无需检查 theData.Length-2 作为上限。这只会掩盖前提条件中的错误。

【讨论】:

    【解决方案2】:

    i++ 是循环的标准用法,但不是唯一的方法。尝试每次增加 3:

     for (int i = 0; i < theData.Length - 2; i+=3) 
        { 
    
            // use theData[i], theData[i+1], theData[i+2]
    
        } 
    

    【讨论】:

    • 到目前为止,您是唯一一个不会在最后一次迭代中出现越界错误的人;相反,你停止了一次迭代。试试i &lt; theData.Length - 2
    • @mmyers - 伟大的思想都一样。 :) 已经改变了。但我认为无论如何都不需要检查,只是因为它看起来合乎逻辑才把它放进去......
    • 问题指出长度将是 3 的倍数,不需要 -2
    • @Henk:你是对的;如果我对你投了反对票,我会收回它。不过,可能想在您的回答中提请注意这一点。
    • 反对票的任何理由...?似乎对这篇文章的每个回答都遭到了反对。
    【解决方案3】:

    难度不大。每次迭代只需将 for 循环的计数器增加 3,然后偏移索引器以一次获得 3 个批次:

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

    如果数组的长度不能保证为三的倍数,则需要改为使用theData.Length - 2 检查上限。

    【讨论】:

      【解决方案4】:

      您的 for 循环不需要只添加一个。你可以循环三个。

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

      基本上,您只是使用索引来获取数组中的值。这里要注意一点,我不是在检查你是否超过了数组的末尾。确保您正在进行边界检查!

      【讨论】:

      • 投反对票?做什么的? (我注意到除了第一个之外的所有答案都被否决了。蹩脚。)
      • 第一个本来是,但好像已经被取消了。奇怪。
      【解决方案5】:

      这应该可行:

      //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];
      }
      

      我曾经因为这个答案而被否决。我很确定这与使用 theData.Length 作为上限有关。代码原样工作正常,因为数组保证为问题所述的三的倍数。如果没有这个保证,你需要用 theData.Length - 2 检查上限。

      【讨论】:

        【解决方案6】:

        这是一个更通用的解决方案:

        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
              }
           }
        
        }
        

        【讨论】:

          【解决方案7】:
          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();
          

          【讨论】:

          • 此问题已包含多个答案和一个已接受的答案。您能否解释(通过编辑您的答案)您的答案与其他答案的不同之处?也知道从长远来看,仅代码的答案没有用处。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-15
          • 1970-01-01
          • 2014-06-04
          • 1970-01-01
          • 2019-07-10
          相关资源
          最近更新 更多