开发者

Java array with my class getting error

I made one class for my 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;
    }


}

But when I wont to make an array of it and fill it with a for, with this code:

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

it开发者_StackOverflow works but with errors: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at circlesObjectCreator.main(circlesObjectCreator.java:30) Java Result: 1

What I am doing wrong? Is a better why to it? Please help and thank you.


You're accessing an array with 10 elements, indexed 0-9, with an index running from 0 to the array length, 10, because of the i <= circles.length. You want to use i < circles.length.


Your for loop checks the value of circles.length, whatever that is, but your array is called circles_test.Additionally you should check with the less-than (<) comparison, because arrays are zero-based. (Length is not the highest index available in the array, it is the number of elements in the array.)


There are several errors in your code.

First, a better name for the circles class would be Circle. Java classes are typically capitalized and singular, not plural.

Next, your for loop is going too far. Your array has 10 elements, but arrays in Java are zero-indexed. This means that the first element in circles_test is circles_test[0], the second is circles_test[1], and so on. But circles_test[10] doesn't exist, because that would be the 11th element in your array of size 10. This causes the ArrayIndexOutOfBoundsException, because you are trying to use the index 10, which is too big. This is happening because you wrote this in your for loop:

i <= circles_test.length

This means that i will go all the way up to and including circles_test.length. But we don't want it to go to 10, because that index is out of bounds, so remove the = sign.

Next, a better way to write your for loop is to include the increment statement in the loop like so:

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

}

For loops work like this:

for(first_statement; second_statement; third_statement)

first_statement will happen once at the beginning of the loop. second_statement will be checked once each time at the beginning of one repetition of the loop, and if it is false, the loop will end. third_statement will happen each time at the end of the loop.

If you have any more questions feel, free to ask.


You made an array of 10 elements, 0-9. But your loop tries to access elements 0-10.

Use i<circles.length so you don't try to access the nonexistent element 10.


The lenght of the Java array is exclusive, that means:

length 0 = No objects

length 1 = one object in the position [0] of the array

length 2 = two objects, in the positions [0][1]

So you need to make your loop condition exclusive i<circles.length

Also, as a plus, the for block can initialize variables and increment them:

for (int i = 0; i < circles.length; i++)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜