The problem I'm running into is that given the ArrayList [0, 0, 4, 0, 0, 0], my function returns [0, 4, 0, 0, 0, 0], where I expect 4 to be the first value in the list.

Here's the function;

public static ArrayList<Integer> zerosBack(ArrayList<Integer> arr) { int zeroes = 0; for(int i = 0; i < arr.size(); i++) { if(arr.get(i) == 0) { arr.remove(i); zeroes = zeroes + 1; } } if(zeroes > 0) { for(int i = 0; i < zeroes; i++) { arr.add(0); } } return arr; }