4

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

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

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

 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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dan S.
  • 87
  • 3
  • 5
  • 11

3 Answers3

7

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

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

EDIT:

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;
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • That's the best way to do it. But what about using the index? – UrsulRosu Oct 07 '13 at 09:09
  • My equals method isthis: public boolean equals(CatalogItem obj) { //start equals if ((itemNum == obj.itemNum) && (info== obj.info) && (cost == obj.cost)) return true; else return false; } //end equals – Dan S. Oct 07 '13 at 09:22
  • @user2460398 if it is logical to uniquely identify Items using only itemNum then remove other comparison from the conditions. If you cannot identify Items using jut the itemNum then it is not logical to delete the objects only using itemNum taken as input. – Juned Ahsan Oct 07 '13 at 09:35
  • Hmmm, that makes sense. I set the equals method() to match only the itemNum and it still didn't remove the object. – Dan S. Oct 07 '13 at 09:46
  • @user2460398 Your equals method signature and implementation is not correct. Always use override annotation to make sure that you are overriding the method. I have added the correct equals method to my answer in EDIT section. – Juned Ahsan Oct 07 '13 at 12:50
  • Thanks, you helped a great deal. That got it to work – Dan S. Oct 07 '13 at 20:03
2

Use ArrayList.remove()

Make sure you have overridden equals in your object class.

Jiji
  • 383
  • 1
  • 7
0

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.

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

Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42