我的类得到错误的Java数组

And*_*tto -2 java arrays class object

我为我的对象创建了一个类:

    public class circles {

    int coor_x;
    int coor_y;

    int ray;


    public circles(int coor_x, int coor_y, int ray) {
    this.coor_x=coor_x;
    this.coor_y = coor_y;
    this.ray =ray ;

    }



  public int getCoor_x() {
        return coor_x;
    }

    public int getCoor_y() {
        return coor_y;
    }

      public int getRay() {
        return ray;
    }



    public void setCoor_x(int coor_x){
    this.coor_x=coor_x;
    }

     public void setCoor_y(int coor_y){
    this.coor_y=coor_y;
    }

      public void setRay(int ray){
    this.ray=ray;
    }


}
Run Code Online (Sandbox Code Playgroud)

但是当我不想制作一个数组并用for填充它时,使用以下代码:

 int coor_x=2;
    int coor_y=2;
    int ray=2;
    int i = 0;    

    circles circles_test[]=new circles[10];


    for(i=0; i<=circles.length;){


        circles_test[i]=new circles(coor_x, coor_y, ray+i); //line 30
        System.out.println("Ray of circles: "+circles_test[i].getRay());
    i++;
    }
Run Code Online (Sandbox Code Playgroud)

它有效,但有错误:线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:10在circlesObjectCreator.main(circlesObjectCreator.java:30)Java结果:1

我做错了什么?这是更好的原因吗?请帮助,谢谢.

Dav*_*ton 5

您正在访问一个包含10个元素的数组,索引为0-9,索引从0运行到数组长度10,因为i <= circles.length.你想用i < circles.length.