Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:打印格式,一行中有两个数组,长度不同_Java_Arrays_Arraylist_String Formatting_Out - Fatal编程技术网

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

Java:打印格式,一行中有两个数组,长度不同,java,arrays,arraylist,string-formatting,out,Java,Arrays,Arraylist,String Formatting,Out,我想用我给出的两个数组的内容在我的控制台上打印一个漂亮的表。两个数组都是ArrayList,可能具有不同的长度。此外,在两个ArrayList中都有长度为3的数组 我现在面临的问题是,我希望将每个ArrayList的两个值都放在一行中。例如: EAN STOCK CAPACITY | EAN STOCK CAPACITY 10 3 3 | 10 2 4 11 1 6 | 1

我想用我给出的两个数组的内容在我的控制台上打印一个漂亮的表。两个数组都是ArrayList,可能具有不同的长度。此外,在两个ArrayList中都有长度为3的数组

我现在面临的问题是,我希望将每个ArrayList的两个值都放在一行中。例如:

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  |   
在左侧可以看到一个包含数组的ArrayList,在右侧可以看到另一个包含数组的ArrayList

但是,我不知道如何在一行中打印两个数组的值时遍历这两个数组。尤其是当两个ArrayList的大小不相同时

这是我到目前为止的代码。。。我不知道如何去实现它

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);
    }
私人饮料(ArrayList储藏室、ArrayList销售室){
System.out.format(“%10s%10s%10s%40s%10s%10s%10s%10s”、“EAN”、“库存”、“容量”、“|”、“EAN”、“库存”、“容量”);System.out.println();
字符串行=”;
对于(int i=0;i
你们能给我一个提示或帮助我吗?
亲切的问候和感谢

首先找到要打印的行总数,然后循环多次。如果当前行超过列表的大小,则打印空格(左)或无空格(右)

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();
    }
}
输出

EAN库存容量| EAN库存容量
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库存容量| EAN库存容量
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

您还想要后一个列表中的表格标题吗?谢谢。这很有效。但是你为什么使用其他格式的代码?@Jan你是在问我为什么用
d
而不是
s
?因为
d
意味着并允许对格式进行更多的控制,例如1000个分隔符、前导零以及如何显示符号,所以它是
int
值的更好格式符号。
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}}));