简体   繁体   English

如何从ArrayList中正确删除Object?

[英]How can I correctly remove an Object from ArrayList?

I'm trying to remove an object from an ArrayList. 我正在尝试从ArrayList中删除一个对象。 Each Item object has 3 attributes; 每个Item对象都有3个属性; 1. itemNum 2. info 3. cost. 1. itemNum 2. info 3.成本。 I also have 3 classes, 1. Item class defines the single items stored in the catalog. 我还有3个类,1。Item类定义存储在目录中的单个项。 2. Catalog class maintains the list of the Item objects. 2. Catalog类维护Item对象的列表。 3 Client class w/main method. 3客户端类w / main方法。 I have the sets and gets in Item class and I have the ArrayList in Catalog. 我有集合并获取Item类,我在Catalog中有ArrayList。 In Client, I have a prompt to “Enter in the itemNum to remove. 在客户端中,我提示“输入要删除的itemNum。 How do I correctly remove an Item object from the ArrayList based on a search for an itemNum? 如何根据搜索itemNum从ArrayList中正确删除Item对象? 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

//below are the set/gets for itemNum. //下面是itemNum的set / gets。 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

//below is my Catalog Class //下面是我的目录类

 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

} }

//below is main. //下面是主要的。 It receives input from the user regarding the itemNum, info, and cost 它接收用户关于itemNum,info和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. 任何帮助都会很棒。

Override equals method in your Item class. 覆盖Item类中的equals方法。 You can use itemNum to check the equality of objects in your equals method. 您可以使用itemNum检查equals方法中对象的相等性。

Then use ArrayList remove(Object o) method to delete the object. 然后使用ArrayList remove(Object o)方法删除对象。 The remove method uses equals internally to find the object to be removed. remove方法在内部使用equals来查找要删除的对象。

EDIT: 编辑:

You are not overriding the equals method properly, here is the right signature and implementation: 你没有正确地覆盖equals方法,这是正确的签名和实现:

@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;
}

Use ArrayList.remove() 使用ArrayList.remove()

Make sure you have overridden equals in your object class. 确保在对象类中重写了等号

To remove the object from the list you need to find out the object first. 要从列表中删除对象,您需要先找到对象。 You can implement Comparable interface and override compareTo() method to find out the object. 您可以实现Comparable接口并覆盖compareTo()方法以找出对象。

Public class CatalogItem implements Comparable
{
 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

public int compareTo(catalogItem c){
  if(c.getItemNum == this.getItemNum()){
     return 0;// zero means both are equal
  }
}
}

When you get teh itemNum form input, the create a CatalogItem object and set this value and iterate through list to check equality. 当您获得itemNum表单输入时,创建一个CatalogItem对象并设置此值并遍历列表以检查相等性。 If you find it then remove from the list, but make sure you are not removing from the same list otherwise you can get Concurrency Exception . 如果您找到它然后从列表中删除,但请确保您没有从同一列表中删除,否则您可以获得Concurrency Exception

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM