边界外异常问题,Java

边界外异常问题,Java,java,arraylist,indexoutofboundsexception,Java,Arraylist,Indexoutofboundsexception,我得到以下错误: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 86, Size: 86 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at Netbooks.Recommendations.getDotProduct(Recommendation

我得到以下错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 86, Size: 86
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at Netbooks.Recommendations.getDotProduct(Recommendations.java:72)
at Netbooks.TestRecomendations.main(TestRecomendations.java:11)
Java Result: 1
我已经查看了很多次代码,但我似乎找不到我在哪里浏览arraylist的索引

以下是dotProduct ArrayList的代码:

public List<Integer> getDotProduct() throws IOException {
    Books book = new Books();
    Ratings cust = new Ratings();
    PureRatings pureRatings = new PureRatings();


    List<String> bookList = book.readBooks();
    List<String> customerList = cust.readCustomers();
    List<List<Integer>> pureRatingsList = pureRatings.parseRatingsFile();
    List<Integer> dotProduct = new ArrayList<Integer>();
    int index = getCustIndex();

    if (index == -1) {
        return dotProduct;
    }

    for (int i = 0; i < customerList.size(); i++) {
        int sum = 0;

        for (int j = 0; j < bookList.size(); i++) {
            if (i == index) {
                dotProduct.add(0);
            } else { //Next line is line 72.
                sum = sum + (pureRatingsList.get(index).get(j)) * (pureRatingsList.get(i).get(j)); //Line 72.
            }
        }
        dotProduct.add(sum);
    }

    return dotProduct;
}
我在另一个类中的主要方法只是以防万一:

public class TestRecomendations {

    public static void main(String[] args) throws IOException {
        Recommendations recomm = new Recommendations();

        List<Integer> dotProduct = recomm.getDotProduct();//Line 11.

        for (int i = 0; i < dotProduct.size(); i++) {
            System.out.println(dotProduct.get(i));
        }
    }
}
它应该只打印出dotProduct ArrayList的元素


我不明白第72行是如何引起问题的,因为我应该能够向ArrayList添加无限数量的项……如果有任何帮助,我将不胜感激。

您知道,有迭代器和foreach这样的东西可以使遍历集合变得更简单吗


问题是列表的索引从0开始,而您尝试从1开始。

您知道迭代器和foreach之类的东西可以简化遍历集合的过程吗


问题是列表的索引从0开始,而您尝试从1开始。

这会导致问题,因为您请求的索引不存在;尔戈出界了

当大小仅为86(索引0-85)时,您正在请求索引86。数组是基于零的


学习使用调试器将真正帮助您解决这样的问题,因为您可以逐步完成程序并准确地看到发生了什么

这会导致问题,因为您请求的索引不存在;尔戈出界了

当大小仅为86(索引0-85)时,您正在请求索引86。数组是基于零的


学习使用调试器将真正帮助您解决这样的问题,因为您可以逐步完成程序并准确地看到发生了什么

第72行中的问题是get,而不是add

我怀疑这可能是问题的根本原因:

for (int i = 0; i < customerList.size(); i++) {
    int sum = 0;

    for (int j = 0; j < bookList.size(); i++) {  
        if (i == index) {
            dotProduct.add(0);
        } else { //Next line is line 72.
            sum = sum + (pureRatingsList.get(index).get(j)) * (pureRatingsList.get(i).get(j)); //Line 72.
        }
    }
    dotProduct.add(sum);
}

大于pureRatingsList的大小,导致您看到的异常。

第72行中的问题是get,而不是add

我怀疑这可能是问题的根本原因:

for (int i = 0; i < customerList.size(); i++) {
    int sum = 0;

    for (int j = 0; j < bookList.size(); i++) {  
        if (i == index) {
            dotProduct.add(0);
        } else { //Next line is line 72.
            sum = sum + (pureRatingsList.get(index).get(j)) * (pureRatingsList.get(i).get(j)); //Line 72.
        }
    }
    dotProduct.add(sum);
}

大于pureRatingsList的大小,导致您看到的异常。

这行不是问题所在吗

对于int j=0;j 我想你需要的是


对于int j=0;j这行不是问题所在吗

对于int j=0;j 我想你需要的是


对于int j=0;j索引基于0。您正在访问86项列表的第87项索引86。很可能您的索引变量出错。例如,您从[0,customerList.size,但在pureRatingsList中使用i。get…correct?您是否尝试使用调试器?在java.lang.IndexOutOfBoundsException上放置断点,并查看其中断的原因。索引基于0。您正在访问包含86项的列表的第87项索引86。很可能您的索引变量出错。例如,您从M[0,customerList.size,但在pureRatingsList中使用i。获取…正确吗?您是否尝试过使用调试器?在java.lang.IndexOutOfBoundsException上放置一个断点,看看它为什么会中断。我今天几乎不了解迭代器,虽然我肯定要研究迭代器,但对迭代器的理解还不够,无法有效地使用迭代器。我从哪里开始学习ex at 1?我今天几乎没学过迭代器,对迭代器的理解还不足以有效地使用迭代器,尽管我肯定要学习它们…我从哪里开始索引1?谢谢!刚刚用j替换了I,现在运行良好。总是简单的错误…谢谢!刚刚用j替换了I,现在运行良好。总是simp错误。。。