Java toString()表示输出

Java toString()表示输出,java,tostring,Java,Tostring,我的代码中有一个bug,相信我,我已经多次运行调试来找出代码的错误,但无法理解错误所在。我必须输出2d Arraylist的内容。例如,如果我这样做: Board<String> board = new Board<String>(-4,1,-2,3,"A"); System.out.println(board); 但是,当我将第四个值3更改为更高的数字时,例如,4((-4,1,-2,4,“A”)),它会说: java.lang.IndexOutOfBoundsEx

我的代码中有一个bug,相信我,我已经多次运行调试来找出代码的错误,但无法理解错误所在。我必须输出2d Arraylist的内容。例如,如果我这样做:

Board<String> board = new Board<String>(-4,1,-2,3,"A"); 
System.out.println(board);
但是,当我将第四个值
3
更改为更高的数字时,例如,
4
(-4,1,-2,4,“A”)
),它会说:

 java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
我所有的构造函数都工作得很好,我假设bug出现在
toString()
方法中。再一次,我试着调试了好几次,但仍然不能确定这里会出什么问题。smb能帮我吗?错误发生在
toString()方法内的此行中:


考虑到增加每列的行数:

for(int j = minCol; j <= maxCol; j++){
    secondLine1 += "|  " + myBoard.get(row++).get(col++);
    secondLine2 += "+---";
}
在外环之外

或者,这里是对整个类的一个建议(请注意,我只改进了索引访问。还有足够的空间进一步改进,例如字符串连接):

公共课程委员会{
私有T元素;
私家旅馆;
私有int maxCol;
明罗私人酒店;
私有int maxRow;
私有整数行数;
私有整数计数;
私人名单委员会;
公共板(int minRow、int maxRow、int minCol、int maxCol、T fillElem){
this.minRow=minRow;
this.maxRow=maxRow;
this.minCol=minCol;
this.maxCol=maxCol;
this.rowCount=maxRow-minRow+1;
this.colCount=maxCol-minCol+1;
if(fillem==null){
抛出新的RuntimeException(“无法将元素设置为null”);
}否则{
this.element=fillem;
}
myBoard=新阵列列表(行数);
对于(int i=0;i=0){
第一个1+=“|”+i;
}否则{
第一个1+=“|”+i;
}
前2+=“+--”;
}
firstLine1+=first1+“|\n”;
firstLine2+=first2+“+\n”;
//剩下的就交给我吧!
第二行1=“”;
第二行2=“”;
对于(int row=minRow;row=0){
第二行1+=“”+行+“”;
}否则{
第二行1+=行+“”;
}

for(int column=minCol;column
for(int i=minRow;i@Andreas错误发生在toString方法中并不意味着错误也存在于toString方法中。您能否发布异常的完整堆栈跟踪,以便我们能够找到问题的确切位置?我认为错误出现在其中一个get方法中。Extract myBoard.get(row++)在一个变量中,您知道它是第一个方法还是第二个方法。请发布您的get方法实现我在哪里使用了get()方法?@typichshergw提取myBiard.get(row++)是什么意思?我刚刚添加了一个完整的解决方案,我已经为您的两个案例测试过了非常感谢!@Typichsherg。我还需要修复一些关于minRow、maxCol前面标志的其他错误。但是谢谢!我想我的问题是访问[row][col]位置的值,对吗?不客气。是和否,访问发生在[row][column]显示了这个问题,但实际的错误是在列的循环中增加了行(row++)(for(int j=minCol;j提示:为了避免类似的错误,请避免使用长而复杂的方法。将所有相关部分封装在方法中。正如您在我的解决方案中看到的,使用单独的方法访问[row][column]中的数据或者创建方法来打印一行或打印标题,等等。尝试在名称好、职责单一的方法中分离操作。->清除代码。这可以帮助您找到此类错误
for(int j = minCol; j <= maxCol; j++){
    secondLine1 += "|  " + myBoard.get(row++).get(col++);
    secondLine2 += "+---";
}
 ArrayList<T> rowCells = myBoard.get(row++);
 for(int j = minCol; j <= maxCol; j++){
       secondLine1 += "|  " + rowCells.get(col++);
       secondLine2 += "+---";
 }
row = 0;
public class Board<T> {
private T element;
private int minCol;
private int maxCol;
private int minRow;
private int maxRow;
private int rowCount;
private int colCount;
private List<List<T>> myBoard;

public Board(int minRow, int maxRow, int minCol, int maxCol, T fillElem) {
    this.minRow = minRow;
    this.maxRow = maxRow;
    this.minCol = minCol;
    this.maxCol = maxCol;
    this.rowCount = maxRow - minRow + 1;
    this.colCount = maxCol - minCol + 1;

    if (fillElem == null) {
        throw new RuntimeException("Cannot set elements to null");
    } else {
        this.element = fillElem;
    }

    myBoard = new ArrayList<List<T>>(rowCount);
    for (int i = 0; i < rowCount; i++) {
        List<T> rowLine = new ArrayList<T>(colCount);
        myBoard.add(rowLine);
        for (int j = 0; j < colCount; j++)
            rowLine.add(element);
    }
}

private T getCellValueAt(int row, int column) {
    return myBoard.get(row - minRow).get(column - minCol);
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    String result = "";
    if (this.element instanceof String) {
        String elem = (String) this.element;
        String firstLine1 = "   ";
        String firstLine2 = "   ";
        String first1 = "";
        String first2 = "";
        String secondLine1 = "";
        String secondLine2 = "   ";

        switch (elem.length()) {
        case 1:
            result = "";
            // Contructs the first two lines!
            firstLine1 = "   ";
            firstLine2 = "   ";
            first1 = "";
            first2 = "";
            for (int i = 0; i < colCount; i++) {
                if (i >= 0) {
                    first1 += "|  " + i;
                } else {
                    first1 += "| " + i;
                }
                first2 += "+---";
            }
            firstLine1 += first1 + "|\n";
            firstLine2 += first2 + "+\n";

            // Constrcuts the rest!
            secondLine1 = "";
            secondLine2 = "   ";

            for (int row = minRow; row <= maxRow; row++) {
                if (row >= 0) {
                    secondLine1 += " " + row + " ";
                } else {
                    secondLine1 += row + " ";
                }

                for (int column = minCol; column <= maxCol; column++) {
                    secondLine1 += "|  " + getCellValueAt(row, column);
                    secondLine2 += "+---";
                }
                secondLine1 += "|\n";
                secondLine1 += secondLine2 + "+\n";
                secondLine2 = "   ";

                // secondLine2 += "+\n   ";
            }

            result += firstLine1 + firstLine2 + secondLine1; // + secondLine2;
            break;
        }
        return builder.append(result).toString();
    }
    return "";
}

public static void main(String[] args) {
    Board<String> board = new Board<String>(-4, 1, -2, 4, "A");
    System.out.println(board);
}