意外的 ArrayIndexOutOfBoundsException

发布于 2024-12-09 03:11:18 字数 4048 浏览 1 评论 0原文

我在 java 中使用数组列表来存储和处理文件中的数据。在中等或小型数据集上,它工作得很好,但异常大的数据集会抛出 ArrayIndexOutOfBoundsException ,并且没有指定哪一行代码抛出异常。这可能是一个错误或内存不足的问题吗?该代码不会对数据大小进行硬编码,因此无法调用不存在的索引。

编辑: 这是抛出错误的方法:

public static ArrayList<Grain> FindGrains(Point[][] p){
    ArrayList<Grain> grains = new ArrayList<Grain>();
    ArrayList<Point> growthPoints = new ArrayList<Point>();
    ArrayList<Point> previousPoints = new ArrayList<Point>();
    boolean finGrainGrow = false;
    int x = -1;
    int y = -1;
    int j, k;
    int grainNum =-1;
    double stepSize = 0.5;

    while(true){
        x = -1;
        y = -1;

        growthPoints.clear();
        previousPoints.clear();

        for(int f =0; f<p.length;f++){
            for(int r =0; r<p[0].length;r++){
                if(p[r][f].getGrainNum() == -1){
                    x = r;
                    y = f;
                    grains.add(new Grain());
                    grainNum++;
                    p[x][y].setGrainNum(grainNum);
                    grains.get(grainNum).add(p[x][y]);
                    growthPoints.add(0,p[x][y]);
                    break;
                }
            }
            if (x!=-1) break;
        }
        if (x==-1) break;

        finGrainGrow = false;

        while(!finGrainGrow){

            finGrainGrow = true;
            previousPoints.clear();
            previousPoints.ensureCapacity(growthPoints.size());
            for(int q=0;q<growthPoints.size();q++){
                previousPoints.add(q,growthPoints.get(q));
            }
            growthPoints = new ArrayList<Point>();

            for(int h = 0; h<previousPoints.size(); h++){
                j = (int)(previousPoints.get(h).getX()/stepSize);
                k = (int)(previousPoints.get(h).getY()/stepSize);
                try{
                    if(checkMisorientation(p[j][k],p[j-1][k]) && p[j-1][k].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j-1][k]);
                        growthPoints.add(p[j-1][k]);
                        p[j-1][k].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j+1][k]) && p[j+1][k].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j+1][k]);
                        growthPoints.add(p[j+1][k]);
                        p[j+1][k].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j][k-1]) && p[j][k-1].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j][k-1]);
                        growthPoints.add(p[j][k-1]);
                        p[j][k-1].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j][k+1]) && p[j][k+1].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j][k+1]);
                        growthPoints.add(p[j][k+1]);
                        p[j][k+1].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
            }

        }
    }
    return grains;
}

我有一个名为 Point 的对象,它包含 5 个 double 值和一个 int(一个 x 和 y 坐标、三个欧拉角以及它所属的grain的 int )。二维点数组使用粒度数 -1 和其他参数的通用值进行初始化。然后从文件中读取正确的值并将其保存到适当的位置。我试图将相邻的点排序到称为 Grain 的对象中,这些对象只是点的数组列表。抛出的错误是:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException

main 方法调用 FindGrains 方法。它没有给出遇到异常的行号,所以我真的不知道我在做什么。非常感谢任何帮助。

I'm using arraylists in java to store and process data from a file. On moderate or small data sets it works perfectly fine, but exceptionally large data sets throw an ArrayIndexOutOfBoundsException with no specification on which line of code threw the exception. Could this be a bug or a problem with running out of memory? The code doesn't hard code a data size, so there's no way that an index that doesn't exist is being called.

Edit:
This is the method throwing the error:

public static ArrayList<Grain> FindGrains(Point[][] p){
    ArrayList<Grain> grains = new ArrayList<Grain>();
    ArrayList<Point> growthPoints = new ArrayList<Point>();
    ArrayList<Point> previousPoints = new ArrayList<Point>();
    boolean finGrainGrow = false;
    int x = -1;
    int y = -1;
    int j, k;
    int grainNum =-1;
    double stepSize = 0.5;

    while(true){
        x = -1;
        y = -1;

        growthPoints.clear();
        previousPoints.clear();

        for(int f =0; f<p.length;f++){
            for(int r =0; r<p[0].length;r++){
                if(p[r][f].getGrainNum() == -1){
                    x = r;
                    y = f;
                    grains.add(new Grain());
                    grainNum++;
                    p[x][y].setGrainNum(grainNum);
                    grains.get(grainNum).add(p[x][y]);
                    growthPoints.add(0,p[x][y]);
                    break;
                }
            }
            if (x!=-1) break;
        }
        if (x==-1) break;

        finGrainGrow = false;

        while(!finGrainGrow){

            finGrainGrow = true;
            previousPoints.clear();
            previousPoints.ensureCapacity(growthPoints.size());
            for(int q=0;q<growthPoints.size();q++){
                previousPoints.add(q,growthPoints.get(q));
            }
            growthPoints = new ArrayList<Point>();

            for(int h = 0; h<previousPoints.size(); h++){
                j = (int)(previousPoints.get(h).getX()/stepSize);
                k = (int)(previousPoints.get(h).getY()/stepSize);
                try{
                    if(checkMisorientation(p[j][k],p[j-1][k]) && p[j-1][k].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j-1][k]);
                        growthPoints.add(p[j-1][k]);
                        p[j-1][k].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j+1][k]) && p[j+1][k].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j+1][k]);
                        growthPoints.add(p[j+1][k]);
                        p[j+1][k].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j][k-1]) && p[j][k-1].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j][k-1]);
                        growthPoints.add(p[j][k-1]);
                        p[j][k-1].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
                try{
                    if(checkMisorientation(p[j][k],p[j][k+1]) && p[j][k+1].getGrainNum() ==-1){
                        grains.get(grainNum).add(p[j][k+1]);
                        growthPoints.add(p[j][k+1]);
                        p[j][k+1].setGrainNum(grainNum);
                        finGrainGrow = false;
                    }
                }
                catch(ArrayIndexOutOfBoundsException t){
                }
            }

        }
    }
    return grains;
}

I have objects called Point, which contain 5 double values and an int(an x and y coordinate, three euler angles, and an int of which grain it belongs to). A two dimensional array of points is initialized with a grain number of -1 and generic values for the other parameters. The correct values are then read in from a file and saved to the appropriate point. I'm trying to sort adjacent Points into objects called Grain, which are just ArrayLists of Points. The error being thrown is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

The main method calls the FindGrains method. It doesnt give a line number for where the exception is encountered so I really have no idea what I'm doing. Any help is very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-16 03:11:18

这当然与OutOfMemoryException无关。

如果您要调用 ArrayList 的以下任何 API,请查看代码中的所有位置:
get(index)、set(index, data)、add(index, data)、remove(index)、addAll(index, collection)、removeRange(fromIndex, toIndex)

调试的重点应该围绕您所在的代码段正在使用 ArrayList 的任何这些 API。您的代码肯定试图访问不存在的 ArrayList 实例的索引。

This is certainly not related to OutOfMemoryException.

Please look at all the places in code if you are calling any of the following APIs of your ArrayList:
get(index), set(index, data),add(index, data), remove(index), addAll(index, collection), removeRange(fromIndex, toIndex)

Your focus of debugging should be around the piece of code where you are using any of these APIs of ArrayList. Your code is certainly trying to access an index of your ArrayList instance which does not exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文