繁体   English   中英

线程“主”中的异常java.lang.IndexOutOfBoundsException:索引:15,大小:5

[英]Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 15, Size: 5

嗨,我正在尝试在euler项目中做第一个问题,它要求1000以下3或5的倍数之和。这是我编写的代码。

import java.util.ArrayList;

public class MultiplesOf3And5 {

    public static void main(String[] args)
    {
        int x = 0; // multiples of 3
        int y = 0; // multiples of 5
        int sum = 0; // sum of the multiples of 3 or 5
        int quotient3; //check for the remainder of numbers that are multiples of 3
        int quotient5; //check for the reaminder of numbers that are multiples of 5

        ArrayList<Integer> multiples = new ArrayList<Integer>();

        while ( x <= 999 && y <= 1000)
        {

            quotient3 = x % 3; // check remainder 
            quotient5 = y % 5; // check reaminder

            if (quotient3 == 0 && quotient5 == 0) // check if both x and y are multiples of 3 and 5
            {
                multiples.add(x); // if true put it in a arraylist
            }

            if (quotient3 == 0) // checks if x is a multiples of 3 
            {
                if (multiples.contains(x)) // check if x is already in the arraylist 
                {
                    EmptyStatement:;
                }
                else {
                    multiples.add(x); // add x in the arraylist 
                }
            }

            if (quotient5 == 0)
            {
                if (multiples.contains(y))
                {
                    EmptyStatement:;
                }
                else {
                    multiples.add(y);
                }
            }

            x+=3;
            y+=5;

        }

        for (int i = 0; i <= multiples.size(); i++) // loop into the arraylist and get the sum of the elements
        {
            int value = (int) multiples.get(i);
            sum = sum + value;
        }
        System.out.print(sum);
    }
}

修复了一些编译器错误后,我设法对其进行了编译。 但是,一旦我运行它,我就会看到错误消息,

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 15, Size: 5
at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at MultiplesOf3And5.main<MultiplesOf3And5.java:23)

我已经搜索了此错误,但仍然无法使它正常工作。

看到您的循环时间超过了arraylist的大小。
您必须使用i <multiples.size(); 而不是我<= multiples.size();

因此,替换您的代码:

for (int i = 0; i <= multiples.size(); i++) 
// loop into the arraylist and get the sum of the elements
        {
        int value = (int) multiples.get(i); 
        sum = sum + value;
        }

附:

for (int i = 0; i < multiples.size(); i++) 
// loop into the arraylist and get the sum of the elements
        {
        int value = (int) multiples.get(i); 
        sum = sum + value;

        }

arrayList基于零,因此如果arraylist.size = 400,则您的最高索引为399。循环应使用less than (<)而不是less than or equal to (<=)

那使用呢

for(int i : multiples){

sum += i ;

}

更简单,这种声明至少不会遇到任何溢出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM