The array is filled with a snake in a circle.
An error occurs:
"Exception in thread" java.lang.ArrayIndexOutOfBoundsException: -1 at para13.main (para13.java:20) ".
Help me fix it, please. Code:
public class para13{ public static void main(String[] args) { int z = 6; int[][] a = new int[z][z]; int x = 0, y = -1, l = 1; boolean ch; for (int i = 1; i<z*z; ++i){ if ((y+1<z) && (a[x][y+1] == 0)){ y = y + 1; a[x][y] = i; } else{ if ((x+1<z) && (a[x+1][y] == 0)){ x = x + 1; a[x][y] = i; } else{ if ((y-1<z) && (a[x][y-1] == 0)){ y = y - 1; a[x][y] = i; } else { if ((x-1<z) && (a[x-1][y] == 0 )){ x = x - 1; a[x][y] = i; } } } } //System.out.print("x " + x + " "); //System.out.println("y " + y); //printMas(a); } printMas(a); } public static void printMas(int[][] b) { //вывод массива for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { System.out.print(b[i][j] + "\t"); } System.out.println(); } System.out.println(); } }