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;
}
It may be
EDIT:
If by equals set you mean each element in the subset are in the same order (A = {1,2,3} and B = {1,2,3}):
Then, you want to check if two subsets of integers are equal using equals method:
Make sure to only compare the two sets when both have the same length. Otherwise, return false.
If your definition of equals set means two sets with the same elements, without mattering their position:
You should check if you countOccurrences is doing something like this:
In this latter case you should keep
if (countOccurrences(data[i]) != A.countOccurrences(data[i])).