I have 3 classes.
Class A has an ArrayList of class B and class B has an ArrayList of class C.

Within Class A, I have a function called contain

public boolean contain(int t, String n)
   {
      for(int i = st.size() - 1; i >= 0; i--)
      {
         if(st.get(i).contain(t, n))
            return true;
      } //find a variable starting from the most recent entries
      return false; //not found
   } //contain()

within class B, I also have a function called contain

public boolean contain(int t, String n)
   {
      return symTable.contains(new Variable(t, n));
   } //contain()

Class C is Variable()

What is happening is that symTable.contains(new Variable(t, n)); would return false even though it finds a matching object. I walked through the debugger and can confirm that the object that I am trying to find is in the list. Here is a screen shot at the point where the two objects did a comparison which yield false for some reason.

http://img146.imageshack.us/f/68863099.png/

obj would be the one that is in my ArrayList while this would be the object that I created in class B's contain function.

What exactly am I doing wrong? Thanks

Have you tried using the equals() method with the two objects that are in the ArrayList to see what it returns?
Does the Variable class have its own equals method or does it use the Object class's method?
If you don't have your own method, you probably need one. Read the API doc about the requirements for the hashcode method also.

I do not have any equals() method in any class? The documentation did not mention anything about needing it.

Have you tried the Object class's equals method with the two object that you think are "equal" to see what it returns?

Stupid me, I needed to create the .equals method for my class and that fixed it.

Also look at the API doc for the equals method and read what it says about the hashcode method. When you override equals, you may have to override hashcode to be sure that hashcode returns a consistent value.

Nevermind, I lied. It didn't work =(. I thought it did since it ran through the program. It does not seem that my equals method has been overriden correctly

public boolean equals(Variable v)
   {
      if(this.getType() == v.getType() && this.toString().equals(v.toString()))
         return true;
      return false;

   }

I guess I'll go research on the hastCode() method once I get back from class.

Use the @Override statement to be sure you are actually overriding method the method.
The Object equals method takes an Object as parameter.

I read a really good article about this so I basically just turned his code into something that works for my class. He mentioned that the hashCode() only works for immutable class which is the case for my class as well. I am wondering why doesn't he just remove the conditional statement in the hashCode() method? Wouldn't this make it work for mutable class?

@Override
   public boolean equals(Object o)
   {
      if(this == o)
         return true; //same object
      if(!(o instanceof Variable))
         return false;

      Variable va = (Variable) o;
      return this.getType() == va.getType() && this.toString().equals(va.toString());

   } //equals(Object v)

   @Override
   public int hashCode()
   {
      final int multiplier = 23;
      if (hashCode == 0)
      {
         int code = 133;
         code = multiplier * code + type;
         code = multiplier * code + name.hashCode();
         hashCode = code;
      } //works because Variable never changes
      return hashCode;

   } //hasCode()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.