简体   繁体   中英

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. Furthermore, inside both ArrayLists are arrays which have a length of 3.

The problem I am facing right now is that I want to have both values of each ArrayLists in one line. 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.

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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