Trying to find all occurrences of an object in Arraylist, in java

I suppose you need to get all indices of the ArrayList where the object on that slot is the same as the given object.

The following method might do what you want it to do:

public static <T> int[] indexOfMultiple(List<T> list, T object) {
    List<Integer> indices = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).equals(object)) {
            indices.add(i);
        }
    }
    // ArrayList<Integer> to int[] conversion
    int[] result = new int[indices.size()];
    for (int i = 0; i < indices.size(); i++) {
        result[i] = indices.get(i);
    }
    return result;
}

It searches for the object using the equals method, and saves the current array index to the list with indices. You're referring to indexOf in your question, which uses the equals method to test for equality, as said in the Java documentation:

Searches for the first occurence of the given argument, testing for equality using the equals method.


Update

Using Java 8 streams it'll become much easier:

public static <T> int[] indexOfMultiple(List<T> list, T object) {
    return IntStream.range(0, list.size())
        .filter(i -> Objects.equals(object, list.get(i)))
        .toArray();
}

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;
}