繁体   English   中英

尝试使用基于 1 的索引创建 ArrayList 的索引越界异常

[英]Index out of bounds exception trying to create ArrayList with 1-based indices

所以到目前为止,我只是使用points.add(new Point(x,y))将点添加到我的ArrayList 但是,我发现我需要第一个点在 index=1 中,以便可以将每个步骤的数字相乘。 所以我尝试将counter0设置为1 ,正如预期的那样,我知道我会因为范围而出错,但是我尝试在 while 循环中更改条件,但似乎没有任何效果。

这是我的代码:

ArrayList<Point> points = new ArrayList<>();
int counter = 1;
int nPoints = 12;
while (counter <= nPoints) {
    x = (int) (centerX + r * Math.cos(start));
    y = (int) (centerY + r * Math.sin(start));

    points.set(counter, (new Point(x, y)));
    //points.add(new Point(x,y));

    counter++;
}

这是我得到的错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0   
    at java.util.ArrayList.rangeCheck(Unknown Source)   
    at java.util.ArrayList.set(Unknown Source)

为了更容易阅读,我删除了与此问题无关的大部分代码。

编辑:

 public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            a = getWidth() /2;
            b = getHeight() /2;
            g2d.setColor(Color.RED);

            //draw cirle
            g2d.drawOval(a-r, b-r, 2*r, 2*r);

            //draw lines
            for (int  i= 1; i < points.size();i ++) {
                g2d.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);

            }

           g2d.dispose();
        }

我创建了一个圆,我试图在其中沿着圆周在点之间画线。 这里我希望第一个点连接到 2、2 到 4、3 到 6、4 到 8 等等......所以这里的模式是每次乘以 2。 所以我最初的想法是我可以使用for循环中的i每次乘以 2。 但是由于我在 ArrayList 中的 i=0 中有我的第一个点,所以我遇到了麻烦。

您试图设置一个不存在的index

您的ArrayList的大小为 0,但您正在尝试访问不存在的索引1

您可以使用ArrayList.add()将另一个元素添加到您的ArrayList 如果您使用set() index必须实际存在于ArrayList

要在索引0处插入元素,请使用points.add(null); 循环前。

所以最终的代码看起来像:

ArrayList<Point> points = new ArrayList<>();
int counter = 1;
int nPoints = 12;
points.add(null); // <= this line was added
while (counter <= nPoints) {
    x = (int) (centerX + r * Math.cos(start));
    y = (int) (centerY + r * Math.sin(start));

    //points.set(counter, (new Point(x, y))); // <= it's not correct use
    points.add(new Point(x,y));

    counter++;
}

使用add而不是set 您不能设置(替换)不存在的项目。 从在文档set

用指定的元素替换此列表中指定位置的元素。

您可以使用index + 1进行计算,但列表索引应从 0 开始。

编辑:

//draw lines
for (int  i= 0; i < points.size() - 1;i ++){
    g2d.drawLine(points.get(i).x,
    points.get(i).y, points.get(i+1).x,
    points.get(i+1).y);
}

如果您尝试设置超出范围的值,Java 中的ArrayList不会扩展到所需的长度。 当数组的长度为 0 时,您正试图在索引 1 处设置 item。 尝试这个:

ArrayList<Point> points = new ArrayList<>();
points.add(null);
for (int counter = 0; counter < nPoints; counter++) {
    x = (int) (centerX + r * Math.cos(start));
    y = (int) (centerY + r * Math.sin(start));

    points.add(new Point(x,y));
}
points.set(0, points.get(1));

编辑:您正在生成相同的Point nPoints 次。 用角度做一些事情,例如:

x = (int) (centerX + r * Math.cos(start + counter * 0.01));
y = (int) (centerY + r * Math.sin(start + counter * 0.01));

编辑:用第一个元素替换第 0 个元素

编辑:既然您提供了绘制代码,我发现您的算法不一定使用基于 1 的索引。 顺便说一句,我非常怀疑您是否从列表中获得了零点。 我怀疑即使您的列表也没有保存,并且列表上出现空指针异常,而不是重点。 无论如何,试试这个:

ArrayList<Point> points = new ArrayList<>();
for (int counter = 0; counter < nPoints; counter++) {
    x = (int) (centerX + r * Math.cos(start));
    y = (int) (centerY + r * Math.sin(start));

    points.add(new Point(x,y));
}

// ...

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    a = getWidth() /2;
    b = getHeight() /2;
    g2d.setColor(Color.RED);

    //draw cirle
    g2d.drawOval(a-r, b-r, 2*r, 2*r);

    //draw lines
    for (int  i= 1; i < points.size();i ++) {
        g2d.drawLine(points.get(i - 1).x, points.get(i - 1).y, points.get(i).x, points.get(i).y);
    }

    g2d.dispose();
}

就我而言,这有帮助:

if (!listName.isEmpty()) {
            return listName.get(0);
        }

希望它对你有用☺️

暂无
暂无

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

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