Java compare this class to that class for equality -


my assignment override method equals(). have few concerns using stack12<e> = (stack12<e>)o; , o instanceof stack12. wonder bad practice, how using that in for-loop feels little not right me.

is there way compare class other object? or compare method robust enough?

  public boolean equals(java.lang.object o){   if(o == this) return true;   if(o == null || !(o instanceof stack12)){      return false;   }    stack12<e> = (stack12<e>)o;   if(this.size != that.size || this.capacity != that.capacity ){      return false;   }   for(int = 0; < this.size; i++){      if( that.stack[i] != this.stack[i] ){         return false;      }   }   return true;  } 

one caveat i'd add whenever override equals(...), want override hashcode(). agree seeing instanceof overused makes me worry code smell, think have no choice use instanceof in situation. casting generic type, may wrong, @ run-time, generics don't exist, may moot.

one potentially major issue see though you're using == in loop. if stack array uses objects, should use equals(...) inside loop. fact class generic suggests stack array hold objects, i'm not sure since don't see this.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -