小编Mat*_*rew的帖子

在java中加载2d数组的所有值

我正在尝试创建一个2D拼图滑块游戏.我创建了自己的对象gamestate来存储父游戏状态和新的游戏状态,因为我计划使用BFS解决它.示例数组看起来像

int[][] tArr = {{1,5,2},{3,4,0},{6,8,7}};
Run Code Online (Sandbox Code Playgroud)

这暗示着

[1,5,2,3,4,0,6,8,7]

为了存储这个状态,我使用了以下for循环,它带来了indexOutOfBounds exceptions.

public class GameState {
public int[][] state; //state of the puzzle
public GameState parent; //parent in the game tree

public GameState() {
    //initialize state to zeros, parent to null
    state = new int[0][0];
    parent = null;
}

public GameState(int[][] state) {
    //initialize this.state to state, parent to null
    this.state = state;

    parent = null;
}

public GameState(int[][] state, GameState parent) {
    //initialize this.state to state, this.parent to parent
    this.state …
Run Code Online (Sandbox Code Playgroud)

java arrays 2d indexoutofboundsexception

5
推荐指数
1
解决办法
87
查看次数

标签 统计

2d ×1

arrays ×1

indexoutofboundsexception ×1

java ×1