在列中打印Java数组

在列中打印Java数组,java,arrays,format,Java,Arrays,Format,我试图用Java格式化两个数组,以打印如下内容: Inventory Number Books Prices ------------------------------------------------------------------ 1 Intro to Java $45.99 2 Intro to C++

我试图用Java格式化两个数组,以打印如下内容:

Inventory Number      Books                          Prices
------------------------------------------------------------------
1                     Intro to Java                  $45.99
2                     Intro to C++                   $89.34
3                     Design Patterns                $100.00
4                     Perl                           $25.00
/**
 * Determines maximum length of all given strings.
 */
public static int maxLength(int padding, String... array) {
    if (array == null) return padding;
    int len = 0;
    for (String s : array) {
        if (s == null) {
            continue;
        }
        len = Math.max(len, s.length());
    }
    return len + padding;
}

/**
 * Unifies array element lengths and appends 3 whitespaces for padding.
 */
public static String[] format(String[] array) {
    if (array == null || array.length == 0) return array;
    int len = maxLength(3, array);
    String[] newArray = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        newArray[i] = array[i];
        while (newArray[i].length() < len) {
            newArray[i] += " ";
        }
    }
    return newArray;
}
我正在使用以下代码:

for(int i = 0; i < 4; i++) {
        System.out.print(i+1);
        System.out.print("                     " + books[i] + " ");
        System.out.print("                 " + "$" + booksPrices[i] + " ");
        System.out.print("\n");
    }
我如何将所有列直接排列在顶部标题下


有没有更好的方法来实现这一点?

您应该看看格式:

System.out.format("%15.2f", booksPrices[i]);   
这将提供15个插槽,并在需要时填充空间

但是,我注意到您没有正确地对数字进行对正,在这种情况下,您希望在books字段中使用左对正:

System.out.printf("%-30s", books[i]);
下面是一个工作片段示例:

String books[] = {"This", "That", "The Other Longer One", "Fourth One"};
double booksPrices[] = {45.99, 89.34, 12.23, 1000.3};
System.out.printf("%-20s%-30s%s%n", "Inventory Number", "Books", "Prices");
for (int i=0;i<books.length;i++){
    System.out.format("%-20d%-30s$%.2f%n", i, books[i], booksPrices[i]);
}
你可以用

System.out.printf(...)
用于格式化输出。使用

String.format(...)
哪个使用

java.util.Formatter
您可以找到文档

要对齐简单字符串,可以使用以下命令:

String formatted = String.format("%20s", str)
这将提前结束

20 - str.length
在实际字符串之前留空,并返回填充字符串。 例如,如果字符串是“Hello,World!”,它将在11个空格前加上前缀:

"           Hello, World!"
要使某个内容左右对齐,必须在指示结果字符串长度的数字前加“-”


要安全地对齐所有内容,首先必须找出最长的字符串。

在不使用任何库的情况下,需要确定每个列的最大长度,并适当地附加所需的空格字符。例如:

Inventory Number      Books                          Prices
------------------------------------------------------------------
1                     Intro to Java                  $45.99
2                     Intro to C++                   $89.34
3                     Design Patterns                $100.00
4                     Perl                           $25.00
/**
 * Determines maximum length of all given strings.
 */
public static int maxLength(int padding, String... array) {
    if (array == null) return padding;
    int len = 0;
    for (String s : array) {
        if (s == null) {
            continue;
        }
        len = Math.max(len, s.length());
    }
    return len + padding;
}

/**
 * Unifies array element lengths and appends 3 whitespaces for padding.
 */
public static String[] format(String[] array) {
    if (array == null || array.length == 0) return array;
    int len = maxLength(3, array);
    String[] newArray = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        newArray[i] = array[i];
        while (newArray[i].length() < len) {
            newArray[i] += " ";
        }
    }
    return newArray;
}
/**
*确定所有给定字符串的最大长度。
*/
公共静态int maxLength(int填充、字符串…数组){
if(array==null)返回填充;
int len=0;
for(字符串s:数组){
如果(s==null){
继续;
}
len=Math.max(len,s.length());
}
返回len+填充;
}
/**
*统一数组元素长度并附加3个空格作为填充。
*/
公共静态字符串[]格式(字符串[]数组){
if(array==null | | array.length==0)返回数组;
int len=最大长度(3,数组);
String[]newArray=新字符串[array.length];
for(int i=0;i
您好,请尝试使用
\t
。例如:
System.out.println(String.format(“\t%s”,books[i]);
对于双制表符,可以任意使用\t\t等等,这实际上比我在注释中写的要好。+1重复输入“-”向左对齐。或者,您也可以使用
String.format
,而不是手动添加空白字符,如@ergonaut在其回答中所示。