Java 移动二维数组中的字符和异常

Java moving character in 2d array and exceptions

我有这个二维数组:

public int[][] intmap =
        {
                {0, 0, 0, 0, 0},
                {0, 1, 2, 0, 0},
                {0, 0, 2, 0, 0},
                {0, 3, 0, 0, 0},
                {0, 0, 0, 3, 3}
        };

如何在不踩到数字2或3的情况下移动数组中的数字1? 如果下一步是其中之一,我想打印出一条警告消息。我尝试使用异常,因为我不想离开地图。 这是一个方向的例子(来自我的代码):

public void goRight() {
    try {
        if (intmap[i][j + 1] != 2 && intmap[i][j + 1] != 3) {
            intmap[i][j] = previousNumber;
            previousNumber = intmap[i][j + 1];
            intmap[i][j] = 1;
        } else {
            System.out.println("You can not move here!");
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("You can not move outside the the map!");
    }
}

我尝试在开始时使用 ++j,但在没有 if() 的情况下效果很好。它没有让我离开地图,但我想解决“数字 2 和 3”的问题。所以我做了一个 if(){}else{} 但效果不是很好。我的朋友告诉我我应该使用 j+1 而不是 ++j 但在这种情况下角色不会移动。 previousNumber 是一个 int,我在其中存储数字 1 所在的数字。默认情况下,我给 int previousNumber = 0。 我能够调用方向方法并打印出数组。

我在 if 语句中使用 ++j 而不是 j+1

public void goRight() {
    try {
        if (intmap[i][j + 1] != 2 && intmap[i][j + 1] != 3) {
            intmap[i][j] = previousNumber;
            previousNumber = intmap[i][++j];
            intmap[i][j] = 1;
        } else {
            System.out.println("You can not move here!");
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("You can not move outside the the map!");
    }
}