第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Java中將Element插入到Array的多個類中

Java中將Element插入到Array的多個類中

尚方寶劍之說 2023-12-13 14:51:54
我的包裹上有這些課程:1. Class Goods with attributes (name, price, id) //Goods id is unique, but the name can be the same2. Class Storage with attributes (Goods gArray[3])3. Class Store with attributes (name, ArrayList<Storage>) //Store name is unique4. Class StoreSystem with attributes (ArrayList<Store>)我想將商品插入屬于某個商店的存儲中。我已經(jīng)成功地將Store插入到ArrayList中,但還沒有找到插入Goods的方法。添加商店的代碼如下:public String addStore(String storeName) {    String output = "";    if(storeCheck(storeName)) { //storeCheck used to check whether the store name exist/not.        output = "store already exist!";    }    else {        Store s1 = new Store();        Storage st1 = new Storage();        s1.setStoreName(storeName);        s1.setStorageList(null);        st1.setGArray(null);        listOfStore.add(s1);        listOfStorage.add(st1);        output = "Store added";    }    return output;}
查看完整描述

3 回答

?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

我相信就是這樣,使用列表作為中間:


    public void addGoods(Goods g) {


        List<Goods> storageList = Arrays.asList(this.getGoods());

        storageList.add(g);

        this.setGoods(storageList.toArray());

    }

像往常一樣獲取和設(shè)置,并且您需要控制大小。


查看完整回答
反對 回復 2023-12-13
?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

您可以在 Storage 類中創(chuàng)建一個方法:

  • 要么采用數(shù)組的索引和商品作為參數(shù),并將商品放置在數(shù)組中的該索引處(如果您有興趣完全控制一切的去向)

  • 或者只是將商品作為參數(shù)并決定將它們放在內(nèi)部的位置(如果您不關(guān)心商品轉(zhuǎn)到數(shù)組中的哪個索引)

那有意義嗎?


查看完整回答
反對 回復 2023-12-13
?
12345678_0001

TA貢獻1802條經(jīng)驗 獲得超5個贊

與現(xiàn)場進行POJO通話。然后在類中添加一個方法(它應(yīng)該像使用一樣簡單,并向其傳遞 3 個 Goods 類型的參數(shù))。就這樣吧。你有一個and 。 StorageGoods gArray[3]StorageStorestoreArrayList.add(new Storage());StorageGoodsStorage


Storage現(xiàn)在,在 a 中正確查找特定項Store而不為特定項分配索引Storage會有點麻煩。理想情況下,每個Storage對象都應(yīng)該有一個id字段。


代碼看起來像這樣:

方法Store add:


<return-type> addToStore(Goods[] goods) {

 storageArray.add(new Storage(goods));

}  

然后Storage類將如下所示:


class Storage {

 private final Goods gArray[3];


 public Storage(final Goods gArray) {

  this.gArray = gArray;

 }


 // getters

}  


查看完整回答
反對 回復 2023-12-13
  • 3 回答
  • 0 關(guān)注
  • 210 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號