【问题标题】:Homework Java Array作业 Java 数组
【发布时间】:2013-10-04 07:50:14
【问题描述】:

该任务要求用户输入 3 个半径和 3 个高度条目,我将它们收集在一个数组中,然后确定每个条目的体积。我被困在阵列上。出于某种原因,我收到了ArrayIndexOutOfBoundsException

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
(CylinderTest.java:19)

我在最后(第 6 个或第三个条目的高度)收到错误。我不明白我做错了什么。我很难理解逻辑,这是我最大的问题。

这里是 CylinderTest(主要)

import javax.swing.*;

//Driver class
public class CylinderTest
{

    public static void main(String[] args)
    {

        Cylinder[] volume = new Cylinder[3];

        for (int counter = 0; counter < 6; counter++)
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter++] = new Cylinder(radius, height);
        }

        String display = "Radius\tHeight\n";
        for (Cylinder i : volume)
        {
            if (i != null)
                display += i.toString() + "\n";
        }
        JOptionPane.showMessageDialog(null, display);
    }
}

这里是 Cylinder 类

public class Cylinder
{
    // variables
    public static final double PI = 3.14159;
    private double radius, height, volume;

    // constructor
    public Cylinder(double radius, double height)
    {
        this.radius = radius;
        this.height = height;
    }

    // default constructor
    public Cylinder()
    {this(0, 0);}

    // accessors and mutators (getters and setters)
    public double getRadius()
    {return radius;}

    public void setRadius(double radius)
    {this.radius = radius;}

    public double getHeight()
    {return height;}

    public void setHeight(double height)
    {this.height = height;}

    public double getVolume()
    {return volume;}

    public void setVolume(double volume)
    {this.volume = volume;}

    // Volume method to compute the volume of the cylinder
    public double volume()
    {return PI * radius * radius * height;}

    public String toString()
    {return volume + "\t" + radius + "\t" + height; }

}

【问题讨论】:

  • Firstable,如果你这样做:volume[counter++] 然后你将移动计数器两次,一次在 volume[counter++] 中,另一次在 for 语句中; counter++)
  • 为了将来的调试参考,CylinderTest.java:19 将您指向发生错误的类 (CylinderTest.java) 和该类中的行号 (19)。 #19 到底是哪一行?
  • 这是一个很好的家庭作业问题尝试。我认为正是 nhgrif 所建议的缺失了。在这种情况下,CylinderTest 类是无关紧要的,因为问题与数组有关。我认为它不值得投反对票。
  • 第 19 行是体积[counter++] = new Cylinder(radius, height);
  • 另外,据我所知,在 update 语句之外的任何地方混用 for loop 的迭代器是非常糟糕的做法。

标签: java arrays indexoutofboundsexception


【解决方案1】:

您的 Cylinder 数组只有 3 个元素。 Cylinder[] volume = new Cylinder[3];

您的for loop 正在尝试访问此数组中元素 2 之后的元素。这些元素不存在。

【讨论】:

    【解决方案2】:

    您的数组大小为 3,由以下行确定:

    Cylinder[] volume = new Cylinder[3];
    

    然后你在这里从 0 循环到 5:

    for (int counter = 0; counter < 6; counter++)
    

    然后您尝试在此处访问这些索引之一:

    volume[counter++] = new Cylinder(radius, height);
    

    由于数组的长度为 3,它只有索引 0、1 和 2。但您尝试访问大于 2 的索引。

    作为旁注,我建议您将语句更改为 volume[counter] = new Cylinder(radius, height); 否则您将在每次迭代中将 for 循环变量 counter 增加两次。

    循环遍历数组中的索引的一个好习惯是在条件中使用数组的长度:

    for (int counter = 0; counter < volume.length; counter++)
    

    这将确保它只遍历数组中存在的索引,无论它有多大或多小。

    【讨论】:

    • 谢谢。我现在有一个 6 个数组和一个 3 个 for 循环。它似乎正在工作。而且我认为逻辑上在我脑海中闪过一个小灯泡。谢谢!气缸 [] 体积 = 新气缸 [6]; for (int counter = 0; counter
    • @user2802785 不要忘记按照我的建议更改您的 for 循环条件。 for (int counter = 0; counter &lt; volume.length; counter++)
    • 这是我现在拥有的:Cylinder[] volume = new Cylinder[3]; for (int counter = 0; counter
    • 如何在 cmets 中表示代码?我在问题中按 CTRL+K,但在这里不起作用。
    • 谢谢。我还有另一个用户协议问题。如果我对同一代码有另一个问题,但与最初的问题无关,我应该开始另一个话题吗?
    【解决方案3】:

    您正在声明一个大小为 3 的 Cylinder 数组,并且您正在尝试读取其中的 6 个。

    Cylinder[] volume = new Cylinder[3]; // 3 size array
    
            for (int counter = 0; counter < 6; counter++) // loop 6 times
            {
                double radius = Double.parseDouble(JOptionPane
                        .showInputDialog("Enter the radius"));
                double height = Double.parseDouble(JOptionPane
                        .showInputDialog("Enter the height"));
                volume[counter++] = new Cylinder(radius, height); // read 0, 1, 2, 3, 4, 5...
            }
    

    应该是:

    Cylinder[] volume = new Cylinder[3]; // 3 size array
    
            for (int counter = 0; counter < volume.length; counter++) // loop 6 times
            {
                double radius = Double.parseDouble(JOptionPane
                        .showInputDialog("Enter the radius"));
                double height = Double.parseDouble(JOptionPane
                        .showInputDialog("Enter the height"));
                volume[counter] = new Cylinder(radius, height);
            }
    

    注意volume.length而不是6,并且在volume[counter++]中删除了++

    【讨论】:

    • 您的 // read 0, 1, 2, 3, 4, 5.. 实际上是 a) 写入/设置,而不是读取。乙)。索引 0、2、4,因为计数器变量在每次迭代中增加了两次。
    • @SimonAndréForsberg 是的,我的意思是“尝试”
    • 谢谢。我已经尝试了每个人的建议,并且都在工作。使用 volume.length 是一个很好的技巧!
    【解决方案4】:

    首先,您的数组已被声明为大小为 3。但是,在您的 for 循环中,您至少可以访问数组的 6 个元素。因此,您将数组的大小增加到至少 6。您应该更改代码:

    volume[counter++] = new Cylinder(radius, height);
    

    volume[counter] = new Cylinder(radius, height);
    

    【讨论】:

    • 谢谢。那行得通。我在想这是一个 3 项数组,每个项都有两个属性:/
    【解决方案5】:

    你确定吗?

        Cylinder[] volume = new Cylinder[3]; 
    
        for (int counter = 0; counter < 6; counter++)
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter++] = new Cylinder(radius, height); 
            //counter will count up to 5 (Array out of Bounds exception for sure..)
            //also: why are you incrementing counter by yourself?
        }
    

    【讨论】:

    • 这应该作为评论发布,而不是答案。
    • 实际上这不会导致数组越界,假设我们有大小为计数器的数组,因为计数器会在返回最后一个计数器值后增加,所以如果数组中有 6 个元素,它会调用位置 5 的数组,将计数器移动到 6 并退出循环。
    • @porfiriopartida 在这个所谓的答案中的代码中,volume.length 仍然是 3。
    • @Simon André Forsberg:好的。我是新来的,我现在不能发布 cmets。我也不知道评论应该是这样的。对此感到抱歉:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2013-02-28
    • 2012-06-03
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多