簡體   English   中英

如何將多個ArrayLists彼此相鄰打印

[英]How can you print multiple ArrayLists next to each other

我有一個對象的arraylist我稱為Process,每個進程都有一個用於分配,max和need的整數arrayList,所以每個進程基本上都有3個arraylists。 我正在嘗試制作一個看起來像這樣的表

              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() + " ";

我建議你使用StringBuilder作為變量temptempAllo ,..而不是String

所以你可以這樣做,

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

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM