ArrayIndexOutOfBoundsException与错误的索引无关

时间:2018-06-27 09:17:44

标签: java

我有来自main的此方法调用:

exercise.SmallestOf(new int[] {2,9,6,4,8,34,2,15,81})

这是该方法的作用:

public int SmallestOf(int[] collection) {
    int min = 0;
    for(int i:collection) {
        if (collection[i]<min)
            min = collection[i];
    }
    return min;
}

在运行时出现ArrayIndexOutOfBoundsException错误。 我认为这有问题:

new int[] {2,9,6,4,8,34,2,15,81}

我知道我可以做到:

int[] c = new int[] {2,9,6,4,8,34,2,15,81}
exercise.SmallestOf(c)

但是我仍然想知道:后面会发生什么并产生错误?

0 个答案:

没有答案