java - How can I remove a specific object from an arrayList and how can I check if it contains this object? -


i have arraylist type shopping cart. when add new item shopping cart, check first if shopping cart has item. cart.contains(item) method didn't work, return false there same item in cart. second problem not able remove item object shopping cart arraylist. code shows below:

@controller @requestmapping("/addto.htm") public class addtocontroller{     @suppresswarnings("unchecked")     @requestmapping(method=requestmethod.get)     protected modelandview handlerequestinternal(httpservletrequest request, httpservletresponse response) throws exception {         httpsession session = request.getsession();         string action = request.getparameter("action");         system.out.println(action);         modelandview mv = new modelandview();         arraylist<item> cart;         if(session.getattribute("cart") != null) {             cart = (arraylist<item>) session.getattribute("cart");         }else {             cart = new arraylist<item>();         }         if(action.equals("addtocart")) {             long itemid = long.parselong(request.getparameter("itemid"));             itemdao itemdao = new itemdao();             item item = itemdao.get(itemid);             system.out.println("111"+cart.contains(item));             if (!cart.contains(item)) {                 cart.add(item);             }             double total = 0;             int count = 0;             (item : cart) {                 total = total + i.getprice();                 count += 1;             }             session.setattribute("cart", cart);             mv.addobject("total", total);             mv.addobject("count", count);             mv.setviewname("user/viewcart");         }         if(action.equals("remove")){             system.out.println("cart size is" + cart.size());             long itemid = long.parselong(request.getparameter("item"));             itemdao itemdao= new itemdao();             item item = itemdao.get(itemid);             system.out.println(cart.contains(item));             session.setattribute("cart", cart);             system.out.println(cart.size());         }         return mv;     } } 

can me solve problem? thanks!!

you need add .equals method item class arraylists know how compare 2 different objects together. while @ should add hashcode method well. useful sets have backup in case need it.

we can use .indexof(item) method position of object in list. if number returns if -1. it's not in list. if 0 or greater it's in there , can use index remove item.

public class item{   private string type;    public item(string type){     this.type = type;   }    public string gettype(){     return type;   }    @override   public int hashcode() {     final int prime = 31;     int result = 1;     result = prime * result + ((type == null) ? 0 : type.hashcode());     return result;   }    @override   public boolean equals(object obj) {     if (this == obj)       return true;     if (obj == null)       return false;     if (!(obj instanceof item))       return false;     item other = (item) obj;     if (type == null) {       if (other.type != null)         return false;     } else if (!type.equals(other.type))       return false;     return true;   } } 

now have .equals , hashcode. can compare them in arraylist.

arraylist<item> itemlist = new arraylist<item>();  // fill list itemlist.add(new item("banana")); itemlist.add(new item("toaster")); itemlist.add(new item("screw driver"));  item item = new item("hand grenade"); itemlist.add(item);  int index = itemlist.indexof(item); if( index != -1 ){   system.out.println("the item in index " + index);    // remove item , store in variable   item removeditem = itemlist.remove(index);   system.out.println("we removed " + removeditem.gettype() + " list."); } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -