Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将元素插入到数组的多个类中_Java_Arrays_Oop_Arraylist - Fatal编程技术网

在Java中将元素插入到数组的多个类中

在Java中将元素插入到数组的多个类中,java,arrays,oop,arraylist,Java,Arrays,Oop,Arraylist,我的包裹上有这些课程: 1. Class Goods with attributes (name, price, id) //Goods id is unique, but the name can be the same 2. Class Storage with attributes (Goods gArray[3]) 3. Class Store with attributes (name, ArrayList<Storage>) //Store name is unique

我的包裹上有这些课程:

1. Class Goods with attributes (name, price, id) //Goods id is unique, but the name can be the same
2. Class Storage with attributes (Goods gArray[3])
3. Class Store with attributes (name, ArrayList<Storage>) //Store name is unique
4. Class StoreSystem with attributes (ArrayList<Store>)

我相信就是这样,用列表作为中介:

    public void addGoods(Goods g) {

        List<Goods> storageList = Arrays.asList(this.getGoods());
        storageList.add(g);
        this.setGoods(storageList.toArray());
    }
公共货物(货物g){
List-storageList=Arrays.asList(this.getGoods());
存储列表。添加(g);
this.setGoods(storageList.toArray());
}

像往常一样获取和设置,您将需要控制大小。

我相信就是这样,使用列表作为中间:

    public void addGoods(Goods g) {

        List<Goods> storageList = Arrays.asList(this.getGoods());
        storageList.add(g);
        this.setGoods(storageList.toArray());
    }
公共货物(货物g){
List-storageList=Arrays.asList(this.getGoods());
存储列表。添加(g);
this.setGoods(storageList.toArray());
}

像往常一样获取和设置,您将需要控制大小。

您可以在存储类中创建一个方法,该方法:

  • 或者将数组的索引商品作为参数,并将商品放置在数组中的该索引处(如果您希望完全控制所有内容的位置)
  • 或者将商品作为参数,并决定在内部放置它们的位置(如果您不关心商品在数组中指向哪个索引)

这有意义吗?

您可以在存储类中创建一个方法:

  • 或者将数组的索引商品作为参数,并将商品放置在数组中的该索引处(如果您希望完全控制所有内容的位置)
  • 或者将商品作为参数,并决定在内部放置它们的位置(如果您不关心商品在数组中指向哪个索引)

这有意义吗?

Goods gArray[3]
字段创建一个名为
Storage
POJO
。然后在
Store
类中添加
Storage
(应该像使用
storeArrayList.add(new Storage());
一样简单;并将商品类型的3个参数传递给它)。就这样。您在
存储
中有
存储
商品

现在,在
存储
中正确地查找特定的
存储
,而不为特定的
存储
指定索引将有点麻烦。理想情况下,每个
存储
对象都应该有一个
id
字段

代码将类似于:
存储
添加
方法:

<return-type> addToStore(Goods[] goods) {
 storageArray.add(new Storage(goods));
}  

使用
goodgarray[3]
字段创建一个名为
Storage
POJO
。然后在
Store
类中添加
Storage
(应该像使用
storeArrayList.add(new Storage());
一样简单;并将商品类型的3个参数传递给它)。就这样。您在
存储
中有
存储
商品

现在,在
存储
中正确地查找特定的
存储
,而不为特定的
存储
指定索引将有点麻烦。理想情况下,每个
存储
对象都应该有一个
id
字段

代码将类似于:
存储
添加
方法:

<return-type> addToStore(Goods[] goods) {
 storageArray.add(new Storage(goods));
}  

根据OP的描述,
存储
不是
商品的
列表
,而是一个固定大小的数组3。我觉得它也很奇怪。但这就是要求。事实上,这是我的校园作业,那个特殊的要求也让我感到困惑。抱歉。是的,固定为数组从OP的描述来看,
存储
不是
商品的
列表
,而是一个固定大小为3的数组。我觉得它也很奇怪。但这就是要求。事实上,这是我的校园作业,那个特殊的要求也让我感到困惑。抱歉。为True,固定为Array请澄清存储是否唯一,如果是,则说明它们是否唯一。此外,是否有任何字段可用于唯一标识
存储中的特定
存储
?类似于
storageId
之类的东西?请澄清存储是否唯一,如果是,则说明它们是唯一的还是非唯一的。此外,是否有任何字段可用于唯一标识
存储中的特定
存储
?类似于
storageId
之类的东西?这实际上是有意义的,我一直认为应该在StoreSystem类中放置addGoods()方法。但是,如果我将addGoods()方法放在Storage类中,那么包含这些商品的存储也会存储到Store类中吗?或者我应该在StoreSystem类中创建另一个方法来添加存储?我认为应该明确类的职责:在Storage类中保留商品管理,在Store类中保留存储管理。这实际上是有道理的,我一直认为应该在StoreSystem类中放置addGoods()方法。但是,如果我将addGoods()方法放在Storage类中,那么包含这些商品的存储也会存储到Store类中吗?或者我应该在StoreSystem类中创建另一个方法来添加存储?我认为您应该明确类的职责:在Storage类中保留商品管理,在Store类中保留存储管理。