I have an ArrayList in Java, and I need to find all occurrences of a specific object in it. The method ArrayList.indexOf(Object) just finds one occurrence, so it seems that I need something else.
I don't think you need to be too fancy about it. The following should work fine:
static <T> List<Integer> indexOfAll(T obj, List<T> list) {
final List<Integer> indexList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (obj.equals(list.get(i))) {
indexList.add(i);
}
}
return indexList;
}