2

IntArray というクラスにこの Java メソッドがあります。このクラスには、セットに整数を追加したり、セットから整数を削除したり、セットのサイズをチェックしたり、2 つのセットが等しいかどうかをチェックしたりするメソッドがあります。2 つのセットは、メインのタイプ IntArray の 2 つの異なるオブジェクトを使用して作成されます。たとえば、オブジェクト A と B. equals メソッドは、整数の 2 つのセットが等しいかどうかをチェックすることになっています。たとえば、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;  
}
4

3 に答える 3

3
 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 メソッドを使用して、整数の 2 つのサブセットが等しいかどうかを確認します。

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

両方の長さが同じ場合にのみ、2 つのセットを比較してください。それ以外の場合は false を返します。

equals セットの定義が、位置に関係なく、同じ要素を持つ 2 つのセットを意味する場合:

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]))ます。

于 2012-11-22T00:43:34.467 に答える
1

2 つのリストの長さが同じであることを事前に確認します。長さが同じでない場合は、false を返します。それらが同じ長さの場合は、要素ごとの比較を行います。

于 2012-11-22T00:46:18.947 に答える
1

述べたように、ここに問題の解決策があります。IntArrayオブジェクトがバッグ/マルチセットではなく、真のセットを表していると想定していることに注意してください。配列内の値が順序付けられているとは想定していません。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 (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]) {

同じ値の出現をカウントしたいので...対応する位置での値の出現ではありません(おそらく異なる値です!)

于 2012-11-22T10:49:21.193 に答える