繁体   English   中英

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

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

我在名为IntArray的类中有此java方法。 该类具有以下方法:将整数添加到集合中或从集合中删除整数,检查集合的大小,并检查2个集合是否相等。 这2组是使用main中2个不同的IntArray类型的对象创建的,可以说对象A和B。equals方法应该检查两组整数是否相等。 例如,设置A = {1,2,3}和B = {1,2,3,4}。 即使一个集合是另一集合的子集,该方法仍然返回true。 我到底在做什么错? 谢谢。

//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]))

可能是

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

编辑:

如果相等,则表示子集中的每个元素的顺序相同(A = {1,2,3}和B = {1,2,3}):

然后,您要使用equals方法检查两个整数子集是否相等:

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

确保仅比较两个长度相同的集合。 否则,返回false。

如果您对equals set的定义意味着两个具有相同元素的集合,而不必考虑它们的位置

您应该检查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;
}

在后一种情况下,您应该保留if (countOccurrences(data[i]) != A.countOccurrences(data[i]))

预先检查两个列表的长度是否相同。 如果它们的长度不同,则返回false。 如果它们的长度相同,则进行逐元素比较。

这是上述问题的解决方案。 请注意,它假定IntArray对象代表一个真实的集合,而不是bag / multi-set。 假定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;
    }
}

如果保证 data数组是有序的,则可以进行简单的逐元素比较。 for代码替换上面代码中的for循环:

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

最后,这是多集案例的解决方案; 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;
}

注意它是

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

而不是

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

因为我们要计算相同值的出现次数...而不是在相应位置(可能是不同值!)中出现的值!

暂无
暂无

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

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