For-each不能正确迭代

时间:2012-08-29 02:03:41

标签: java foreach

我得到了这个for-each循环没有正确迭代对象'CharacterMove'的ArrayList。我知道它不是空的,因为我在迭代之前尝试打印size()

这是for-each i have

for (CharacterMove move : charinfo.getMoves()) {

ArrayList

private List<CharacterMove> moveset = new ArrayList<>();

任何?

编辑: 这是完整的方法

public static void writeDataFromChara(CharacterInfo charinfo, String fileTarget, boolean popupFail) {
    try {
        File file = new File(fileTarget);
        PrintStream ps = new PrintStream(file);

        ps.print("<INFO>");
        ps.println();

        System.out.println("Size: " + charinfo.getMoves().size());

        //Write the information
        for (Entry<String, String> entry : charinfo.tree.getAllDatas().entrySet()) {
            ps.print(entry.getKey() + ":" + entry.getValue());
            ps.println();
        }

        //Write the moves, frames and hitboxes
        for (CharacterMove move : charinfo.getMoves()) {
            System.out.println("Z"); //Never called just like everything else
            ps.print(move.name + ":");
            ps.println();
            for (Pair<CharacterMoveFrame,Integer> frame : move.frames) {
                ps.print("frame:"); //Indicate that this is part of a new frame
                for (Entry<String, Collider> collider : frame.left.hitboxes.entrySet()) {
                    ps.print(collider.getValue().x + ",");
                    ps.println();
                    ps.print(collider.getValue().y + ",");
                    ps.println();
                    ps.print(collider.getValue().w + ",");
                    ps.println();
                    ps.print(collider.getValue().h);
                    ps.println();
                }
            }
        }
        ps.close();
    } catch (IOException e0) {
        if (popupFail) {
            JOptionPane.showMessageDialog(new JPanel(), "An error happened while writing the data", "Error", 1);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

检查此行;

   System.out.println("Size: " + charinfo.getMoves().size());

如果大小为'0',则第一个for-each循环不起作用。