Java的错误的ArrayIndexOutOfBounds [英] Java ArrayIndexOutOfBounds Error

查看:158
本文介绍了Java的错误的ArrayIndexOutOfBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个数组画布[256] [256]具有随机指标画布[R] [R](r为随机)通过阵列设置为1,那么我想环路看看究竟哪个索引不为0,然后随机选择一个点(上,下,左,右),并设置一个1为好。它的工作原理完全没有通过第一循环,但随后给了我一个数组越界的错误之后。

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?

推荐答案

好吧,让我们由一个走过所有的问题之一。

Alright, let's walk through all your problems one by one.


  1. 该指数。这是显而易见的,我不会再讨论。

  1. The indices. This is obvious and I won't discuss it further.

checkPopulation() particleDiffusion()递归调用对方。因为无论是 stepsTaken diffusionStep 在你最终会看到一个计算器的code路径将被修改。你似乎并不需要使用递归这个,为什么不使用循环呢?我假设你有初始呼叫checkPopulation(),因此,如何以替换它:

checkPopulation() and particleDiffusion() recursively invoke each other. Since neither stepsTaken or diffusionStep are modified in the code path you will eventually see a StackOverflow. You don't seem to need to use recursion for this, why not use a loop instead? I'm assuming you have an initial call to checkPopulation(), so how about replacing it with:

for (int stepsTaken = 0; stepsTaken < diffusionStep; stepsTaken++) {
    checkPopulation();
}

现在,在较高的水平,你的方法看起来像

Right now, at a high level, your methods look like

main() {
    checkPopulation();
}

checkPopulation() {
    ...
    particleDiffusion();
}

particleDiffusion() {
    ...
    if (stepsTaken < diffusionStep) {
        checkPopulation();
    }
}

通过的变化,它现在看起来像

With the change, it now looks like

main() {
    for (int stepsTaken = 0; stepsTaken < diffusionStep; stepsTaken++) {
        checkPopulation();
    }
}

checkPopulation() {
    ...
    particleDiffusion();
}

particleDiffusion() {
    ...
}


  • 调用 r.nextInt(3)== X 多次不跨越四个方向分布的概率均匀。但也不能保证任何的四种情况实际上会成功。保存的结果r.nextInt(3)中的一个变量,然后做这个变量的比较。

  • Calling r.nextInt(3) == x multiple times does not distribute the probability across the four directions evenly. There's no guarantee any of the four cases will actually be successful. Save the result of r.nextInt(3) in a variable and then do the comparison on that variable.

    这篇关于Java的错误的ArrayIndexOutOfBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆