You cannot do this with indexOf. Instead all of the objects in the list should inherit from a common interface - for example
interface HasIdNum {
String getIdNum();
}
Now you list can be List<HasIdNum>
and you can loop through it to find the object by id using:
for (HasIdNum hid: objList) {
if (hid.getIdNum().equals(idNumToFind) {
return hid;
}
}
return null;
To get the index rather than the object do:
for (int i=0;i<objList.size();i++) {
HasIdNum hid = objList.get(i);
if (hid.getIdNum().equals(idNumToFind) {
return i;
}
}
return -1;
Alternatively you can use reflection to query the methods of the object, but that will be much slower and much less safe as you lose all the compile time type checking.