用数组迭代 ArrayList

问题描述 投票:0回答:4

我正在使用数组迭代

List
的索引。这行代码

score += SquareTable[index.get(i)];

产生错误,我不知道如何修复它。

@Override
public int evaluatePieceScore() {

    int score = 0;
    List<Piece> index = chessgame.getPieces();

    
    for (Piece piece : this.chessgame.getPieces()){
        if(piece.getColor() == Piece.YELLOW_COLOR){
            
            
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_PAWN),   PawnSquareTable);
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KNIGHT), KnightSquareTable);
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_BISHOP), BishopSquareTable);               
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_ROOK),   RookSquareTable);
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_QUEEN),  QueenSquareTable);
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING),   KingMiddleGameSquareTable);
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING),   KingEndGameSquareTable);
            
            
        }else if(piece.getColor() == Piece.BROWN_COLOR){
            
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_PAWN),   reverse(PawnSquareTable));
            score += this.CalculateScore(index ,getPieceValue(Piece.TYPE_KNIGHT), reverse(KnightSquareTable));
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_BISHOP), reverse(BishopSquareTable));              
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_ROOK),   reverse(RookSquareTable));
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_QUEEN),  reverse(QueenSquareTable));
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING),   reverse(KingMiddleGameSquareTable));
            score += this.CalculateScore(index, getPieceValue(Piece.TYPE_KING),   reverse(KingEndGameSquareTable));
            
            
        }else{
            throw new IllegalStateException("Unknown piece color found: "+piece.getColor());
        }
    }       
    return score;
}

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable ){
    
    int score = pieceValue * index.size();
    
    for(int i = 0; i < index.size(); i++){
        score += SquareTable[index.get(i)];
    }
    
    return score;
}
java arraylist iterator
4个回答
0
投票

您正在尝试使用

SquareTable
实例(来自列表)而不是
Piece
来访问
int
数组。

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable ){

 SquareTable[index.get(i)];

0
投票
index.get(i) gives you an object of type `Piece` 

并根据此代码

int[] SquareTable
,

SquareTable
接受类型
int

这就是给你的

TypeMismatchError


0
投票

您在实现中弄乱了集合,看起来像这样

public int CalculateScore(List<Piece> index, int pieceValue, int[] SquareTable ){    
    int score = pieceValue * index.size();

    for(int i = 0; i < SquareTable.size(); i++){
        score += getPieceValue(index.get(SquareTable[i]));
    }

    return score;
}

0
投票

index.get(I) 获取 Piece 对象,因此您不能使用它来访问数组中的元素。

© www.soinside.com 2019 - 2024. All rights reserved.