So I have an array Canvas[256][256] that has a random index Canvas[r][r] (r being random) set to 1. I then want to loop through the array to see exactly which index is not 0, and then pick a random spot (above, below, left, right) and set that one to 1 as well. It works perfectly fine through the first loop, but then gives me an array out of bounds error after that.
public static void checkPopulation() {
for(int x = 0; x < Canvas.length; x++) {
for(int y = 0; y < Canvas.length; y++) {
if(Canvas[x][y] != 0) {
particleDiffusion(x, y);
}
}
}
}
public static void particleDiffusion(int x, int y) {
Random r = new Random();
if(r.nextInt(3) == 0) {
Canvas[x+1][y] = 255;
} else if(r.nextInt(3) == 1) {
Canvas[x][y+1] = 255;
} else if(r.nextInt(3) == 2) {
Canvas[x-1][y] = 255;//THIS IS WHERE ERROR IS POINTING
} else if(r.nextInt(3) == 3) {
Canvas[x][y-1] = 255;
}
if(stepsTaken < diffusionStep) {
checkPopulation();
} else {
System.out.println("done");
return;
}
}
can somebody help me with what I am doing wrong? And why it loops through once and then gives the error?
x = 0at the line you indicated?square 2-d array..?