如何从 ArrayList 中正确删除对象? [英] How can I correctly remove an Object from ArrayList?

查看:50
本文介绍了如何从 ArrayList 中正确删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 ArrayList 中删除一个对象.每个 Item 对象有 3 个属性;1. itemNum 2. 信息 3. 成本.我也有 3 个类,1. Item 类定义了存储在目录中的单个项目.2. Catalog 类维护 Item 对象的列表.3 客户端类 w/main 方法.我在 Item 类中有集合和获取,在目录中有 ArrayList.在客户端,我有一个提示输入要删除的 itemNum.如何根据对 itemNum 的搜索从 ArrayList 中正确删除 Item 对象?以下是我的代码以及我目前尝试过的代码.

I’m trying to remove an object from an ArrayList. Each Item object has 3 attributes; 1. itemNum 2. info 3. cost. I also have 3 classes, 1. Item class defines the single items stored in the catalog. 2. Catalog class maintains the list of the Item objects. 3 Client class w/main method. I have the sets and gets in Item class and I have the ArrayList in Catalog. In Client, I have a prompt to "Enter in the itemNum to remove. How do I correctly remove an Item object from the ArrayList based on a search for an itemNum? Below is my code and what I’ve tried so far.

 Public class Item 
 {
 private int itemNum;
  private String info;
  private double cost;
  private int itemNum;


   public Item()
  {   //start constructor
     itemNum = 0;   //default values
     info = "x";
     cost = 0;
  }   //end constructor


 public CatalogItem(int newItemNum, String newInfo, double newCost)
  {   //start overload constructor
     this.itemNum = newItemNum;
     this.info = newInfo;
     this.cost = newCost;
  }   //end overload constructor

//下面是itemNum的set/gets.我也有设置/获取成本和信息,但选择不包括空间

//below are the set/gets for itemNum. I also have sets/gets for cost and info, but choose not to include do to space

  public int getItemNum()
  {   //start itemNum accessor
     return itemNum;

  }   //end getItemNum

  public void setItemNum(int newItemNum)
  {   //start itemNum mutator
     this.itemNum = newItemNum;
  }   //end setItemNum
  }   //end Item class

  public boolean equals(CatalogItem obj)
  {   //start equals
     if (itemId == obj.itemId)
        return true;
     else
        return false;
  }   //end equals

//下面是我的目录类

 import java.util.*;

  public class Catalog
 {   //start class
  private ArrayList<CatalogItem> listOfObjects = new ArrayList<CatalogItem>(100);   //creates ArrayList
  Item newItem = new Item(newItemNum, newInfo, newCost);   

  public void remove(int id)
  {   //start remove
     int item = id;

     for (int index = 0; index < listOfObjects.size();        index++)
        if (newItem.getItemId() == item)   //if item in the inventory matches the item number passed  
        listOfObjects.remove(index);  //removes based on index, I’ve also tried listOfObjects.remove(item);

   /*   I’ve also tried an enhanced for loop
     for (CatalogItem obj : listOfObjects)
        if (newItem.getItemId() == item)
           listOfObjects.remove(newItem);         */


  }   //end remove

}

//下面是主要的.它接收用户关于 itemNum、信息和成本的输入

//below is main. It receives input from the user regarding the itemNum, info, and cost

 import java.util.*;   //allows use of Scanner class

   public class Client
  {   //start client class

  public static void main(String[] args)
  {   //start main
     Catalog serv = new Catalog();   //creates instance of Catalog class
     Scanner scan = new Scanner(System.in);   //creates instance of Scanner class called scan
              System.out.print("\nEnter in the Item ID you want to remove: ");  
              id = scan.nextInt();
              serv.remove(id);   //sends id to Catalog Class to be removed
 }   //end main
 }   //end class

它编译得很好,但不会根据找到的索引删除.任何帮助都会很棒.

It compiles fine, but doesn't remove based on the found index. Any help would be great.

推荐答案

覆盖 Item 类中的 equals 方法.您可以使用 itemNum 在您的 equals 方法中检查对象的相等性.

Override equals method in your Item class. You can use itemNum to check the equality of objects in your equals method.

然后使用ArrayList remove(Object o) 方法删除对象.remove 方法在内部使用 equals 来查找要删除的对象.

Then use ArrayList remove(Object o) method to delete the object. The remove method uses equals internally to find the object to be removed.

您没有正确覆盖 equals 方法,这是正确的签名和实现:

You are not overriding the equals method properly, here is the right signature and implementation:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Item other = (Item) obj;
    if (itemNum != other.itemNum)
        return false;
    return true;
}

这篇关于如何从 ArrayList 中正确删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆