java - 数组在代码的一部分中省略行,但在另一部分中显示它

标签 java arrays

所以我正在创建一个 String[] ,在这部分代码中我错过了我正在检查的代码行! *请注意:我确实知道这不是最好的检查方法,但这只是一个“概念证明”,稍后会进行更改。无论如何,这是给我带来问题的代码:

private void checkMethods() {

    for (int i  = 0; i < array.length; i++) {
        String s  = array[i];
        System.out.println(s);

        if(array[i].contains("onEnable()")) {
            System.out.println("enable");
            array[i] = MethodUpdate.onEnable;
        } 

        else if (isOnDisable(s)) {
            System.out.println("disable");
            array[i] = MethodUpdate.onDisable;
        } 

        else if (isOverride(s)) {
            if (checkNextLine(array[i++])) {
                array[i] = "";
            }
        }
    }
}

现在返回以下内容:

public class DummyBukkitClass extends JavaPlugin {

  @Override
    // Some Profound code can be found here
  }

  @Override
    // The Profound code is now ending =(
  }

}

但是在下面的代码中它也返回了我需要的内容:

   public void updateFile(File file) throws IOException {
    BufferedReader br = null;
    pw = null;

    try {
        br = new BufferedReader(new FileReader("classes/DummyBukkitClass.java"));
        file.getParentFile().mkdirs();
        pw = new PrintWriter(file);

        String line;
        int index = 0;

        while ((line = br.readLine()) != null) {
            array[index] = line;
            index++;
        }


    } finally {
        checkMethods();
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
            pw.println(array[i]);
        }

        br.close();
        pw.flush();
        pw.close();
    }
}

现在返回:

public class DummyBukkitClass extends JavaPlugin {

  @Override
  public void onEnable() {
    // Some Profound code can be found here
  }

  @Override
  public void onDisable() {
    // The Profound code is now ending =(
  }

}

我只是很困惑为什么第一行省略了两行,而我在它们出现后立即调用它!它们能够在整个添加过程中被打印;我可以在调用 checkMethods() 之前打印它们。请帮忙!

谢谢你!

编辑:

要解决我需要更改的问题:

        else if (isOverride(array[i])) {
            if (checkNextLine(array[i++])) {
                array[i] = "";
            }
        }

在 checkMethods() 中:

        else if (isOverride(array[i])) {
            if (checkNextLine(array[i+1])) { //HERE
                array[i] = "";
            }
        }

其背后的原因是:我增加了“i”的大小,然后再次使用该“i”而不重置它。 “i+1”修复了这个问题,因为您没有为 i 设置新值。

最佳答案

将 checkNextLine(array[i++]) 更改为 checkNextLine(array[i + 1]),否则会跳行。

关于java - 数组在代码的一部分中省略行,但在另一部分中显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26776312/

相关文章:

c# - 循环后所有元素相同

java - 添加到 java.util.regex.Pattern 或合并它们

java - 被其他线程引用的已卸载 OSGi 包中的对象会发生什么情况?

c++ - 是否可以输入 const double 数组?

C 编程 - 将一个单词添加到数组中,然后打印数组中的内容

C:自顶向下归并排序 - 为什么无限递归?

java局部变量 - 我如何使用它的索引获取变量名或类型

java - 为什么我的 HttpClient 类卡住某些 URL

java - 泛型中的通配符 : "? super T" works while "? extends T" does not?

c++ - 复杂类型数组的静态初始化