【问题标题】:Java array with my class getting errorJava 数组与我的类得到错误
【发布时间】:2011-10-06 21:34:04
【问题描述】:

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

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


}

但是当我不打算将它创建一个数组并用 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++;
    }

它可以工作,但有错误: 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:10 在 circlesObjectCreator.main(circlesObjectCreator.java:30) Java 结果:1

我做错了什么?是一个更好的为什么呢?请帮忙,谢谢。

【问题讨论】:

  • 试试for(i=0; i&lt;circles.length;)

标签: java arrays class object


【解决方案1】:

由于i &lt;= circles.length,您正在访问一个包含 10 个元素、索引为 0-9、索引从 0 到数组长度 10 的数组。你想使用i &lt; circles.length

【讨论】:

  • 感谢大家的快速回复。对我来说真是太可惜了。
【解决方案2】:

您的for 循环检查circles.length 的值,不管它是什么,但您的数组称为circles_test。此外,您应该检查小于(&lt;)比较,因为数组是零-基于。 (长度不是数组中可用的最高索引,它是数组中元素的数量。)

【讨论】:

    【解决方案3】:

    您的代码中有几个错误。

    首先,circles 类的一个更好的名称是 Circle。 Java 类通常是大写和单数,而不是复数。

    接下来,你的 for 循环走得太远了。您的数组有 10 个元素,但 Java 中的数组是零索引的。这意味着 circles_test 中的第一个元素是circles_test[0],第二个是circles_test[1],以此类推。但是circles_test[10] 不存在,因为那将是大小为 10 的数组中的第 11 个元素。这会导致 ArrayIndexOutOfBoundsException,因为您正在尝试使用索引 10,它太大了。这是因为你在 for 循环中写了这个:

    i <= circles_test.length
    

    这意味着i 将一直上升到并包括circles_test.length。但我们不希望它变为 10,因为该索引超出范围,因此删除 = 符号。

    接下来,编写 for 循环的更好方法是在循环中包含增量语句,如下所示:

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

    For 循环是这样工作的:

    for(first_statement; second_statement; third_statement)
    

    first_statement 将在循环开始时发生一次。 second_statement 每次循环开始时都会检查一次,如果为假,则循环结束。 third_statement 每次都会在循环结束时发生。

    如果您还有其他问题,请随时提出。

    【讨论】:

    • 圆的声明纯属文体。这可能会让你不快,但肯定不是错误的。
    • 很公平,但我确实说“更好”。
    • 不,您说“您错误地声明了您的圆圈数组”。 circles c[] = new circles[10] 是完全有效的 Java。
    【解决方案4】:

    您创建了一个包含 0-9 的 10 个元素的数组。但是您的循环尝试访问元素 0-10。

    使用i&lt;circles.length,这样您就不会尝试访问不存在的元素 10。

    【讨论】:

      【解决方案5】:

      Java 数组的长度是独占的,这意味着:

      length 0 = 没有对象

      length 1 = 数组位置 [0] 中的一个对象

      length 2 = 两个对象,在位置 [0][1]

      所以你需要让你的循环条件排他i&lt;circles.length

      另外,for 块可以初始化变量并增加它们:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-13
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-22
        • 2017-06-12
        • 1970-01-01
        相关资源
        最近更新 更多