【问题标题】:c# restart for loopc# 重启for循环
【发布时间】:2012-06-02 23:52:38
【问题描述】:

所以我有这几行代码:

string[] newData = File.ReadAllLines(fileName)
int length = newData.Length;
for (int i = 0; i < length; i++)
{
    if (Condition)
    {
       //do something with the first line
    }
    else
    {
      //restart the for loop BUT skip first line and start reading from the second
    }
}

我尝试过使用 goto,但如您所见,如果我再次启动 for 循环,它将从第一行开始。

那么如何重新开始循环并更改起始行(从数组中获取不同的键)?

【问题讨论】:

  • 如果条件为真你只想读第一行,如果条件为假则只从第2行读到最后?
  • 还要注意你的i &lt;= newData.Length 应该是&lt;
  • 如果是真的,我在第一行做一些工作,然后是第二行等等。

标签: c# for-loop restart


【解决方案1】:

我认为 for loop 在这里是错误的循环类型,它不能正确表达循环的意图,并且肯定会向我建议您不会弄乱计数器。

int i = 0;
while(i < newData.Length) 
{
    if (//Condition)
    {
       //do something with the first line
       i++;
    }
    else
    {
        i = 1;
    }
}

【讨论】:

  • 但这只是一个丑陋的for循环格式....同意OP应该购买其他东西。
  • 不,这是一个while循环。是的,您可以使用 for 循环来做到这一点,但我认为这是一种代码味道。 For 暗示做某事多次,或多次。这不是这个循环的作用。
  • +1,因为我不是聋子! :) 返回的 for 循环是一个糟糕的代码。
  • @Henk:我认为你没有听到我的声音。两个循环是否等效并不重要(我不在乎),当有人阅读代码时,他们可能会忽略 for 版本,因为它循环了很多次而没有捕捉到对 @ 的隐藏更改987654324@。我认为while 条件的明显简单性使它成为读者检查这并不像看起来那么简单的标志。恕我直言,for 版本中的结构不计入蹲下,因为您颠覆了for,它的结构打破了for 的最常见用例。
  • 这个答案是我的解决方案。谢谢!
【解决方案2】:

只需更改 for 循环的index

for (int i = 0; i < newData.Length; i++) // < instead of <= as @Rawling commented.
{
    if (//Condition)
    {
       //do something with the first line
    }
    else
    {
      // Change the loop index to zero, so plus the increment in the next 
      // iteration, the index will be 1 => the second element.
      i = 0;
    }
}

请注意,这看起来像是一个出色的意大利面条代码...更改 for 循环的索引通常表明您做错了什么。

【讨论】:

  • @Rawling。没收到你最后的评论。你什么意思?
  • 意大利面条+1。有人认为 for 循环在这里不合适,但它似乎被置若罔闻:)
  • 是的,但如果它在第二行失败,它将继续读取第二行并转到 3、4 等。基本上,如果条件失败,我需要阅读下一行。比如:第一行是 newData[0],第二行是 newData[1] 我需要重新启动循环并每次都更改 newData[i]。 PS:对不起我的英语不好
  • 撤销意味着收回/删除:)
  • 是的。我认为在这里使用 for 循环是一个很大的错误。我会尽力解决这个问题。
【解决方案3】:

只需在您的else 语句中设置i = 0;然后循环声明中的i++ 应将其设置为1,从而跳过第一行。

【讨论】:

  • 他希望 i 为 0。他最好的办法是调整数组的大小
【解决方案4】:

您只需重置 i 并调整数组大小

int length = newData.Length; // never computer on each iteration
for (int i = 0; i < length; i++)
{
    if (condition)
    {
       //do something with the first line
    }
    else
    {
      // Resize array
      string[] newarr = new string[length - 1 - i];
      /*
       * public static void Copy(
       *    Array sourceArray,
       *    int sourceIndex,
       *    Array destinationArray,
       *    int destinationIndex,
       *    int length
       * )
       */
      System.Array.Copy(newData, i, newarr, 0, newarr.Length); // if this doesn't work, try `i+1` or `i-1`
      // Set array
      newData = newarr;
      // Restart loop
      i = 0;
    }
}

【讨论】:

    【解决方案5】:
    string[] newData = File.ReadAllLines(fileName)
    
    for (int i = 0; i <= newData.Length; i++)
    {
        if (//Condition)
        {
           //do something with the first line
        }
        else
        {
          //restart the for loop BUT skip first line and start reading from the second
          i = 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多