简体   繁体   中英

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

So up until this point I've just used points.add(new Point(x,y)) to add points to my ArrayList . However, I found out that I need the first point to be in index=1 in order to make it possible to multiply the number for each step. So I tried setting the counter from 0 to 1 , and as expected I knew I would get a error because of the range, but I've tried changing up the condition within the while-loop, but nothing seems to work.

Here's my code:

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++;
}

This is the error that I am getting:

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)

I've deleted much of the code that isn't relevant for this problem in order to make it easier to read.

Edit:

 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();
        }

I've created a circle in which I'm trying to draw lines between points along the circumference. Here I want the 1st point to connect to 2, 2 to 4, 3 to 6, 4 to 8 and so on... So the pattern here is is that it is multiplied by 2 for each time. So my initial thought was that I could use the i inside the for -loop to multiply by 2 each time. But since I have my first point in i=0 inside the ArrayList, I am having trouble.

Your trying to set an index that doesn't exist.

The size of your ArrayList is 0 but you are trying to access index 1 which doesn't exist.

You can use ArrayList.add() to add another element to your ArrayList . If you use set() the index must actually exist in the ArrayList .

To insert an element at index 0 use points.add(null); before loop.

So the final code looks like:

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++;
}

Use add instead of set . You can not set (replace) an item that does not exist. From the docs on set :

Replaces the element at the specified position in this list with the specified element.

You can use index + 1 for your calulations, but list indices should start at 0.

Edit:

//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);
}

ArrayList s in Java do not extend to the desired length if you try to set a value outside of it's range. You are trying to set item at index 1, when the array has 0 length. Try this:

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));

EDIT: you are generating the same Point nPoints times. Do something with the angle, for example:

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

EDIT: replacing the 0th element with the 1st

EDIT: now that you provided the paint code, I see that your algorithm does not necessarily use 1-based indices. By the way I highly doubt that you got a null point out of the list. I suspect that even your list is not saved, and got null pointer exception on the list, not on the point. Anyway, try this:

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();
}

In my case that helped:

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

Hope it works by you ☺️

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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