为什么我在初始化具有非具体值的数组时得到ArrayIndexOutOfBoundsException:0?

时间:2016-05-26 18:20:53

标签: java

public class Gardener {


private int amountFlowerbed;

Gardener(int amountFlowerbed){

    this.amountFlowerbed=amountFlowerbed;
}
Flowerbed[] flowerbeds = new Flowerbed[amountFlowerbed];

public void work() {
    Watering_machine wateringMachine = new Watering_machine();

    Thread[] threads = new Thread[amountFlowerbed];
    for (int i=0;i<amountFlowerbed;i++) {
        flowerbeds[i]=new Flowerbed(wateringMachine,i+1);
        threads[i] = new Thread(flowerbeds[i]);
        threads[i].start();
    }
    }
}

当我用amountFlowerbed初始化数组花坛时,我得到了这个例外,但是如果我用一些具体的值(例如2)做它,它可以正常工作。因此,amountFlowerbed包含相同的值。最奇怪的是线程数组用amountFlowerbed初始化

0 个答案:

没有答案