0
public class Main {
public static class EE implements Comparable<EE> {
int x;
int[] rac;
public EE(int x, int[] rac) {
this.x = x;
this.rac = rac;
}
public int compareTo(EE that) {
if (this.x != that.x) return this.x - that.x;
else return this.rac[2] = that.rac[2];
}
}
public static void main(String[] args) {
int [][] ary = {
{1,1,3,3},
{1,3,2,4},
{2,3,3,4}};
PriorityQueue<EE> pq = new PriorityQueue<EE>();
for (int[] rec : ary) {
EE e1 = new EE(rec[0], rec);
EE e2 = new EE(rec[2], rec);
pq.add(e1);
pq.add(e2);
}
}
私はこのコードを実行していますが、すべては問題ありませんが、2番目のforループが入力されたとき、recは最初は[1,3,2,4] pq.add(e1)が呼び出されると、recの値は[1、3、3、4]になります。なぜ誰がこのことが起こるのか説明できますか?前もって感謝します!PriorityQueue add要素は要素を変えます、変わったバグ
これは機能します!私は許可された後、この答えを受け入れますが、なぜこれが要素の値を変更するか考えて、追加されましたか? –
あなたが持っていたのは、凝縮された課題でした。基本的にはあなたの声明はthis.rac [2] = that.rac [2]を割り当ててからthis.rac [2]を返すことでした。それは同時に両方を行い、したがって値が変化しました。代入を実行しない比較演算子として==を使用する必要がありました。 – Jacob