Java 向对象的arrayList添加信息

Java 向对象的arrayList添加信息,java,arraylist,Java,Arraylist,基本上我有三个类:Store类、Stock类和GUI类。创建存储时,我希望它具有自己的ArrayList,以便可以向其中添加多个stock对象。(通过GUI完成) 我尝试只包含所需的基本代码(删除了getter方法、setter方法、默认构造函数compareTo等) 下面是这些类的一些代码: 贮藏 GUI:(只是与我的问题相关的代码) 它的GUI代码真的把我搞砸了,基本上我想把所选的股票添加到所选的商店中。完成此操作后,您将如何访问特定存储的arrayList中的信息 非常感谢。我会这样做,我

基本上我有三个类:Store类、Stock类和GUI类。创建存储时,我希望它具有自己的ArrayList,以便可以向其中添加多个stock对象。(通过GUI完成)

我尝试只包含所需的基本代码(删除了getter方法、setter方法、默认构造函数compareTo等)

下面是这些类的一些代码:

贮藏

GUI:(只是与我的问题相关的代码)

它的GUI代码真的把我搞砸了,基本上我想把所选的股票添加到所选的商店中。完成此操作后,您将如何访问特定存储的arrayList中的信息


非常感谢。

我会这样做,我会将每个门店的参考添加到自己的列表中

ArrayList<Store> storeList = new ArrayList<Store>();
//This next line will add it at the location of the store ID so you can reference it by that unique number later on
storeList.add(z.getStoreID, z);

总体而言,我还是个新手,所以这可能不是最好的解决方案,但我会这样做。

Duplicate of被要求创建一个新的解决方案,因为在该解决方案中,我对GUI没有太具体。@C101der这就是为什么StackOverflow都是关于编辑的。不,不是。这是一个完全不同的问题,而前面的问题已经得到了回答。对于具有类似问题的人来说,对一个确切案例进行多次编辑将不会有未来的价值。@C101der:在上一个示例中,有一行
Stock s=stocks.get(indexStock)。变量
stocks
来自哪里?我问这个问题是因为
Store
stocks
,虽然你已经有
stocks
,你想把其中一个设置到
Store
。实际上这是一个很好的方法。虽然您不应该使用
stores=newstore()设置
stores
。请立即执行
stores=storeList…
。谢谢。还是有点困惑,但我会试一试。
public class Stock {
    private int id;
    private String name;
    private double price;
    private int units; 



    public Stock(int idIn, String nameIn, double priceIn, int unitsIn) {
        id = idIn;
        name = nameIn;
        price = priceIn;
        units = unitsIn;
    }

}
    int indexStore = lst_Store.getSelectedIndex(); //store that the user selects from list
    int indexStock = lst_Stock.getSelectedIndex(); //stock item selected by user

    //get the specific stock details
    Stock s = stocks.get(indexStock);

    Store z = new Store(); //so i can call method below

    z.addStockItem(s);
ArrayList<Store> storeList = new ArrayList<Store>();
//This next line will add it at the location of the store ID so you can reference it by that unique number later on
storeList.add(z.getStoreID, z);
Store s = new Store();
s = storeList.get(storeIDYoureInterestedIn)