toString() 表示输出
toString() representation output
我的代码有一个错误,请相信我,我已经 运行 调试了很多次以找出代码的问题,但无法理解问题所在。我必须输出 2d Arraylist 的内容。例如,如果我这样做:
Board<String> board = new Board<String>(-4,1,-2,3,"A");
System.out.println(board);
它输出以下内容:
| -2| -1| 0| 1| 2| 3|
+---+---+---+---+---+---+
-4 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-3 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-2 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-1 | A| A| A| A| A| A|
+---+---+---+---+---+---+
0 | A| A| A| A| A| A|
+---+---+---+---+---+---+
1 | A| A| A| A| A| A|
+---+---+---+---+---+---+
但是当我将第四个值 3
更改为更大的数字时,例如 4
((-4,1,-2,4,"A")
),然后它显示:
java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
我的所有构造函数都运行良好,我认为错误出在 toString()
方法中。再一次,我试过多次调试它,但仍然无法确定这里可能出什么问题。 smb可以帮帮我吗?错误发生在 toString()
方法内的这一行:
你增加每列的行数:
for(int j = minCol; j <= maxCol; j++){
secondLine1 += "| " + myBoard.get(row++).get(col++);
secondLine2 += "+---";
}
这也是它适用于#rows >= #columns 但不适用于#rows < #columns
的原因
在内部 for 循环之前提取 myBoard.get(row++) 作为变量,如
ArrayList<T> rowCells = myBoard.get(row++);
for(int j = minCol; j <= maxCol; j++){
secondLine1 += "| " + rowCells.get(col++);
secondLine2 += "+---";
}
并移动
row = 0;
跳出外循环。
或者,这里有一个整体的建议class(请注意,我只改进了索引访问。还有足够的空间进一步改进,例如字符串连接):
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);
}
}
我的代码有一个错误,请相信我,我已经 运行 调试了很多次以找出代码的问题,但无法理解问题所在。我必须输出 2d Arraylist 的内容。例如,如果我这样做:
Board<String> board = new Board<String>(-4,1,-2,3,"A");
System.out.println(board);
它输出以下内容:
| -2| -1| 0| 1| 2| 3|
+---+---+---+---+---+---+
-4 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-3 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-2 | A| A| A| A| A| A|
+---+---+---+---+---+---+
-1 | A| A| A| A| A| A|
+---+---+---+---+---+---+
0 | A| A| A| A| A| A|
+---+---+---+---+---+---+
1 | A| A| A| A| A| A|
+---+---+---+---+---+---+
但是当我将第四个值 3
更改为更大的数字时,例如 4
((-4,1,-2,4,"A")
),然后它显示:
java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
我的所有构造函数都运行良好,我认为错误出在 toString()
方法中。再一次,我试过多次调试它,但仍然无法确定这里可能出什么问题。 smb可以帮帮我吗?错误发生在 toString()
方法内的这一行:
你增加每列的行数:
for(int j = minCol; j <= maxCol; j++){
secondLine1 += "| " + myBoard.get(row++).get(col++);
secondLine2 += "+---";
}
这也是它适用于#rows >= #columns 但不适用于#rows < #columns
的原因在内部 for 循环之前提取 myBoard.get(row++) 作为变量,如
ArrayList<T> rowCells = myBoard.get(row++);
for(int j = minCol; j <= maxCol; j++){
secondLine1 += "| " + rowCells.get(col++);
secondLine2 += "+---";
}
并移动
row = 0;
跳出外循环。
或者,这里有一个整体的建议class(请注意,我只改进了索引访问。还有足够的空间进一步改进,例如字符串连接):
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);
}
}