如何从ArrayList中正确删除Object?

时间:2013-10-07 09:01:30

标签: java object arraylist

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

 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。我也有成本和信息的设置/获取,但选择不包括做空间

  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,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

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

3 个答案:

答案 0 :(得分:7)

覆盖equals课程中的Item方法。您可以使用itemNum检查equals方法中对象的相等性。

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

修改

你没有正确地覆盖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;
}

答案 1 :(得分:2)

使用ArrayList.remove()

确保在对象类中覆盖等于

答案 2 :(得分:0)

要从列表中删除对象,您需要先找到对象。您可以实现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
  }
}
}

当您获得itemNum表单输入时,创建一个CatalogItem对象并设置此值并迭代列表以检查相等性。如果您找到它,然后从列表中删除,但请确保您没有从同一列表中删除,否则您可以获得Concurrency Exception