0

I have array list in java:

List<Correction> Auv=new ArrayList<>();
List<Correction> Produv=new ArrayList<>();

then I want to substract Produv value by Auv, here's an example:

Produv.add(new Correction("a","b"));
Produv.add(new Correction("a","c"));
Produv.add(new Correction("b","d"));
Produv.add(new Correction("b","c"));

Auv.add(new Correction("c","a"));
Auv.add(new Correction("b","c"));

Produv.removeall(Auv);

but nothing subtracted, the array still contain it initial value, is there any way to do this? I try to override equals(), and still got the same result

here the code of my Correction class:

    public class Correction {
    private String node0;
    private String node1;

    public Correction(String node0, String node1) {
        this.node0 = node0;
        this.node1 = node1;
    }

    public void setNode0(String node0){
        this.node0=node0;
    }
    public void setNode1(String node1){
        this.node1=node1;
    }

    public String getNode0(){
        return node0;
    }
    public String getNode1(){
        return node1;
    }

    @Override
    public boolean equals(Object object){
        boolean same = false;

        if (object != null && object instanceof Correction)
        {
            same = this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node1;
        }

        return same;
    }
}

Solved!! it just simply a mistake on overriding equals() method(thank's guys) here my correction:

   @Override
    public boolean equals(Object object){
        boolean same = false;

        if (object != null && object instanceof Correction)
        {
            same = (this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node0)||(this.node0 == ((Correction) object).node0 && this.node1 == ((Correction) object).node1);
        }

        return same;
    }
8
  • 1
    Should this.node0 be equal to object.node1 or object.node0? Commented Jan 14, 2015 at 19:57
  • 1
    Also, the code you have won't compile. Commented Jan 14, 2015 at 19:57
  • 1
    Auv.add("c","a"); doesn't make any sense Commented Jan 14, 2015 at 19:58
  • 1
    Additionally, How do I compare strings in Java? applies. Commented Jan 14, 2015 at 19:58
  • 1
    Looks like mostly a simple typo to me. (Besides comparing strings.) However, I'll add that Correction overrides equals but does not override hashCode(), which is poor practice. Commented Jan 14, 2015 at 19:59

1 Answer 1

2

Your equals method looks wrong. It makes more sense to compare this.node0 to ((Correction) object).node0.

I think it should be:

public boolean equals(Object object){
    boolean same = false;

    if (object != null && object instanceof Correction)
    {
        same = this.node0.equals(((Correction) object).node0) && this.node1.equals(((Correction) object).node1);
    }

    return same;
}

Also, is this a typo?

Auv.add("c","a");
Auv.add("b","c");

It should probably be :

Auv.add(new Correction ("c","a"));
Auv.add(new Correction ("b","c"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.