The indexof method of arraylist is required for a function during project creation today. The arraylist contains a set of custom notice objects, you need to know the location of the current notice object (this object is queried by another method) in this arraylist, check the API, and use the indexof method, however, when debugging, the returned value is-1, and the notice object does exist in the list and the content of the two objects is identical. So I wrote a demo to verify the problem.
Test Class 1: testclass. Name, ID
class TestClass {public TestClass(String name, String id) {this.name = name;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}String name;String id;}
Test code:
package test.test;import java.util.ArrayList;import java.util.List;public class TestIndexof {public static void main(String[] args) {List<TestClass> list = new ArrayList<TestClass>();TestClass c1 = new TestClass("first", "1");list.add(c1);TestClass c2 = new TestClass("second", "2");list.add(c2);TestClass c3 = new TestClass("third", "3");list.add(c3);TestClass c4 = new TestClass("third", "3");System.out.println("Index:"+list.indexOf(c3));System.out.println("Index:"+list.indexOf(c4));}}class TestClass {public TestClass(String name, String id) {this.name = name;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}String name;String id;}
Output result:
Index: 2
Index:-1
The C3 object returns an index of 2 and C4 is-1, and the content of both objects is the same. Note that C3 adds the list object through the add method, while C4 is new. Check the source code of indexof:
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; }
Because the testclass class does not overwrite the euqals method, the equals method of the object is used here. Because the passed C3 memory address is the same as the C3 memory address in the list, the correct result is returned, c4 is new, and the address in the memory is different from C3 in the list. Although their content is the same,-1 is returned.
To solve this problem, you can override the equals method of the object passed in indexof. Because the testclass object IDs are not repeated, the equals method is rewritten using the comparison ID method:
class TestClass {public TestClass(String name, String id) {this.name = name;this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}@Overridepublic boolean equals(Object obj) {if (obj instanceof TestClass) {if (this.getId().equals(((TestClass) obj).getId())) {return true;}else {return false;}}return false;}String name;String id;}
Run the test code again after modifying the testclass class. The result is as follows:
Index: 2
Index: 2