gpt4 book ai didi

java - 在 Java 中克隆列表

转载 作者:行者123 更新时间:2023-12-01 21:12:51 25 4
gpt4 key购买 nike

我正在对象中开发一个函数,该函数接受对象列表作为参数并将其内容克隆到自己的列表中。修改新列表不应影响传入的列表。我知道列表将通过引用传递,但是列表中的对象是通过引用还是值传递? (抱歉,如果这听起来很愚蠢)

我正在传递一个扩展 Piece 类的棋子列表(pawn、rook 等)。我正在考虑在 Piece 类中创建一个 clonePiece() 函数,但我不知道如何去做。这是我到目前为止所拥有的:

    public void copyPieces(List<Piece> whitePieces, List<Piece> blackPieces){
for (int i = 0; i < whitePieces.size(); i++){
this.whitePieces.add(whitePieces.get(i).clonePiece());
}
for (int i = 0; i < blackPieces.size(); i++){
this.whitePieces.add(blackPieces.get(i).clonePiece());
}

您将如何在抽象类中实现clonePiece()函数来创建其继承类的新实例?

编辑:

public abstract class Piece {
private int color;
private int x;
private int y;

public Piece (int color, int x, int y){
this.color = color;
this.y = y;
this.x = x;
}

public int getColor(){
return this.color;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}

public void move(int x, int y, Board board){
board.getGameTiles()[this.x][this.y].setToUnoccupied();
this.x = x;
this.y = y;
}

public abstract ArrayList<Move> getMoves(Board board);

public Piece clonePiece(){
return this;
}

}

public class Rook extends Piece{

int x, y, color;
private ArrayList<Move> moves;

public Rook(int color, int x, int y) {
super(color, x, y);
this.x = x;
this.y = y;
this.color = color;
moves = new ArrayList<>();
}

@Override
public ArrayList<Move> getMoves(Board board) {

//moves right
int a = 1;
while(UtilFunctions.isInBoundaries(x+a, y)){
if(!board.getGameTiles()[x+a][y].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x+a, y, 0));
}
else{
if(board.getGameTiles()[x+a][y].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x+a, y, 1));
}
break;
}
a++;
}

//moves left
a = -1;
while(UtilFunctions.isInBoundaries(x+a, y)){
if(!board.getGameTiles()[x+a][y].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x+a, y, 0));
}
else{
if(board.getGameTiles()[x+a][y].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x+a, y, 1));
}
break;
}
a++;
}

//moves up
a = 1;
while(UtilFunctions.isInBoundaries(x, y+a)){
if(!board.getGameTiles()[x][y+a].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x, y+a, 0));
}
else{
if(board.getGameTiles()[x][y+a].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x, y+a, 1));
}
break;
}
a++;
}

//moves down
a = -1;
while(UtilFunctions.isInBoundaries(x, y+a)){
if(!board.getGameTiles()[x][y+a].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x, y+a, 0));
}
else{
if(board.getGameTiles()[x][y+a].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x, y+a, 1));
}
break;
}
a++;
}

return moves;
}

}

最佳答案

让片段返回自己的#copy方法(或使用Clonable):

public abstract class Piece {

public Piece copy() {
//return copy
}
}

这可以类似地在子类中使用:

public class Rook extends Piece {

@Override
public Rook copy() {
//return copy
}
}

然后,当您有一个 Piece 对象列表时,您只需对这些对象调用 #copy 并替换列表中的引用即可。例如:

List<Piece> pieces = /* some list */;
List<Piece> copy = new ArrayList<>(pieces);
copy.replaceAll(Piece::copy); //replace references with copies

关于java - 在 Java 中克隆列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40832770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com