how to check if an object already exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #16
    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

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #17
      can I see how you have defined the object variable?

      Did you set it to null , initially?

      Originally posted by oll3i
      i get nullpointerexce ption

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #18
        Originally posted by oll3i
        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
        That's the convenient part of it all: the List does it for you when you call a
        List.contains() of List.indexOf() method. Have a look at the contains() method:
        (this is from the 1.6 JDK)
        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;
            }
        See how it uses the equals() method when it needs to find an object.

        kind regards,

        Jos

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #19
          if 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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #20
            Originally posted by oll3i
            if 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
            Your Klient class equals() method just needs to compare your 'this' Klient
            with another Klient object:
            Code:
            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 ...;
               }
            Don't forget to implement the hashCode() method too. The two methods always
            come in pairs, if you implement one you have to implement the other one too.

            kind regards,

            Jos

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #21
              Code:
              public boolean equals(Object o) {
                  if (!(o instanceof Klient)) return false;
                  Klient k = (Klient) o;
                  return  nazwa ==k.nazwa && pieniadze == k.pieniadze;
                }
              i did it like this is it okey

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by oll3i
                Code:
                public boolean equals(Object o) {
                    if (!(o instanceof Klient)) return false;
                    Klient k = (Klient) o;
                    return  nazwa ==k.nazwa && pieniadze == k.pieniadze;
                  }
                i did it like this is it okey
                If 'nazwa' and 'pieniadze' can be compared for equality using the '==' operator,
                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,

                Jos

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #23
                  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

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #24
                    Originally posted by oll3i
                    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)
                    That's an entirely different problem. Forget about GUIs for now and just take
                    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,

                    Jos

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #25
                      i wdnt have to add to the list if the equals method worked :)

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #26
                        Originally posted by oll3i
                        i wdnt have to add to the list if the equals method worked :)
                        True, but my guess is that your equals() method doesn't work yet and the less
                        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:
                        Code:
                        if (!list.contains(klient)) list.add(klient);
                        ... or you could use a Set for this but still then: your equals() method should
                        work correctly.

                        kind regards,

                        Jos

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #27
                          the equals method works i compared two clients

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #28
                            Originally posted by oll3i
                            the equals method works i compared two clients
                            Good; did you override the hashCode() method too?

                            kind regards,

                            Jos

                            Comment

                            • oll3i
                              Contributor
                              • Mar 2007
                              • 679

                              #29
                              Code:
                              public int hashCode()
                              {
                              int result = 17;
                              result = 37 * result + pieniadze.hashCode();
                              result = 37 * result + nazwa.hashCode();
                              
                              return result;
                              }
                              pieniadze is a double and i have a problem
                              and i dont know if it a good definition

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #30
                                Originally posted by oll3i
                                Code:
                                public int hashCode()
                                {
                                int result = 17;
                                result = 37 * result + pieniadze.hashCode();
                                result = 37 * result + nazwa.hashCode();
                                
                                return result;
                                }
                                pieniadze is a double and i have a problem
                                and i dont know if it a good definition
                                primitives don't have methods so 'double.hashCod e()' doesn't make sense; but
                                why not simply cast the double value to an int and use that as the double's
                                hash value:
                                Code:
                                int result = 17;
                                result = 37 * result + (int)pieniadze;
                                result = 37 * result + nazwa.hashCode();
                                Is 'nazwa' an object? If so you have to use its 'equals()' method in your own
                                'equals()' method (see a couple of replies back).

                                kind regards,

                                Jos

                                Comment

                                Working...