简体   繁体   English

Java:打印格式,一行中有两个数组,长度不同

[英]Java: Print format with two arrays in one line and different length

I want to print a beautiful table to my console with the content of two arrays I have given. 我想用我给出的两个数组的内容将漂亮的表打印到控制台。 Both arrays are ArrayLists and may have a different length. 这两个数组都是ArrayList,并且长度可能不同。 Furthermore, inside both ArrayLists are arrays which have a length of 3. 此外,在两个ArrayList的内部都是长度为3的数组。

The problem I am facing right now is that I want to have both values of each ArrayLists in one line. 我现在面临的问题是我想将每个ArrayLists的两个值都放在一行中。 Eg: 例如:

EAN   STOCK   CAPACITY  |  EAN   STOCK   CAPACITY
 10       3          3  |   10       2         4
 11       1          6  |   11       4         5
 12       5          7  |   12       6         9
 13       7          9  |   13       3         5
 14       4          7  |   14       0         4
 15       0          7  |   
 16       0          2  |   

On the left side you can see one ArrayList with the arrays contained and on the right side the other ArrayList with the arrays contained. 在左侧,您可以看到一个包含数组的ArrayList,在右侧是另一个包含数组的ArrayList。

However, I don't know how to iterate through both arrays while printing the values of both in one line. 但是,我不知道如何在一行中打印两个数组的值时遍历两个数组。 Especially when the size of both ArrayLists are not the same.. 尤其是当两个ArrayList的大小不相同时。

This is my code so far... I don't know how to realize it 到目前为止,这是我的代码...我不知道如何实现

private void printDrinks(ArrayList<int[]> storageRoom, ArrayList<int[]> saleRoom){
        System.out.format("%10s %10s %10s %40s %10s %10s %10s", "EAN", "STOCK", "CAPACITY", "|", "EAN", "STOCK", "CAPACITY"); System.out.println();
        String row = "";

        for (int i = 0; i < storageRoom.size(); i++){
            row += String.format("%10s %10s %10s %10s", storageRoom.get(i)[0], storageRoom.get(i)[1], storageRoom.get(i)[2]);
        }

        for (int i = 0; i < saleRoom.size(); i++){
            row += String.format("%10s %10s %10s %10s", saleRoom.get(i)[0], saleRoom.get(i)[1], saleRoom.get(i)[2]);
        }

        System.out.println(row);
    }

Can you guys give me a hint or help me? 你们能给我提示还是帮我吗? Kind regards and Thank You! 亲切的问候,谢谢!

First find the total number of rows to print, then loop that many times. 首先找到要打印的总行数,然后循环多次。 Print spaces (left) or nothing (right) if the current row exceeds size of list. 如果当前行超出列表大小,则打印空格(左)或不打印任何内容(右)。

private static void printDrinks(List<int[]> storageRoom, List<int[]> saleRoom){
    int rows = Math.max(storageRoom.size(), saleRoom.size());
    System.out.println("EAN   STOCK   CAPACITY  |  EAN   STOCK   CAPACITY");
    for (int i = 0; i < rows; i++) {
        if (i < storageRoom.size())
            System.out.printf("%3d %7d %10d  |", storageRoom.get(i)[0], storageRoom.get(i)[1], storageRoom.get(i)[2]);
        else
            System.out.printf("%25s", "|");
        if (i < saleRoom.size())
            System.out.printf("  %3d %7d %10d", saleRoom.get(i)[0], saleRoom.get(i)[1], saleRoom.get(i)[2]);
        System.out.println();
    }
}

Tests 测验

printDrinks(Arrays.asList(new int[][] {{10,3,3},{11,1,6},{12,5,7},{13,7,9},{14,4,7},{15,0,7},{16,0,2}}),
            Arrays.asList(new int[][] {{10,2,4},{11,4,5},{12,6,9},{13,3,5},{14,0,4}}));
printDrinks(Arrays.asList(new int[][] {{10,2,4},{11,4,5},{12,6,9},{13,3,5},{14,0,4}}),
            Arrays.asList(new int[][] {{10,3,3},{11,1,6},{12,5,7},{13,7,9},{14,4,7},{15,0,7},{16,0,2}}));

Outputs 产出

EAN   STOCK   CAPACITY  |  EAN   STOCK   CAPACITY
 10       3          3  |   10       2          4
 11       1          6  |   11       4          5
 12       5          7  |   12       6          9
 13       7          9  |   13       3          5
 14       4          7  |   14       0          4
 15       0          7  |
 16       0          2  |
EAN   STOCK   CAPACITY  |  EAN   STOCK   CAPACITY
 10       2          4  |   10       3          3
 11       4          5  |   11       1          6
 12       6          9  |   12       5          7
 13       3          5  |   13       7          9
 14       0          4  |   14       4          7
                        |   15       0          7
                        |   16       0          2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM