我有一个名为 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() + " ";
我建议您对变量 temp
、tempAllo
等使用 StringBuilder
,而不是 String
.
这样你就可以做,
tempAllo.append(allocation.get(j).toString()).append(" ");
关于java - 如何将多个 ArrayList 并排打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898228/