if i define an equal method for klient (client in english) how shd i use it to compare if that client already exists cos i created a list of clients and i need to find that client in a list or shd i do in in some other way
how to check if an object already exists
Collapse
X
-
That's the convenient part of it all: the List does it for you when you call aOriginally posted by oll3iif i define an equal method for klient (client in english) how shd i use it to compare if that client already exists cos i created a list of clients and i need to find that client in a list or shd i do in in some other way
List.contains() of List.indexOf() method. Have a look at the contains() method:
(this is from the 1.6 JDK)
See how it uses the equals() method when it needs to find an object.Code:public boolean contains(Object o) { Iterator<E> e = iterator(); if (o==null) { while (e.hasNext()) if (e.next()==null) return true; } else { while (e.hasNext()) if (o.equals(e.next())) return true; } return false; }
kind regards,
JosComment
-
Your Klient class equals() method just needs to compare your 'this' KlientOriginally posted by oll3iif i define method equals for klient how i can compare if that klient(client in english) already exists i created a list of clients client doesnt inherits from any other class so i can not compare it to a class it inherits from
with another Klient object:Don't forget to implement the hashCode() method too. The two methods alwaysCode:public class Klient { . . public boolean equals(Object obj) { if (obj == null || !(obj instanceof Klient)) return false; Klient that= (Klient)obj; // compare 'this' Klient with 'that' Klient and return true or false; return ...; }
come in pairs, if you implement one you have to implement the other one too.
kind regards,
JosComment
-
If 'nazwa' and 'pieniadze' can be compared for equality using the '==' operator,Originally posted by oll3ii did it like this is it okeyCode:public boolean equals(Object o) { if (!(o instanceof Klient)) return false; Klient k = (Klient) o; return nazwa ==k.nazwa && pieniadze == k.pieniadze; }
I think all is fine. If one of them is an object (i.e. not a primitive) you have to
compare those members using the equals() method again (think about Strings).
Don't forget to implement the hashCode() method too.
kind regards,
JosComment
-
but i have a problem here cos when one button is clicked i create a client and add it to the list then when other button is clicked that client adds to the cart but the client created with the first button is not seen in the other portion of the code (the code for the second button)Comment
-
That's an entirely different problem. Forget about GUIs for now and just takeOriginally posted by oll3ibut i have a problem here cos when one button is clicked i create a client and add it to the list then when other button is clicked that client adds to the cart but the client created with the first button is not seen in the other portion of the code (the code for the second button)
care that your equals() and hashCode() methods are correctly implemented.
Build a little main() method that sticks Klients in a List and tries to find them
again afterwards. If all that works, build your GUI.
kind regards,
JosComment
-
True, but my guess is that your equals() method doesn't work yet and the lessOriginally posted by oll3ii wdnt have to add to the list if the equals method worked :)
circumstancial code around (such as buttons etc.) the less difficult it is to
debug your code. Note that a list can store duplicate objects (the equals()
method returns true) so you should test before adding:... or you could use a Set for this but still then: your equals() method shouldCode:if (!list.contains(klient)) list.add(klient);
work correctly.
kind regards,
JosComment
-
primitives don't have methods so 'double.hashCod e()' doesn't make sense; butOriginally posted by oll3ipieniadze is a double and i have a problemCode:public int hashCode() { int result = 17; result = 37 * result + pieniadze.hashCode(); result = 37 * result + nazwa.hashCode(); return result; }
and i dont know if it a good definition
why not simply cast the double value to an int and use that as the double's
hash value:Is 'nazwa' an object? If so you have to use its 'equals()' method in your ownCode:int result = 17; result = 37 * result + (int)pieniadze; result = 37 * result + nazwa.hashCode();
'equals()' method (see a couple of replies back).
kind regards,
JosComment
Comment