如何调整我的 toString 方法以处理多个输入

How to adjust my toString method to work with multiple inputs

这很草率,但它适用于 printint 一个 6x6 二维数组,每边有 4 个数组:

  1 4 2 2 2 3 
 --------------
1 | 0 0 0 2 0 0  | 4
3 | 3 4 0 0 0 0  | 2
3 | 0 0 0 1 0 0  | 2
2 | 0 1 0 0 0 0  | 4
6 | 0 0 0 0 0 6  | 1
2 | 5 0 1 0 0 4  | 2
 --------------
  2 1 3 4 3 2  
null

任何人都可以帮助我调整此方法以适用于任何大小的数组吗?不只是 6x6 吗?

@Override
public String toString() {
    System.out.print(' ');
    System.out.print(' ');
    for(int i = 0; i < NS.length; i++){
        System.out.print(NS[i]);
        System.out.print(' ');
    }
    System.out.println();
    for (int i = 0; i < board.length; i++) {
        if (i % board.length == 0) {
            System.out.println(' ' + "--------------");
        }
    }
    for (int j = 0; j < board.length; j++) {
        if (j % board.length == 0) {

            for (int r = 0; r < board.length; r++) {
                System.out.print(WE[r]);
                System.out.print(" | ");
                for (int c = 0; c < board[r].length; c++) {
                    System.out.print(board[r][c]);
                    System.out.print(' ');
                }
                System.out.print(" | ");
                System.out.print(EW[r]);
                System.out.println();
                }
            }
        }
    System.out.println(' ' + "--------------");

    System.out.print(' ');
    System.out.print(' ');
    for(int i = 0; i < SN.length; i++){
        System.out.print(SN[i]);
        System.out.print(' ');
    }
    System.out.println();
    return null;
}

NS是top数组在上面

SW就是最下面的数组

WE是左边的数组

EW是右边的数组

board是二维数组

这是一个 7x7 结果:

  1 4 2 2 2 3 4 
 --------------
1 | 0 0 0 2 0 0 1  | 4
3 | 3 4 0 0 0 0 0  | 2
3 | 0 0 0 1 0 0 0  | 2
2 | 0 1 0 0 0 0 0  | 4
6 | 0 0 0 0 0 6 0  | 1
2 | 5 0 1 0 0 4 0  | 2
4 | 0 0 0 0 0 0 0  | 2
 --------------
  2 1 3 4 3 2 3 
null

这是一个 4x4 结果:

  1 4 2 2 
 --------------
1 | 0 0 0 2  | 4
3 | 3 4 0 0  | 2
3 | 0 0 0 1  | 2
2 | 0 1 0 0  | 4
 --------------
  2 1 3 4 
null

toString 需要 return 一个 String,所以你应该构建一个 StringBuffer 和 return 而不是 returning null那 - 没有 System.out.prints。水平线可以用 StringUtils.repeat.

来实现
@Override
public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("  ");
    for(int i = 0; i < NS.length; i++){
        sb.append(NS[i]+" ");
    }
    sb.append("\n");
    sb.append("   " + StringUtils.repeat("-", board.length) + "\n");

等...

    return sb.toString();
}

现在您的对象正确地实现了 toString(),您可以只打印您的对象。

System.out.println(yourObject);

我的问题解决了。我创建了一个字符串并使用基本相同的逻辑添加到它。

@Override
public String toString(){
    StringBuilder table = new StringBuilder();
    table.append("    ");

    for (int i = 0; i < NS.length; i++) {
        table.append(NS[i]);
        table.append(" ");

    }


    for (int i = 0; i < board.length; i++) {
        if (i % board.length == 0) {
            table.append("\n");
            for (int a = 0; a < 4; a++){
                table.append(" ");
            }
            for (int a = 0; a < board.length * 2; a++) {
                table.append("-");
            }

        }
    }

    table.append("\n");
    for (int j = 0; j < board.length; j++) {
        if (j % board.length == 0) {

            for (int r = 0; r < board.length; r++) {
                table.append(WE[r]);
                table.append(" | ");

                for (int c = 0; c < board[r].length; c++) {
                    table.append(board[r][c]);
                    table.append(" ");

                }
                table.append("| ");
                table.append(EW[r]);
                table.append("\n");

            }
        }
    }
    for (int i = 0; i < board.length; i++) {
        if (i % board.length == 0) {

            for (int a = 0; a < 4; a++){
                table.append(" ");
            }
            for (int a = 0; a < board.length * 2; a++) {
                table.append("-");
            }

        }
    }
    table.append("\n");
    table.append("    ");

    for (int i = 0; i < SN.length; i++) {
        table.append(SN[i]);
        table.append(" ");


    }
    return table.toString();

}