As I said in my comment, your method doesn't require shifting the array because the method only checks if a given is already shifted (and returns false in that case. But for the sake of answering, here is the corrected version:
import java.util.Arrays;
public class Board {
public static final int EMPTY = 0;
public static boolean alterOneLine(int[] x) {
int[] temp = new int[x.length];
int tempCounter = 0;
for (int i = 0; i < x.length; i++) {
if (x[i] != EMPTY)
temp[tempCounter++] = x[i];
}
if (Arrays.equals(x, temp))
return false;
else
return true;
}
public static boolean alterOneLine2(int[] x) {
boolean zeroSeen = false;
for (int i = 0; i < x.length; i++) {
if (x[i] != 0 && zeroSeen) {
return true;
}
if (x[i] == 0) {
zeroSeen = true;
}
}
return false;
}
public final static void main(String[] args) {
System.out.println(alterOneLine(new int[]{0,8,0,7,0,0,2}));
System.out.println(alterOneLine(new int[]{8,7,2,0,0,0,0}));
System.out.println(alterOneLine(new int[]{5,0,0,0,1,0,3}));
System.out.println(alterOneLine(new int[]{5,1,3,0,0,0,0}));
System.out.println(alterOneLine2(new int[]{0,8,0,7,0,0,2}));
System.out.println(alterOneLine2(new int[]{8,7,2,0,0,0,0}));
System.out.println(alterOneLine2(new int[]{5,0,0,0,1,0,3}));
System.out.println(alterOneLine2(new int[]{5,1,3,0,0,0,0}));
}
}
Your error was using the same index when assiging a value to temp instead of an individual counter that is only increased if you shift a value to the left.
I also added a second method with the same functionality but without creating a temporary array and supsequent call of Arrays#equals (two iterations over the whole array instead of one with my new method).
trueif a given array is shifted. Your code is essentially just creating a copy of the original array and does no shift. You have to keep an additional counter fortempthat you only increment when assigning a new value, e.g.temp[tmpCounter++] = x[i]