【问题标题】:arraylist within arraylist in JavaJava中arraylist中的arraylist
【发布时间】:2018-10-09 14:46:00
【问题描述】:

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

这是我目前的代码:

//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
}

【问题讨论】:

  • 我认为这是一个糟糕的设计。 Shop 应该提供一个 API 来维护其产品列表。

标签: java arrays arraylist collections


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您首先需要将 'addProduct' 方法放在 'Shop' 类中,以便将产品添加到商店的数组列表中。

    然后,您可以在“ShopAssistant”类中创建诸如“addProductToShop”之类的方法,该方法获取商店和产品,并将产品添加到商店,例如使用 for 循环查找商店,并使用 'Shop' 类中的 'addProduct' 方法添加产品。

    我还建议将“shop”重命名为“shops”,将“product”重命名为“products”,以便更清楚地了解它们的含义。

    【讨论】:

      【解决方案3】:
      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 来做到这一点,然后遍历并按名称或描述等查找商店。
      • 问题是我不确定如何将特定产品的产品数组列表添加到具有特定名称的商店数组列表中。我必须从用户那里获取一个字符串名称作为商店名称,然后将其与现有商店名称进行比较,然后添加产品。这就是我卡住的地方。因为我不知道如何将给定名称与现有商店名称进行比较,并将特定产品仅添加到 arraylist 的该商店。
      • 您是从控制台获取数据还是从文件中读取数据?无论哪种方式,一旦您拥有商店名称列表,您就可以遍历您的 ArrayList 商店并检查名称是否匹配。否则,如果您不需要使用数组列表,您可以使用键值映射来存储您的商店,然后只需按名称获取商店并添加产品。
      • 您有读取数据格式的示例吗?
      猜你喜欢
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2014-09-28
      • 1970-01-01
      相关资源
      最近更新 更多