Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I correctly remove an Object from ArrayList?

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.

like image 891
Dan S. Avatar asked Jan 13 '23 04:01

Dan S.


2 Answers

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;
}
like image 120
Juned Ahsan Avatar answered Jan 21 '23 07:01

Juned Ahsan


Use ArrayList.remove()

Make sure you have overridden equals in your object class.

like image 40
Jiji Avatar answered Jan 21 '23 06:01

Jiji