So I have some code which finds neighbours of a given State. So the positions the blank tile (which is represented as 0 in my code) can be moved.I am doing the 3x3 tile puzzle. The goal state is represented like this :1 2 34 5 67 8 0Say I have a problem state which is:8 5 1 3 4 7 0 6 2I would call the findNeighbours() method on this State and it should return two states like this .8 5 1                             8 5 1 3 4 7   (This is a right move)    0 4 7  (This is an up move)     6 0 2                             3 6 2 The reason I'm posting here is because I'm not getting the performance I'd hoped for and I'm assuming this findNeighbours() is the issue as the code is clunky to say the least! I should also point out that state is an array which looks something like state = {1,2,3,4,5,6,7,8,0} and neighbours is an ArrayList which I am adding every State to   public void findNeighbours() {        for (int i = 0; i < state.length; i++) {            if (state[i] == 0) {                if (i % 3 != 0) {                    int[] left = new int[9];                    System.arraycopy(state, 0, left, 0, left.length);                    int temp = left[i];                    left[i] = left[i - 1];                    left[i - 1] = temp;                    State newState = new State(left,this, left);                    this.neighbours.add(newState);                }                if (i % 3 != 2) {                    int[] right = new int[9];                    System.arraycopy(state, 0, right, 0, right.length);                    int temp = right[i];                    right[i] = right[i + 1];                    right[i + 1] = temp;                    State newState = new State(right, this, right);                    this.neighbours.add(newState);                }                if (i > 3) {                    int[] up = new int[9];                    System.arraycopy(state, 0, up, 0, up.length);                    int temp = up[i];                    up[i] = up[i - 3];                    up[i - 3] = temp;                    State newState = new State(up, this, up);                    this.neighbours.add(newState);                }                if (i < 6) {                    int[] down = new int[9];                    System.arraycopy(state, 0, down, 0, down.length);                    int temp = down[i];                    down[i] = down[i + 3];                    down[i + 3] = temp;                    State newState = new State(down,  this, down);                    this.neighbours.add(newState);                }                break;            }        }    }
Finding all neighbours of a node sliding tile
java;array;sliding tile puzzle