gpt4 book ai didi

java - 实例化多个相同的对象

转载 作者:行者123 更新时间:2023-11-30 07:27:28 25 4
gpt4 key购买 nike

在实例化同一类型的多个对象时,我遇到了一个奇怪的问题。

我正在尝试将新创建的对象解析为 LinkedList queue 。所以问题是,它应该解析第一个对象 tempBoardDirection在第一个 if 语句中放入队列,然后解析第二个对象 tempBoardDirectionLeft进入队列。

出错的部分是它看起来更像是在改变我的 tempBoard对象,因为当我打印该字段时,我正在更改它,它在这 3 个对象中始终相同。

因为它们互相引用,所以我在哪里犯了错误?

我尝试查看tempBoard.robots[0] ,在第一个 if 语句之前、之后以及第二个 if 语句之后,这就是数据:

  • 在第一个 if 语句之前 - java.awt.Point[x=0,y=1]
  • 第一个 if 语句之后 - java.awt.Point[x=0,y=4]
  • 第二个 if statmenet 之后 - java.awt.Point[x=0,y=0]

不应从 java.awt.Point[x=0,y=1] 更改此值,因为我正在创建新对象并编辑其属性,而不是 tempBoard属性。

这是函数的一个片段,应该解释它出错的地方:

while(!q.isEmpty()){
Board tempBoard = q.poll();

if(tempBoard.isGoal())
return tempBoard.moves;

Endpoints endpoint = tempBoard.possibleEndpointsForRobot(0);
System.out.println(tempBoard.robots[0]);

if(!visited[endpoint.right.x][endpoint.right.y]){
Board tempBoardDirection = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirection.moves = tempBoard.moves;
tempBoardDirection.moveRobot(0,Direction.Right);
visited[endpoint.right.x][endpoint.right.y] = true;
q.add(tempBoardDirection);
System.out.println("right: "+tempBoardDirection.robots[0]);
}

if(!visited[endpoint.left.x][endpoint.left.y]){
Board tempBoardDirectionLeft = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirectionLeft.moves = tempBoard.moves;
tempBoardDirectionLeft.moveRobot(0,Direction.Left);
visited[endpoint.left.x][endpoint.left.y] = true;
q.add(tempBoardDirectionLeft);
System.out.println("Left: "+tempBoardDirectionLeft.robots[0]);
}

这是失败的整个函数:

public String computeSolution() throws Exception {
Queue<Board> q = new LinkedList<>();

q.add(this.board);
this.visited[this.board.robots[0].x][this.board.robots[0].y] = true;

int i = 0;

while(!q.isEmpty()){
Board tempBoard = q.poll();

if(tempBoard.isGoal())
return tempBoard.moves;

Endpoints endpoint = tempBoard.possibleEndpointsForRobot(0);
System.out.println(tempBoard.robots[0]);

if(!visited[endpoint.right.x][endpoint.right.y]){
Board tempBoardDirection = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirection.moves = tempBoard.moves;
tempBoardDirection.moveRobot(0,Direction.Right);
visited[endpoint.right.x][endpoint.right.y] = true;
q.add(tempBoardDirection);
System.out.println("right: "+tempBoardDirection.robots[0]);
}

if(!visited[endpoint.left.x][endpoint.left.y]){
Board tempBoardDirectionLeft = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirectionLeft.moves = tempBoard.moves;
tempBoardDirectionLeft.moveRobot(0,Direction.Left);
visited[endpoint.left.x][endpoint.left.y] = true;
q.add(tempBoardDirectionLeft);
System.out.println("Left: "+tempBoardDirectionLeft.robots[0]);
}

if(!visited[endpoint.up.x][endpoint.up.y]){
System.out.println("works up");
Board tempBoardDirection = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirection.moves = tempBoard.moves;
tempBoardDirection.moveRobot(0,Direction.Up);
visited[endpoint.up.x][endpoint.up.y] = true;
q.add(tempBoardDirection);
}

if(!visited[endpoint.down.x][endpoint.down.y]){
System.out.println("works down");
Board tempBoardDirection = new Board(tempBoard.board, tempBoard.robots, tempBoard.goal);
tempBoardDirection.moves = tempBoard.moves;
tempBoardDirection.moveRobot(0,Direction.Down);
visited[endpoint.down.x][endpoint.down.y] = true;
q.add(tempBoardDirection);
}
//System.out.println(tempBoard.moves);
i++;
}

System.out.println("num of loops: "+i);
throw new Exception("no solution");
}

更新1:

这是 Board 类,按照要求我也应该放在这里。

public class Board {
public static final char GOAL_CHAR = 'G';
public static final char WALL_CHAR = '#';
public static final char EMPTY_CHAR = ' ';

public char[][] board;
public int size;
public Point[] robots;
public Point goal;
public String moves = "";
public boolean[][] visited;

public Board(char[][] board, Point[] robots, Point goal) {
this.board = board;
this.size = board.length;
this.robots = robots;
this.goal = goal;
this.visited = new boolean[this.size][this.size];
}


public Integer getCurrentRobotFromQueue(Queue q) {
return IntStream.range(0, robots.length).filter(i -> q.element().equals(robots[i])).findFirst().getAsInt();
}

public boolean isGoal() {
return this.robots[0].equals(this.goal);
}

public Endpoints possibleEndpointsForRobot(int robot) {
assert robot < robots.length;

Point up = pointAfterMovingRobot(robot, Direction.Up);
Point down = pointAfterMovingRobot(robot, Direction.Down);
Point left = pointAfterMovingRobot(robot, Direction.Left);
Point right = pointAfterMovingRobot(robot, Direction.Right);

return new Endpoints(up, down, left, right);
}

public Point pointAfterMovingRobot(int robot, Direction m) {
assert robot < robots.length;

Point pos = new Point(robots[robot]);
int drow = 0, dcol = 0;

if (m == Direction.Up) {
drow = -1;
} else if (m == Direction.Down) {
drow = 1;
}

if (m == Direction.Left) {
dcol = -1;
} else if (m == Direction.Right) {
dcol = 1;
}

while (withinBoard(pos.x+drow, pos.y+dcol) &&
(board[pos.x+drow][pos.y+dcol] == EMPTY_CHAR
|| board[pos.x+drow][pos.y+dcol] == GOAL_CHAR)) {
pos.translate(drow, dcol);
}

return pos;
}

public void moveRobot(int robot, Direction d) {
assert robot < robots.length;

Point from = robots[robot];
Point to = pointAfterMovingRobot(robot, d);
board[from.x][from.y] = EMPTY_CHAR;
robots[robot] = to;
board[to.x][to.y] = (char) ('0' + robot);

//add move to moves.

this.moves += robot+d.toString()+", ";
}

private boolean withinBoard(int x, int y) {
return x >= 0 && x < size && y >= 0 && y < size;
}

public String toString() {
StringBuilder sb = new StringBuilder(size * size);

for (int i = 0; i < board.length; i++) {
sb.append(board[i]);
sb.append(System.getProperty("line.separator"));
}

return sb.toString();
}
}

最佳答案

我找到了解决方案。问题是我需要在创建对象的新实例之前创建信息的副本。

我解决这个问题的方法是创建一个新的父类(super class)并这样做:

public Board(Board tempBoard){
this.board = new char[tempBoard.board.length][tempBoard.board.length];
this.robots = new Point[tempBoard.robots.length];
System.arraycopy( tempBoard.board, 0, board, 0, tempBoard.board.length );
System.arraycopy( tempBoard.robots, 0, robots, 0, tempBoard.robots.length );
this.size = tempBoard.size;
this.goal = tempBoard.goal;
this.moves = tempBoard.moves;
}

关于java - 实例化多个相同的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36631382/

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