check if two subsets of integers are equal using equals method

Posted by MAS on Stack Overflow See other posts from Stack Overflow or by MAS
Published on 2012-11-22T00:40:36Z Indexed on 2012/11/22 5:01 UTC
Read the original article Hit count: 203

Filed under:

I have this java method in class called IntArray. The class has methods to add integers to a set or remove integers from a set,check size of a set, and check if 2 sets are equal. the 2 sets are created using 2 different objects of type IntArray in main, lets say object A and B. equals method supposed to check if two sets of integers are equal. for example set A = {1,2,3} and B = {1,2,3,4}. The method still return true even though one set is a subset of the other set. What exactly I am doing wrong? Thanks.

//part of the code in main
IntArray A = new IntArray();
IntArray B = new IntArray();
if(A.equals(B))
System.out.println("A and B are equal");



 //equals method in IntArray class
 public boolean equals(Object b)
 {
  if (b instanceof IntArray)
    {
      IntArray A = (IntArray) b;
      for (int i = 0; i < data.length; i++)
      if (countOccurrences(data[i]) != A.countOccurrences(data[i]))
      return false;
      return true;
    }
 else return false;  
}

© Stack Overflow or respective owner

Related posts about java