【问题标题】:difficulty adding to non static arraylist难以添加到非静态数组列表
【发布时间】:2020-05-04 17:58:03
【问题描述】:

我正在尝试添加到数组列表的数组列表中。我有一个名为 prod 的数组列表和一个名为 shoppingBasket 的二维数组列表。

我遇到的主要问题是我希望它在购物篮中添加几件商品(即在购物篮列表中添加几个产品列表),但它没有这样做,而是将购物篮的第一项替换为下一项(所以有仅购物篮中的每一项)。 我对java相当陌生,不知道如何纠正这个问题。

我也尝试过创建另一个类“项目”,然后从这里创建对象,然后作为 ArrayList 添加到数组列表中 例如

public class Item {
    private int bar;
    private String name;
    private String type;
    private String brand;
    private String colour;
    private String con;
    private int quantity;
    private float cost;
    private String addi;

    public Item (int bar, String name, String type, String brand, String colour, String con, int quantity,float cost, String addi) {
        this.bar = bar;
        this.name = name;
        this.type = type;
        this.brand = brand;
        this.colour = colour;
        this.con = con;
        this.quantity = quantity;
        this.cost = cost;
        this.addi = addi;
    }

  public class Basket {
    //arraylist for the shopping basket
    private ArrayList<Item> shoppingBasket;

    public Basket() {
        shoppingBasket = new ArrayList<>();
    }

    //function adding items to the basket
    public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {

         Item items = new Item (bar, name, type, brand, colour, con, quantity, cost, addi);
         shoppingBasket.add(items);
    }
  }

当我尝试此方法并打印 arraylist 进行测试时,我只会显示 [Item@23455] 之类的内容,并且仍然会替换原来的项目而不是添加,而且我真的不明白如何正确执行此操作因此,如果有人可以向我解释该方法(如果它比我已经做过的更容易使用),那将不胜感激。尽管我也希望不必过多地更改代码。

有问题的代码: 一个类中的函数,用于将项目添加到购物篮。

public class Basket {
    //arraylist for the shopping basket
    private ArrayList<ArrayList<String>> shoppingBasket;

    public Basket() {
        shoppingBasket = new ArrayList<>();
    }

    //function adding items to the basket
    public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {

          ArrayList<String> prod = new ArrayList<>();

          //adding one item to the basket shop.add(bar); shop.add(name);
          prod.add(Integer.toString(bar)); 
          prod.add(name); 
          prod.add(type); 
          prod.add(brand); 
          prod.add(colour); 
          prod.add(con);
          prod.add(Integer.toString(quantity));
          prod.add(Float.toString(cost)); 
          prod.add(addi);

          shoppingBasket.add(prod); 

        System.out.println(shoppingBasket);     

    }

n.b.正在使用另一个表中的侦听器从表中添加项目(每个单元格都是不同的变量):

selectionModel.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                int row = tableViewAll.getSelectedRow();

                viewAllModel=(DefaultTableModel) tableViewAll.getModel();

                int bar=Integer.parseInt(viewAllModel.getValueAt(row,0).toString());
                String name = viewAllModel.getValueAt(row,1).toString();
                String type = viewAllModel.getValueAt(row,2).toString();
                String brand = viewAllModel.getValueAt(row,3).toString();
                String colour= viewAllModel.getValueAt(row,4).toString();
                String con = viewAllModel.getValueAt(row,5).toString();
                int quantity=1;
                float cost=Float.parseFloat(viewAllModel.getValueAt(row,7).toString());
                String addi= viewAllModel.getValueAt(row,8).toString();

                Basket item = new Basket();
                item.addToBasket(bar, name, type, brand, colour, con, quantity, cost, addi);    

提前致谢:)

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    您的设计/代码和解决方案存在问题:

    1. Item 不应该知道Basket 中有多少个数字。它的编号应在添加时传递到篮子。所以,从Item 中删除属性quantity
    2. 您需要Map 而不是List,因为当再次添加购物篮中的现有商品时,只会增加其数量。
    class Basket {
        private Map<String, Integer> basket = new HashMap<String, Integer>();
    
        public void addToBasket(Item item, int quantity) {
            if (item != null) {
                String key = item.getName();
                basket.put(key, basket.getOrDefault(key, 0) + quantity);
            }
        }
    
        public void showBasket() {
            for (Entry<String, Integer> entry : basket.entrySet()) {
                System.out.println("Item = " + entry.getKey() + ", Quantity = " + entry.getValue());
            }
        }
    }
    
    1. 每当调用valueChanged 时,Basket 都会被重置。将以下代码移到valueChanged 之外,并将其放在类中而不是方法中。
    Basket item = new Basket();
    

    应该这样写

    Basket basket = new Basket();
    selectionModel.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
    
            int row = tableViewAll.getSelectedRow();
    
            viewAllModel=(DefaultTableModel) tableViewAll.getModel();
    
            int bar=Integer.parseInt(viewAllModel.getValueAt(row,0).toString());
            String name = viewAllModel.getValueAt(row,1).toString();
            String type = viewAllModel.getValueAt(row,2).toString();
            String brand = viewAllModel.getValueAt(row,3).toString();
            String colour= viewAllModel.getValueAt(row,4).toString();
            String con = viewAllModel.getValueAt(row,5).toString();
            float cost=Float.parseFloat(viewAllModel.getValueAt(row,7).toString());
            String addi= viewAllModel.getValueAt(row,8).toString();
            basket.addToBasket(new Item(bar, name, type, brand, colour, con, cost, addi), 1);    
            //...
        });
    
    

    一旦您将 UI 扩展为具有数量的输入字段(文本字段/下拉菜单),您将从输入字段获取数量并将其传递给 addToBasket 而不是 1

    【讨论】:

    • 非常感谢,这非常详细,帮助很大:D
    【解决方案2】:

    您每次都在听众中创建一个新篮子:Basket item = new Basket(); 当您每次创建一个新实例并添加一个项目时,“新”篮子中只会有一个项目。

    您需要创建一个实例(可能在您的模型中)并使用该实例来添加项目。

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多