我必须创建一个返回数组的函数,该数组将在之后显示。我正在尝试使其完全缩进。这是通过String.format()
实现的,但实际上取决于内容。例如,显示的点和空格小于基本字符串,因此不会缩进我的代码
那么,如何实现完美的缩进,无论字符大小如何?
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();
}
{code}My Code Here{code}
或:
!! This message will be all monospace.