Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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中arraylist中的arraylist_Java_Arrays_Arraylist_Collections - Fatal编程技术网

Java中arraylist中的arraylist

Java中arraylist中的arraylist,java,arrays,arraylist,collections,Java,Arrays,Arraylist,Collections,我必须创建一个包含商店对象的arraylist,其中包含名称、描述和另一个产品对象arraylist。addProduct方法留空,因为这是我面临的问题。我需要在使用Shops对象创建的shop arraylist中选择特定的店铺名称,然后在产品的arraylist中添加一个或多个产品。我不明白我怎么能做到这一点。如果你们能帮我 这是我目前掌握的代码: //class of shops but I removed the getters and setters to keep the code

我必须创建一个包含商店对象的arraylist,其中包含名称、描述和另一个产品对象arraylist。addProduct方法留空,因为这是我面临的问题。我需要在使用Shops对象创建的shop arraylist中选择特定的店铺名称,然后在产品的arraylist中添加一个或多个产品。我不明白我怎么能做到这一点。如果你们能帮我

这是我目前掌握的代码:

//class of shops but I removed the getters and setters to keep the code short
public class Shops {
    private  String name;
    private String desc;
    private ArrayList<Products> product;

    public Shops(String name, String desc, ArrayList<Products> product) {
        this.name = name;
        this.desc = desc;
        this.product = product;
    }

//another class called Shop assistant which adds products to a specific shop

 public class ShopAssistant {

   ArrayList<Shops> shop = new ArrayList<>();

public void addShops(Shops shop) {
    shop.add(shop);
}
public void addProduct(Products product ) {
    //add products to product arraylist which should be linked to shop arraylist
}
//商店类,但我删除了getter和setter以保持代码简短
公营商店{
私有字符串名称;
私有字符串描述;
私有ArrayList产品;
公共商店(字符串名称、字符串描述、ArrayList产品){
this.name=名称;
this.desc=desc;
本产品=产品;
}
//另一个叫做Shop assistant的类,它向特定的商店添加产品
公务舱店员{
ArrayList shop=新的ArrayList();
公共商店(商店){
添加(shop);
}
公共产品(产品){
//将产品添加到product arraylist,该列表应链接到shop arraylist
}

您必须知道在何处添加产品。您必须将其添加到ArrayList中的Shop对象,而不是ArrayList中。您可以通过ArrayList中的索引获取商店,因此您可能需要另一个int参数。

您首先需要将“addProduct”方法添加到“Shop”类中,以便将产品添加到商店的ArrayList中

然后,您可以在“ShopAssistant”类中创建一个方法,例如“addProductToShop”,该方法接受一个店铺和一个产品,并将该产品添加到该店铺中,例如,使用for循环查找该店铺,并使用“shop”类中的“addProduct”方法添加该产品

我还建议将“商店”改名为“商店”,将“产品”改名为“产品”,以便更清楚地了解它们的含义。

public class shop{
public class Shop {
    private String name; 
    private String description;
    private ArrayList<Products> products;

    public Shop(String name, String description, ArrayList<Products>products){
        this.name = name; 
        this.description = description;
        this.products = products;
    }

    //This is the main issue that you were missing. 
    //You need to have addProducts in both the shop and assistant. 
    public void addProducts(ArrayList<Products>newProducts){
        newProducts.forEach((product) -> {
            this.products.add(product);
        });
    }
}

public class ShopAssistant {
    ArrayList<Shop> shops = new ArrayList<>();
    public void addShop(Shop newShop){
        shops.add(newShop);
    }

    //Call AddProducts for each shop in the list of shops that need products updated.
    public void addProducts(ArrayList<Products>newProducts, ArrayList<Shop>shopsToAddTo){
        shopsToAddTo.forEach((shop) -> {
            shop.addProducts(newProducts);
        });
    }
}
私有字符串名称; 私有字符串描述; 私人ArrayList产品; 公共商店(字符串名称、字符串描述、ArrayListproducts){ this.name=名称; this.description=描述; 这一点。产品=产品; } //这是您遗漏的主要问题。 //你需要在商店和售货员那里都有新产品。 公共无效添加产品(ArrayListnewProducts){ 新产品。forEach((产品)->{ 此.products.add(产品); }); } } 公务舱店员{ ArrayList shops=新的ArrayList(); 公共商店(商店新闻商店){ 添加(新闻商店); } //为需要更新产品的店铺列表中的每个店铺呼叫AddProducts。 公共无效添加产品(ArrayListnewProducts、ArrayListshopsToAddTo){ shopsToAddTo.forEach((商店)->{ 商店。添加产品(新产品); }); } }
我认为这是一个糟糕的设计。商店应该提供一个API来维护它的产品清单。像这样的东西应该是有效的。虽然没有时间测试它,但是如果有一些bug,不要感到沮丧。你只需要生成需要添加产品的商店的列表。你可以通过设置一个ARR来完成。a列出主类中的店铺列表,然后循环查找并按名称或描述等查找店铺。问题是,我不确定如何将特定产品的产品列表添加到具有特定名称的店铺列表中。我必须从用户处获取店铺名称的字符串名称,然后将其与现有店铺名称进行比较,然后添加产品。这就是我被卡住的地方。因为我不知道如何将给定的名称与现有的店铺名称进行比较,并将特定的产品仅添加到arraylist的该店铺中。您是从控制台接收数据还是从文件中读取数据?无论哪种方式,一旦您拥有店铺名称列表,您都可以在arraylist中循环,检查名称是否匹配。否则,如果不要求您使用数组列表,您可以使用键值映射来存储店铺,然后按名称获取店铺并添加产品。您是否有读取数据的格式示例?