Wieso bekomme ich immer dieses Probelm?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
[CODE lang="java" title="Labyrinth"]package labyrinth;
public class Main {
static int[][] labyrinth = new int[][] {
{1,1,1,1,1,1,1,1,1,1}, //0 = unknown
{1,0,1,3,0,0,0,0,0,1}, //1 = wall
{1,0,1,1,1,0,1,1,0,1}, //3 = start
{1,0,0,0,0,0,1,0,0,1}, //5 = up
{1,1,1,1,1,1,1,1,0,1}, //6 = right
{1,0,0,0,0,0,0,1,0,1}, //7 = left
{1,1,1,1,1,1,0,1,0,1}, //8 = down
{1,0,0,0,1,1,0,0,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1}};
public static void main(String[] args) {
for(int i=0; i<=9; i++){
for(int j=0; j<=9; j++){
System.out.print(labyrinth[j] + " ");
}
System.out.println("");
}
System.out.println();
if(search(3,1) == false) {}
System.out.println();
for(int i=0; i<=9; i++){
for(int j=0; j<=9; j++){
System.out.print(labyrinth[j] + " ");
}
System.out.println("");
}
}
public static boolean search(int x, int y){
if(labyrinth[x][y-1] == 0){
labyrinth[x][y-1] = 8;
if(y-1 == 0 || search(x, y-1))
{
return true;
}
}
if(labyrinth[x+1][y] == 0){
labyrinth[x+1][y] = 7;
if(x+1 == 0 || search(x+1, y))
{
return true;
}
}if(labyrinth[x][y+1] == 0){
labyrinth[x][y+1] = 5;
if(y+1 == 0 || search(x, y+1))
{
return true;
}
}if(labyrinth[x-1][y] == 0){
labyrinth[x-1][y] = 6;
if(x-1 == 0 || search(x-1, y))
{
return true;
}
}
return false;
}
}[/CODE]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
[CODE lang="java" title="Labyrinth"]package labyrinth;
public class Main {
static int[][] labyrinth = new int[][] {
{1,1,1,1,1,1,1,1,1,1}, //0 = unknown
{1,0,1,3,0,0,0,0,0,1}, //1 = wall
{1,0,1,1,1,0,1,1,0,1}, //3 = start
{1,0,0,0,0,0,1,0,0,1}, //5 = up
{1,1,1,1,1,1,1,1,0,1}, //6 = right
{1,0,0,0,0,0,0,1,0,1}, //7 = left
{1,1,1,1,1,1,0,1,0,1}, //8 = down
{1,0,0,0,1,1,0,0,0,1},
{1,0,1,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1}};
public static void main(String[] args) {
for(int i=0; i<=9; i++){
for(int j=0; j<=9; j++){
System.out.print(labyrinth[j] + " ");
}
System.out.println("");
}
System.out.println();
if(search(3,1) == false) {}
System.out.println();
for(int i=0; i<=9; i++){
for(int j=0; j<=9; j++){
System.out.print(labyrinth[j] + " ");
}
System.out.println("");
}
}
public static boolean search(int x, int y){
if(labyrinth[x][y-1] == 0){
labyrinth[x][y-1] = 8;
if(y-1 == 0 || search(x, y-1))
{
return true;
}
}
if(labyrinth[x+1][y] == 0){
labyrinth[x+1][y] = 7;
if(x+1 == 0 || search(x+1, y))
{
return true;
}
}if(labyrinth[x][y+1] == 0){
labyrinth[x][y+1] = 5;
if(y+1 == 0 || search(x, y+1))
{
return true;
}
}if(labyrinth[x-1][y] == 0){
labyrinth[x-1][y] = 6;
if(x-1 == 0 || search(x-1, y))
{
return true;
}
}
return false;
}
}[/CODE]