java - How can I correctly remove an Object from ArrayList? -
i’m trying remove object arraylist. each item object has 3 attributes; 1. itemnum 2. info 3. cost. have 3 classes, 1. item class defines single items stored in catalog. 2. catalog class maintains list of item objects. 3 client class w/main method. have sets , gets in item class , have arraylist in catalog. in client, have prompt “enter in itemnum remove. how correctly remove item object arraylist based on search itemnum? below code , i’ve tried 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 set/gets itemnum. have sets/gets cost , info, choose not include 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 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; (int index = 0; index < listofobjects.size(); index++) if (newitem.getitemid() == item) //if item in inventory matches item number passed listofobjects.remove(index); //removes based on index, i’ve tried listofobjects.remove(item); /* i’ve tried enhanced loop (catalogitem obj : listofobjects) if (newitem.getitemid() == item) listofobjects.remove(newitem); */ } //end remove
}
//below main. receives input user regarding 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 item id want remove: "); id = scan.nextint(); serv.remove(id); //sends id catalog class removed } //end main } //end class
it compiles fine, doesn't remove based on found index. great.
override equals
method in item
class. can use itemnum
check equality of objects in equals method.
then use arraylist remove(object o)
method delete object. remove
method uses equals
internally find object removed.
edit:
you not overriding equals method properly, here right signature , 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; }
Comments
Post a Comment