java - 更新 ArrayList

标签 java arrays list arraylist

每当玩家输入数字时,我都会尝试更新我的棋盘(由数组列表中的三个数组列表组成)。该数字与棋盘上的一个正方形的对应关系如下:

1 2 3
4 5 6
7 8 9

我在更新网格时遇到问题。

函数

public static void playBoard(int choice, ArrayList<ArrayList<String>> board, boolean playerTurn) {
    String val;
    if (!playerTurn) {
        val = "| X |";
    }
    else {
        val = "| O |";
    }
    if (choice>=1 && choice<=3) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(0);
        if (choice ==3) {
            val+="\n";
        }
        updateRow.set(choice-1, val);
        System.out.println(updateRow);
        board.set(0, updateRow);
        System.out.println(display(board));
    }
    else if (choice>=4 && choice<=6) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(1);
        if (choice ==6) {
            val+="\n";
        }
        updateRow.set((choice-4), val);
        board.set(1, updateRow);
        System.out.println(display(board));
    }
    else if (choice>=7 && choice<=9) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(2);
        if (choice ==9) {
            val+="\n";
        }
        updateRow.set(choice-7, val);
        board.set(2, updateRow);
        System.out.println(display(board));
    }
    else {
        System.out.println("Input out of range");
        return;
    }
}

问题在于,当用户输入一个值时,该值对应的整个列都会更新,而不是单个方 block 。

我已经检查过:

  • 仅触发一个 if 语句。
  • 更新仅发生一次
  • 更新发生在正确的索引上。

通过我的调试,我认为问题所在是:

updateRow.set(choice-1, val);

当用户(玩家1)输入1时:

预期输出

| X || - || - |
| - || - || - |
| - || - || - |

实际输出

| X || - || - |
| X || - || - |
| X || - || - |

显示功能

抱歉,我没有意识到你们需要看到这个其他功能

    public static String display(ArrayList<ArrayList<String>> board) {
    StringBuilder builder = new StringBuilder();
    for (ArrayList<String> row : board) {
        for (String space: row) {
            builder.append(space);
        }
    }
    String text = builder.toString();
    return text;
}

最佳答案

问题出现在创建中:您可能为每一行使用相同的列 ArrayList 对象。

// Error:
ArrayList<String> row = new ArrrayList<>();
row.add("...");
row.add("...");
row.add("...");
for (int i = 0; i < 3; ++i) {
    board.add(row);
}

应该是:

for (int i = 0; i < 3; ++i) {
    ArrayList<String> row = new ArrrayList<>();
    row.add("...");
    row.add("...");
    row.add("...");
    board.add(row);
}

同样的概念错误意味着:不需要这样做:

board.set(2, updateRow); // Not needed.

更改板持有的 updateRow 对象中的条目是通过引用完成的。

一些提示:

  • 这里可以使用String[][]
  • 将显示/ View (字符串)与数据模型(字符?)分开会更容易, 所以也许 char[][] board = new char[3][3];

关于java - 更新 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58604443/

相关文章:

python - 函数中只允许循环奇数

通用列表中的 C-Void 指针(包含结构)

java - 计算字符串中第一个与负数的运算

java - Spring Boot Apache Camel Quartz JDBC 调度器

java - 如何使用 applicationContext.xml 中的嵌套属性构造一个 bean?

javascript - 正确的 MongoDB 集合结构,包括 Meteor 中的值数组(无键)

c# - LINQ List 搜索多个列表。返回单个列表

java - sqlite 表存储第一条记录,但不存储之后的记录

java - 如何正确使用 clone() 方法?

javascript - 在 JavaScript 中复制卡住数组有什么缺点吗?