java - 如何将多个 ArrayList 并排打印

标签 java arraylist

我有一个名为 Process 的对象的数组列表,每个进程都有一个用于分配、最大值和需要的整数数组列表,因此每个进程基本上都有 3 个数组列表。我正在尝试制作一个看起来像这样的表格

              Allocation       Max           Need
 Process 1    1 2 3 4          1 2 3 4     1 2 3 4 
 Process 2    5 7 8 9          5 7 8 9     5 7 8 9 
 Process 3    1 2 3 4          1 2 3 4     1 2 3 4 
 Process 4    5 7 8 9          5 7 8 9     5 7 8 9 

等 每个数字都是它自己的插槽,所以所有数组的大小都是 4。这是我正在尝试的代码

 public String toString() {
    String temp = "";
    String tempAllo = "";
    String tempMax = "";
    String tempNeed = "";

    for (int j = 0; j < allocation.size(); j++) {
        tempAllo = allocation.get(j).toString() + " ";
        tempMax = max.get(j).toString() + " ";
        tempNeed = need.get(j).toString() + " ";
    }

    temp = id + "\t" + tempAllo + "\t" + tempMax + "\t" + tempNeed + "\n";

    return temp;
}

但是打印出来了

                   Allocation       Max           Need
     Process 1        4             4              4 
     Process 2        9             9              9 
     Process 3        4             4              4
     Process 4        9             9              9 

所以它只打印出最后一个。提前感谢您的帮助

最佳答案

应该是:(注意+=)

tempAllo += allocation.get(j).toString() + " ";
tempMax += need.get(j).toString() + " ";
tempNeed += allocation.get(j).toString() + " ";

我建议您对变量 temptempAllo 等使用 StringBuilder,而不是 String.

这样你就可以做,

tempAllo.append(allocation.get(j).toString()).append(" ");

关于java - 如何将多个 ArrayList 并排打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898228/

相关文章:

java - 努力理解图形组中的等于

JavaFX TextField 使用等宽字体调整大小

java - List<string> 包含此的项目

java - Java 旅行商

java - 与 ArrayList 的合并排序

java - 如果数组中已存在数字,则不会跳过?

java - 在 java 中,有没有办法确保在 finally block 中调用多个方法?

java - 为什么我收到输入不匹配错误 (Java)

java - Java 中的多列

java - 我不想输入列表的大小,但我也想在列表中动态添加数字