无论字符大小如何生成制表符缩进数组

问题描述 投票:0回答:1

我必须创建一个返回数组的函数,该数组将在之后显示。我正在尝试使其完全缩进。这是通过String.format()实现的,但实际上取决于内容。例如,显示的点和空格小于基本字符串,因此不会缩进我的代码

enter image description here

那么,如何实现完美的缩进,无论字符大小如何?

    public static String generateArray(String[][] myArray) {
    StringBuilder result = new StringBuilder('\n');
    for (int i = 0; i < myArray.length; i++) {
        for (int j = 0; j < myArray[i].length; j++) {
            if (i == 0) {
                result.append(String.format("%-20s", myArray[i][j]));
            } else {
                result.append(String.format("%-20s", (myArray[i][j] != null ? myArray[i][j] : "...")));
            }
        }
        result.append('\n');
    }

    return result.toString();
}
java arrays indentation display
1个回答
0
投票
{code}My Code Here{code}

或:

!! This message will be all monospace.

© www.soinside.com 2019 - 2024. All rights reserved.