Java 在给定行和列位置设置给定元素

Java 在给定行和列位置设置给定元素,java,Java,我有2D ArrayList和一个方法public void set(int row,int col,tx),它将这个元素更改为x。当我尝试运行代码时,它会给我java.lang.ArrayIndexOutOfBoundsException,我不明白为什么,因为我已经初始化了我的电路板: Board board = new Board(-2,3,-4,3,"xxx"); 其中构造函数值是 新板(int-minRow、int-maxRow、int-minCol、int-maxCol、te) ja

我有2D ArrayList和一个方法
public void set(int row,int col,tx)
,它将
这个元素
更改为
x
。当我尝试运行代码时,它会给我
java.lang.ArrayIndexOutOfBoundsException
,我不明白为什么,因为我已经初始化了我的电路板:

Board board = new Board(-2,3,-4,3,"xxx");
其中构造函数值是

新板(int-minRow、int-maxRow、int-minCol、int-maxCol、te)

java.lang.ArrayIndexOutOfBoundsException
发生在以下行中的
set()
方法中:

if(myBoard.get(row).get(col).equals(this.element))
为什么我会犯这样的错误?我是否正确访问ArrayList中的行和列编号


我的
toString()
在执行
board.set时表示
,您正试图访问
ArrayList
索引-1处的元素
row
set()
中是-1,在
if
语句中基本上调用
myBoard.get(-1)

您需要记住,
ArrayList
在索引方面是基于0的。要解决此问题,请从-1中减去最小索引以获得正确的索引。这也适用于列索引。例如,给定

Board board = new Board(-2,3,-4,3,"xxx");
你需要做什么

int row = (-1) - (-2);     //row you want - lowest row (row at index 0 in the ArrayList)
int col = (0) - (-4);      //col you want - lowest col (col at index 0 in the ArrayList)
board.set(row, col, "A");

负索引对于Java中的所有有序数据结构都无效。你的
Board
类应该将索引重写为正数,以向调用者隐藏这个古怪的逻辑:

public void set(int virtualRow, int virtualCol, T x) {
    int row = translateIndex(minRow, virtualRow, maxRow);
    int col = translateIndex(minCol, virtualCol, maxCol);

    // this.expandToInclude(row, col);
    if (myBoard.get(row).get(col).equals(this.element)) {
        myBoard.get(row).set(col, x);
        RowColStack<T> temp = new RowColStack<T>(row, col, x);
        undoStack.add(temp);
    }
}

private int translateIndex(int min, int index, int max) {
    if (index< min || row > max) throw new ArrayOutOfBoundException();
    return index - min;
}
公共无效集(int-virtualRow、int-virtualCol、tx){
int row=translateIndex(minRow、virtualRow、maxRow);
int col=translateIndex(minCol、virtualCol、maxCol);
//此项扩展包括(行、列);
if(myBoard.get(row.get(col.equals)(this.element)){
myBoard.get(row.set)(col,x);
RowColStack temp=新的RowColStack(行、列、x);
撤消堆栈。添加(临时);
}
}
私有int-translateIndex(int-min、int-index、int-max){
如果(索引max)抛出新的ArrayOutOfBoundException();
返回指数-最小值;
}
两条评论:

  • 一旦您知道了电路板的大小(在程序执行期间似乎不会改变),使用2D数组而不是
    ArrayList
    可能会简单得多
  • 您没有在初始化中创建数组列表中的元素。因此,实际上您有一个0x0数组,并且将接收任何get调用的
    java.lang.ArrayIndexOutOfBoundsException
  • 当然,您可以接收负索引,但在访问其他答案中提到的元素之前,请将它们转换为适当的索引到数组/arraylist中
  • 您可以使用如下类似的逻辑初始化阵列列表:

    for(int i = 0; i < nRows; i++) {
       rowList = new ArrayList();
       myboard.add(rowList); 
       for(int j = 0; j < nCols; j++)
          rowList.add(null);
       }
    }
    
    for(int i=0;i

    将其视为伪代码并在此基础上构建。

    是什么让你认为列表接受负索引?等等,它不???@DiciOf当然不,所有有序集合和数组都是0索引的(它们从0开始)使负数成为呈现问题,而不是数据结构问题。@Dici,我能问你smth吗?在my code
    myBoard
    中表示
    2D Arraylist
    以及当我执行smth时,如
    myBoard.get(row).set(col,x)
    那么它如何知道
    myBoard
    包含该元素。因为我在代码中只在构造函数中声明了
    myBoard
    ,就是这样。有什么我不明白的吗?这个索引太混乱而且容易出错。如果他需要负指数,那么
    类需要知道如何将负指数重写为正指数,但这不应该由用户来做it@Dici我同意。我只是想指出他的错误所在,并推动他朝着正确的方向开始制定解决方案,而不是提供最佳和最完美的解决方案。嗯,好吧,这是一个选择+那就是我主要关心的问题。如何在初始化过程中在arraylist中创建元素@KDMIf有
    T元素
    我可以在for循环中添加(j,this.element)
    行列表吗@KDM@John你应该能够。