Java ArrayList IndexOf - Find the index of an object

Let's say I have a class

public class Data{ public int k; public int l; public Data(int k, int l){ this.k = k; this.l = l; } public boolean equals(Date m){ if(this.k == mk && this.l = ml) return true; return false; } } 

And I add some Data objects to the ArrayList:

 ArrayList<Data> holder = new ArrayList<Data>; Data one = new Data(0,0); Data two = new Data(0,4); Data three = new Data(0,5); 

Why doesn't indexOf find this ?:

 holder.indexOf(new Data(0,4)); //returns -1 

Is indexOf better than the list of array itself? Or am I missing something.

+7
java arraylist
source share
4 answers

The indexOf() method scans the entire list. Here is an excerpt from the Java 7 source code:

 public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } 

It’s better to let Java go through it rather than writing it yourself. Just make sure your equals method is sufficient to find the object you need. You will also want to override hashCode() .

I will not write your equals method, but I would recommend you at least:

  • Check null
  • Check if the instances you are comparing match.
  • You do not need to do if(boolean_expr) { return true; } if(boolean_expr) { return true; } ; just return the boolean expression.
  • Make sure you actually override your equals method - the Object parameter is required for signing, not Date .
+17
source share

The signature of your equals method is incorrect. You do not override equals in Object , but simply overload it.

To override the behavior of the equals method in Object , your signature must exactly match that in Object . Try the following:

 public boolean equals(Object o) { if(!(o instanceof Data)) return false; Data other = (Data) o; return (this.k == other.k && this.l == other.l); } 

Also, as suggested by others, it is recommended to override the hashCode method so that your object works correctly in map-based collections.

+8
source share

The answer from Makoto is right. I would say the same thing. But you have some errors in your code above.

  • You wrote "public boolean equals (Date m) {". I think you meant data instead of date.
  • You wrote "if (this.k == mk && this.l = ml)". The second condition is if the request should be "==".

To your question: Makoto's answer is one of the solutions. My solution is to use eclipse to automatically create hashcode and equals methods. Like this:

 public class Data { // your class code here public int hashCode() { final int prime = 31; int result = 1; result = prime * result + k; result = prime * result + l; return result; } public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof Data)) { return false; } Data other = (Data) obj; if (k != other.k) { return false; } if (l != other.l) { return false; } return true; } } 
+2
source share

By convention, you want to override hashcode also when you override equals

You will most likely find that indexOf uses the hashcode method to match an object, not equal

If you use eclise to edit code, eclipse will create a good equals and hashcode method for you from the "source" menu.

-2
source share

All Articles