[英]arraylist within arraylist in Java
I have to create an arraylist of Shops objects which have a name, description and another arraylist of products objects. 我必须创建一个Shops对象的数组列表,该对象具有名称,描述和另一个product对象的arraylist。 The addProduct method is left blank because this is where I face a problem.
addProduct方法留空,因为这是我面临的问题。 I need to select a specific shop name in the shop arraylist created with Shops objects and then add one or more products in the arraylist of products.
我需要在使用Shops对象创建的商店阵列列表中选择一个特定的商店名称,然后在产品阵列列表中添加一个或多个产品。 I don't understand how I can manage to do that.
我不知道如何才能做到这一点。 If you guys could help me.
如果你们可以帮助我。
This is the code I have so far: 这是我到目前为止的代码:
//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
}
You have to know where to add the product. 您必须知道在哪里添加产品。 You have to add it to a Shop object inside the ArrayList, not to the ArrayList.
您必须将其添加到ArrayList内的Shop对象中,而不是添加到ArrayList中。 You can get a shop by its index in the ArrayList, so you probably need another int parameter.
您可以通过ArrayList中的索引来获得一家商店,因此您可能需要另一个int参数。
You first need the 'addProduct' method to be in the 'Shop' class, to add a product to the shop's arraylist. 首先,您需要将'addProduct'方法添加到'Shop'类中,以将产品添加到商店的数组列表中。
Then, you can create a method such as 'addProductToShop' in the 'ShopAssistant' class that takes a shop and a product, and adds the product to the shop eg use a for-loop to find the shop, and add the product using the 'addProduct' method from the 'Shop' class. 然后,您可以在“ ShopAssistant”类中创建诸如“ addProductToShop”之类的方法,该方法接收一家商店和一种产品,并将该产品添加到该商店,例如,使用for循环查找该商店,然后使用“商店”类中的“ addProduct”方法。
I would also advise renaming 'shop' to 'shops' and 'product' to 'products' to make it more clear what they hold. 我还建议将“商店”重命名为“商店”,将“产品”重命名为“产品”,以更清楚地说明其所拥有的内容。
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);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.