Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组_Java_Arrays_Indexoutofboundsexception - Fatal编程技术网

Java数组

Java数组,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,分配要求用户输入3个半径和3个高度条目,我将它们收集在一个数组中,然后确定每个条目的体积。我被困在阵列上了。出于某种原因,我得到了一个数组索引OutofBoundsException Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 (CylinderTest.java:19) 我在最后一次(第六次,或第三次输入的高度)得到错误。我不明白我做错了什么。我很难理解逻辑,这是我最大的问题 这是气缸测试(主)

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

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
(CylinderTest.java:19)
我在最后一次(第六次,或第三次输入的高度)得到错误。我不明白我做错了什么。我很难理解逻辑,这是我最大的问题

这是气缸测试(主)


您的阵列大小为3,由以下行决定:

Cylinder[] volume = new Cylinder[3];
然后在此处从0循环到5:

for (int counter = 0; counter < 6; counter++)
由于数组的长度为3,因此它只有索引0、1和2。但您尝试访问高于两个的索引

作为旁注,我建议您将语句更改为
volume[counter]=新圆柱体(半径、高度)
否则,在每次迭代中,您将增加for循环变量
计数器
两次

在数组中循环索引时的一个良好做法是在以下条件下使用数组的长度:

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

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

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

Cylinder[] volume = new Cylinder[3];
然后在此处从0循环到5:

for (int counter = 0; counter < 6; counter++)
由于数组的长度为3,因此它只有索引0、1和2。但您尝试访问高于两个的索引

作为旁注,我建议您将语句更改为
volume[counter]=新圆柱体(半径、高度)
否则,在每次迭代中,您将增加for循环变量
计数器
两次

在数组中循环索引时的一个良好做法是在以下条件下使用数组的长度:

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

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

您正在声明一个3大小的圆柱体数组,并尝试读取其中的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...
        }
圆柱体[]体积=新圆柱体[3];//3尺寸阵列
for(int counter=0;counter<6;counter++)//循环6次
{
double radius=double.parseDouble(JOptionPane
.showInputDialog(“输入半径”);
double height=double.parseDouble(JOptionPane
.showInputDialog(“输入高度”);
体积[计数器++]=新圆柱体(半径、高度);//读取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);
        }
圆柱体[]体积=新圆柱体[3];//3尺寸阵列
for(int counter=0;counter

请注意volume.length而不是6,并且在volume[counter++]中删除了+//p>您正在声明一个3大小的圆柱体数组,并尝试读取其中的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...
        }
圆柱体[]体积=新圆柱体[3];//3尺寸阵列
for(int counter=0;counter<6;counter++)//循环6次
{
double radius=double.parseDouble(JOptionPane
.showInputDialog(“输入半径”);
double height=double.parseDouble(JOptionPane
.showInputDialog(“输入高度”);
体积[计数器++]=新圆柱体(半径、高度);//读取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);
        }
圆柱体[]体积=新圆柱体[3];//3尺寸阵列
for(int counter=0;counter

注意volume.length而不是6,并且在volume[counter++]中删除了+//p>首先,您的数组已声明为大小为3。但是,在for循环中,您至少可以访问数组的6个元素。因此,您必须将数组的大小至少增加到6。您应该更改代码:

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


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

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


您的
圆柱体
数组只有3个元素<代码>气缸[]容积=新气缸[3]


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

您的
圆柱体
数组只有3个元素<代码>气缸[]容积=新气缸[3]


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

您确定吗

    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?
    }
气缸[]容积=新气缸[3];
用于(int计数器=0;计数器<6;计数器++)
{
double radius=double.parseDouble(JOptionPane
.showInputDialog(“输入半径”);
double height=double.parseDouble(JOptionPane
.showInputDialog(“输入高度”);
体积[计数器++]=新圆柱体(半径、高度);
//计数器的计数将最多为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?
    }
气缸[]容积=新气缸[3];
用于(int计数器=0;计数器<6;计数器++)
{
双半径=double.parseDouble(JO