亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用另一個類將對象添加到arraylist?

如何使用另一個類將對象添加到arraylist?

斯蒂芬大帝 2022-05-12 18:44:37
基本上我的教授告訴我使用arraylist?老實說,我不明白他的意思..我認為他希望我將對象添加到arraylist?現在,我真的不知道該怎么做..我的代碼正在運行并且非常好。但是,他還是想讓我利用arraylist讓它看起來更好?/* * 要更改此許可證頭,請在項目屬性中選擇許可證頭。* 要更改此模板文件,請選擇工具 | 模板 * 并在編輯器中打開模板。*/ 包 ahaprogram2;這是另一堂課     package ahaprogram2;     import java.util.ArrayList;     import java.util.Scanner;  public class Container { Scanner reader  = new Scanner(System.in); public ArrayList<String> CanContainer = new ArrayList<String>(); public int Contsizep; public String contName; public String changeName; public Container(String contname){     this.contName = contname; } public void AddCan(String CantoAdd) {     this.CanContainer.add(CantoAdd); } public void RemoveCan(String CantoRemove) {      if (this.CanContainer.contains(CantoRemove)) {        this.CanContainer.remove(CantoRemove);            System.out.println("** " + CantoRemove + " Can removed      successfully**");     }      else {          System.out.println("Can cannot be found.. make sure to put the       exact name!!");      }        }    public void renameCont(String changename) {    this.contName += changename;    }     public void printContents() {     System.out.println("Here are the contents of " + contName);     System.out.println("");     for(String counter : this.CanContainer){        System.out.println(counter); }       }      public void printContainer() {  // for OPTION A ONLY     System.out.println("CONTAINER NAME SUCCESSFUL: ** " + contName +    "**");   }   }我只是想把所有東西都放在一個數組列表中,請幫忙..我的教授沒有面對面地教我們,這就是為什么我真的盡我所能在 youtube 上觀看視頻并在這里問..
查看完整描述

2 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

您的許多代碼似乎都基于Container以相似(如果不相同)的方式交互五個對象之一。首先,您可以使用 ArrayList 來存儲 Container 對象的列表,而不是手動聲明每個容器:


public static ArrayList<Container> containerList = new ArrayList<Container>();

然后,您可以使用新容器填充此列表ArrayList.add(E e),并結合 for 循環或其他一些構造:


for (int i = 1; i <= 5; i++) {

  Container container = new Container("Container " + i + ": ");

  containerList.add(container);

}

同樣,您可以使用ArrayList.get(int index)(如果您知道索引)或ArrayList.indexOf(Object o)(如果您有對特定容器的引用)訪問任何特定容器。這可以替換或補充您的條件語句。例如,您的(contInput.equals("X"))語句列表可以替換為:


int index = Integer.parseInt(contInput);

System.out.print("Input the name of Container " + index + ": ");

Container container = containerList.get(index - 1); // arrays start at 0, but your numbering starts at 1


String contImp = reader.nextLine();

container.renameCont(contImp);

container.printContainer();

希望這可以幫助。


查看完整回答
反對 回復 2022-05-12
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您可以像這樣將容器添加到 ArrayList:


ArrayList<Container> containers = new ArrayList<>();

containers.add(new Container("Container 1: "));

containers.add(new Container("Container 2: "));

containers.add(new Container("Container 3: "));

然后像這樣得到它們:


Container firstContainer = containers.get(0);

Container secondContainer = containers.get(1);


查看完整回答
反對 回復 2022-05-12
  • 2 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號