contains problem with own class
in
Programming Questions
•
5 months ago
contains uses the equals method of a object.
PVector has it's own for example so this will print true:
- void setup() {
- ArrayList<PVector> pvl = new ArrayList<PVector>();
- pvl.add(new PVector());
- PVector v = new PVector();
- println(pvl.contains(v));
- }
Notice i didn't add v to the list.
I want this as well on my own class.
This is my attempt:
- void setup() {
- Test t1 = new Test();
- Test t2 = new Test();
- println("equal: "+t1.equals(t2));
- ArrayList<Test> ta = new ArrayList<Test>();
- ta.add(t1);
- println("contains: "+ta.contains(t2));
- }
- class Test {
- int a = 0;
- Test() {
- }
- boolean equals(Test t) {
- return (a == t.a);
- }
- int hashCode() {
- int result = 1;
- result = 31 * result;
- return result;
- }
- }
The equals work, the contains does not.
I readed on stackoverflow that hascode has to be overwritten as well but it doesn't seem to help.
What is it i'm doing wrong?
1