簡體   English   中英

在列中打印Java數組

[英]Print Java arrays in columns

我正在嘗試用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

我正在使用以下代碼:

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");
    }

但是我得到的是格式不正確的結果:

Inventory Number      Books                          Prices
------------------------------------------------------------------
1                     Intro to Java                  $45.99 
2                     Intro to C++                  $89.34 
3                     Design Patterns                  $100.0 
4                     Perl                  $25.0 

如何將所有列直接排列在頂部標題下方?

有沒有更好的方法可以做到這一點?

您應該看一下格式:

System.out.format("%15.2f", booksPrices[i]);   

這將提供15個插槽,並在需要時在其中填充空格。

但是,我注意到您沒有對數字進行右對齊,在這種情況下,您需要在書本字段中左對齊:

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]);
}

導致:

Inventory Number    Books                         Prices
0                   This                          $45.99
1                   That                          $89.34
2                   The Other Longer One          $12.23
3                   Fourth One                    $1000.30

您可以使用

System.out.printf(...)

用於格式化輸出。 那用

String.format(...)

使用

java.util.Formatter

您可以在此處找到文檔。

要對齊簡單的字符串,可以使用以下命令:

String formatted = String.format("%20s", str)

這將前置

20 - str.length

在實際的String之前為空格,並將返回填充的String。 例如,如果您的字符串是“ Hello,World!” 它將添加11個空白:

"           Hello, World!"

要使某些內容 向右 對齊,您必須在表示結果字符串長度的數字前加一個“-”。

為了安全地對齊所有內容,您首先必須找出最長的字符串。

在不使用任何庫的情況下,您需要確定每個列的最大長度,並適當地附加所需的空格字符。 例如這樣:

/**
 * 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM