Vector.remove()for循环

Aar*_*ron 3 java for-loop file vector

我正在编写游戏,几乎完成了保存文件系统.我有两个Vector(一个包含savegame 的名称,一个包含sessionID).

在启动时,程序将从文件中读取数据并将该信息添加到向量中.然后调用另一个方法来检查Vector中显示的文件是否存在.如果没有,它们将从矢量中删除.最后,Vector被打印到并重写文件.

我遇到的问题是for循环不检查Vector中的每个项目,因为当项目被删除时Vector.size()正在减少.是否有更好的方法来形成for循环,或者是否有解决方法我可以使用?

private static void slistCleanup() throws IOException {

          private static Vector<String> saveNames = new Vector<String>();
          private static Vector<Integer> sessionIDs = new Vector<Integer>();

    Scanner slistReader = new Scanner(new FileReader(sessionList));
    File tempSave;
    String path;
    int run = 1;
    String tempName = " ";
    int tempID = 0;

    for (int x = 0; x < saveNames.size(); x++) {

        path = currentDir + "\\saves\\" + sessionIDs.elementAt(x) + ".sav";
        tempSave = new File(path);

        System.out.println("-----------------------"); //debug
        System.out.println("current pass: " + run);
        System.out.println("tempSave Path: " + tempSave.getAbsolutePath()); //debug
        System.out.println("tempSave exists: " + tempSave.exists()); //debug
        System.out.println("-----------------------"); //debug
        run++; //debug

        if (!tempSave.exists()) {

            saveNames.remove(x);
            sessionIDs.remove(x);
        }
    }

    for (int x = 0; x < saveNames.size(); x++) {

        System.out.println(saveNames.elementAt(x));
        System.out.println(sessionIDs.elementAt(x));
    }

    slistReader.close();
}
Run Code Online (Sandbox Code Playgroud)

如果您需要更多代码,请告诉我们.

Vin*_*ele 10

向后循环:

for (int x = saveNames.size()-1; x >= 0; x--)
Run Code Online (Sandbox Code Playgroud)