ArrayList: IndexOutOfBounds exception issue

ぃ、小莉子 提交于 2019-12-02 14:26:27

List indices run from 0 to size()-1, but you are looping from 1 to size().

The first element of a Collection has index 0, not 1. Thus for example, accessing the third element should be done as collection.get(2).

Your loops should iterate like follows:

outer: for (int a = 0; a < num.size(); a++){
    inner: for (int b = 0; b < prime.size(); b++){
        ...
    }
}

Note the strict (<) index comparison. Moreover, there is the following simpler way:

outer: for (Integer numItem : num){
    inner: for (Integer primeItem : prime){
        ...
    }
}

This for loop form iterates on the whole collection.

Try using Double instead of Integer. (Note how Double is an Object with a capital "D").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!