【问题标题】:Remove/change specific object from an ArrayList从 ArrayList 中删除/更改特定对象
【发布时间】:2021-08-02 17:54:12
【问题描述】:

我正在用 Java 为“硬件商店”构建一个简化的库存程序。我想按名称和数量将商品注册到一个数组中,如果商品售出,我还想删除或更改数量。

我相信我已经完成了 80% 的工作。我只是不知道如何在存储后删除或更改对象。这与equals(Object obj) 方法有关吗?我发现很难理解如何在我的案例中使用该方法。

硬件项目的类别:

public class Items {

    private String item;
    private String quantity;

    public Items(){

    }

    public String getName() {
        return item;
    }

    public String getType() {
        return item;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setItem(String item) {
        this.item=item;
    }


    public void setQuantity(String quantity) {
        this.quantity=quantity;
    }
}

--

public class Hardware {
    public static void main(String[] args) {

        List<Items> allItems =new ArrayList<Items>();

        Scanner input = new Scanner(System.in);

        int choice;
        while(true){

            System.out.println("Inventory\n");

            System.out.print("1.) Add new items to your inventory. \n");
            System.out.print("2.) Delete items from your inventory.\n");
            System.out.print("3.) Change quantity of an item.\n");

            System.out.print("6.) Exit\n");
            System.out.print("\nEnter Your Menu Choice: ");

            choice = input.nextInt();
            Scanner in = new Scanner(System.in);

            switch(choice){

                case 1:
                    Items myItems = new Items();
                    System.out.println("Item:");
                    myItems.setItem(in.nextLine());
                    System.out.println("Quantity?");
                    myItems.setQuantity(in.nextLine());

                    allItems.add(myItems);
                    System.out.println(allItems);

                    System.out.println("The inventory contains the following items:");
                    for(int i = 0; i < allItems.size(); i++) {
                        System.out.println(allItems.get(i).getName());

                    }

                    break;
                case 6:
                    System.out.println("Exiting Program...");
                    System.exit(0);
                    break;
                default :
                    System.out.println("This is not a valid Menu Option! Please Select Another");
                    break;

             }

        }


     }

}

【问题讨论】:

  • 你甚至不需要equals,但它可以让你的代码更简单。只需遍历您的项目并尝试找到具有用户输入名称的项目。
  • 您必须将 allItems 传输到某个方法,该方法会从中删除所需的项目。说,你必须声明一些方法deleteSpecificItem(List&lt;Items&gt; allItems, string itemName) 然后声明新方法replaceSpecificItemInList(List&lt;Items&gt; allItems, string itemName, Items newItem)

标签: java oop arraylist methods


【解决方案1】:

您应该像这样在 Items 类中实现equals 方法:

public boolean equals(Object object){
   if(this == object){
      return true;
   }
   if(object instanceof Items){
      Items newItem = (Item) object;
      return item.equals(newItem.item) && quantity.equals(newItem.quantity);
   }
   return false;
}

然后对于删除和更改选项,使用用户输入创建一个 Item 对象,并循环遍历 List,将每个 Item 对象与使用用户输入创建的对象进行比较,直到找到所需的对象,然后执行必要的操作。

【讨论】:

  • 感谢您的回复。但是如果创建了一个新对象——那么我还必须填写我要删除的对象的数量?
  • for(int i = 0; i
  • @HelmiGranfelt 你刚开始学习 Java 吗?这是删除代码:static void deleteSpecificItem(List&lt;Items&gt; allItems, String itemName) { Iterator&lt;Items&gt; iter = allItems.iterator(); while (iter.hasNext()) { Items value = (Items)iter.next(); if(Objects.equals(value.getName(), new String(itemName))) { iter.remove(); } } } 请让我知道您是否是初学者。剩下的我来教你。
猜你喜欢
  • 1970-01-01
  • 2012-01-21
  • 2021-03-11
  • 2015-08-02
  • 1970-01-01
  • 2015-08-05
  • 2013-04-17
相关资源
最近更新 更多