简体   繁体   English

使用equals方法检查两个整数子集是否相等

[英]check if two subsets of integers are equal using equals method

I have this java method in class called IntArray. 我在名为IntArray的类中有此java方法。 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. 该类具有以下方法:将整数添加到集合中或从集合中删除整数,检查集合的大小,并检查2个集合是否相等。 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. 这2组是使用main中2个不同的IntArray类型的对象创建的,可以说对象A和B。equals方法应该检查两组整数是否相等。 for example set A = {1,2,3} and B = {1,2,3,4}. 例如,设置A = {1,2,3}和B = {1,2,3,4}。 The method still return true even though one set is a subset of the other set. 即使一个集合是另一集合的子集,该方法仍然返回true。 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;  
}
 if (countOccurrences(data[i]) != A.countOccurrences(data[i]))

It may be 可能是

 if (countOccurrences(data[i]) != A.countOccurrences(A.data[i]))

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}): 如果相等,则表示子集中的每个元素的顺序相同(A = {1,2,3}和B = {1,2,3}):

Then, you want to check if two subsets of integers are equal using equals method: 然后,您要使用equals方法检查两个整数子集是否相等:

if (data[i].equals(A.data[i]));

Make sure to only compare the two sets when both have the same length. 确保仅比较两个长度相同的集合。 Otherwise, return false. 否则,返回false。

If your definition of equals set means two sets with the same elements, without mattering their position : 如果您对equals set的定义意味着两个具有相同元素的集合,而不必考虑它们的位置

You should check if you countOccurrences is doing something like this: 您应该检查countOccurrences是否正在执行以下操作:

public int countOccurrences(int element) 
{
     int count = 0;
     for(int i = this.data.length - 1; i >= 0; i--) 
        if(this.data[i] == element) 
          count ++;
    return count;
}

In this latter case you should keep if (countOccurrences(data[i]) != A.countOccurrences(data[i])) . 在后一种情况下,您应该保留if (countOccurrences(data[i]) != A.countOccurrences(data[i]))

Pre-check that the two lists are the same length. 预先检查两个列表的长度是否相同。 If they are not the same length, return false. 如果它们的长度不同,则返回false。 If they are the same length, do the element-by-element comparison. 如果它们的长度相同,则进行逐元素比较。

Here is a solution to the problem as stated. 这是上述问题的解决方案。 Note that it assumes that the IntArray object represents a true set, not a bag / multi-set. 请注意,它假定IntArray对象代表一个真实的集合,而不是bag / multi-set。 It does not assume that the values in the data arrays are ordered. 假定data数组中的值是有序的。

public boolean equals(Object otherObject) {
    if (otherObject == this) {  
        // This is an optimization for the case where an object
        // is compared with itself
        return true;
    } else if (otherObject instanceof IntArray) {
        IntArray other = (IntArray) other;
        if (this.data.length != other.data.length) {
            // If the sets have different nos of elements they cannot be equal
            return false;
        }
        for (int i = 0; i < this.data.length; i++) {
            boolean found = false;
            for (int j = 0; j < this.data.length; j++) {
                if (this.data[i] == other.data[j]) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

If the data arrays are guaranteed to be ordered, then you can do a simple elememt-by-element comparison. 如果保证 data数组是有序的,则可以进行简单的逐元素比较。 Replace the for loops in the code above with this: for代码替换上面代码中的for循环:

        for (int i = 0; i < this.data.length; i++) {
            if (this.data[i] != other.data[i]) {
                return false;
            }
        }

Finally, here is the solution for the multi-set case; 最后,这是多集案例的解决方案; ie where the elements of this.data are not necessarily unique in the array: this.data的元素在数组中不一定是唯一的:

public boolean equals(Object otherObject) {
    if (otherObject == this) {  
        return true;
    } else if (otherObject instanceof IntArray) {
        IntArray other = (IntArray) other;
        if (this.data.length != other.data.length) {
            return false;
        }
        for (int i = 0; i < this.data.length; i++) {
            if (this.count(this.data[i]) != other.count(this.data[i]) {
                return false;
            }
        }
        return true;
    } else {
        return false;
    }
}

public int count(int x) {
    int count = 0;
    for (int y : this.data) {
        if (x == y) {
            count++;
        }
    }
    return count;
}

Note that it is 注意它是

  if (this.count(this.data[i]) != other.count(this.data[i]) {

rather than 而不是

  if (this.count(this.data[i]) != other.count(other.data[i]) {

because we want to count the occurrences of the same value ... not the occurrences of the values at corresponding positions (which are probably different value!) 因为我们要计算相同值的出现次数...而不是在相应位置(可能是不同值!)中出现的值!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM