Possible Duplicate:
Looping in a spiral
I'm creating a program to populate a 3 by 3 matrix. I want to result in something looking like this
5 4 3
6 1 2
7 8 9
As you have probably noticed it is a spiral. Now the algorithm I'm using is this: I have a 2-d array where the values represent the coordinates of the number. First I assign that every number coordinate in this array will have a value of 10. Then starting at 9 I decrease my x coordinate and assign the value of the coordinate to currentnum - 1 until it reaches the end or its value is not 10; Then I do the same thing except I increase the value of Y; Then decrease the value of x; Then of Y;
The reason I assign 10 to every number is so like it acts as a road for my program. Since current num will never exceed 9. If the value of a square is 10 it is like a green light. If it is not 10 meaning a value has been assigned to that square it breaks out of it.
Here is my code, please note it is written in Java
public class spiral {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int spiral [] [] = new int[3][3];
        for(int i = 0; i <= 2; i++){
            for(int j = 0; j <= 2; j++){
                spiral[i][j] = 10;
            }
        }
        //0 is x value, 1 is y value
        spiral[0][0] = 9;
        int x = 1;
        int y = 1;
        int counter = 1;
        int currentnum = 9;
        int gridsquare  = 3;
        for(int i = 0; i <= 8; i++){
            if(counter == 5){
                counter = 1;
            }
            if(counter == 1){
                System.out.println(x + " " + y);
                for(int j = 0;j <= 1;j++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x += 1;
                    }
                    else{
                        y += 1;
                        break;
                    }
                }
            }
            if(counter == 2){
                for(int k = 0; k <= 0; k++){
                    System.out.print(x + " " + y);
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y += 1;
                    }
                    else{
                        x -= 1;
                        break;
                    }
                }
            }
            if(counter == 3){
                for(int z = 0; z <= 0; z++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        x -= 1;
                    }
                    else{
                        y -= 1;
                        break;
                    }
                }
            }
            if(counter == 4){
                for(int b = 0; b <= 0; b++){
                    if(spiral[x][y] == 10){
                        spiral[x][y] = currentnum;
                        currentnum--;
                        y -= 1;
                    }
                    else{
                        x += 1;
                        break;
                    }
                }
            }
            counter++;
        }
        System.out.print(currentnum);
    }
}
I'm getting this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at spiral.main(spiral.java:44)
Since I'm new to Java would someone please suggest a posible fix for this. Also if you see any problems with my algorithm please do inform me.
 
     
     
    