NoSuchElementExists错误...是因为迭代器中的每个都有一个?

时间:2014-04-29 06:31:18

标签: java

NoSuchElementExists错误,以及一些AbstractList警告......是因为迭代器中的每个都有一个?

while(it.hasNext()) {
                for(Square s : ChessBoard.BOARD_SQUARES) {
                    int[] move = it.next();
                    // Match moves to squares
                    if(Arrays.equals(s.getPosition(), move)) {
                        // Remove moves if they are capturing your own pieces
                        if(s.hasPiece()) {
                            boolean capturingYourOwnPiece = ((s.getPiece()).getTeamColor()).equals(ChessBoard.getTurn());
                            if(capturingYourOwnPiece) {
                                it.remove();
                            }
                        // Remove diagonal moves if no adjacent pieces
                        } else {
                            if(move[0]!=0) {
                                it.remove();
                            }
                        }
                    }

                    // If it passes tests, then add it to the official list
                    // of legal moves
                    listOfLegalSquares.add(ChessBoard.getSquare(move));
                }
            }

2 个答案:

答案 0 :(得分:3)

您在for循环中调用it.next(),这意味着每个it.hasNext()

可能会多次调用它

正确使用迭代器是为每it.next()次呼叫it.hasNext()

答案 1 :(得分:0)

理想情况下,你应该只在内部调用.next,或者在执行之前调用chNe hasNext