簡體   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