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.lang.ArrayIndexOutOfBoundsException是什么意思:0_Java_Arrays - Fatal编程技术网

java.lang.ArrayIndexOutOfBoundsException是什么意思:0

java.lang.ArrayIndexOutOfBoundsException是什么意思:0,java,arrays,Java,Arrays,我的编译器一直指向这一行: arr[i] = new specialDelivery(name,name2,weight,special); 这是: arr[i] = new specialDelivery(name,name2,weight,special); 标题中说明了错误 public static void main ( String args [] ) { int size = 0,distance; double weight = 0.0; Str

我的编译器一直指向这一行:

arr[i] = new specialDelivery(name,name2,weight,special);
这是:

arr[i] = new specialDelivery(name,name2,weight,special);
标题中说明了错误

public static void main ( String args [] )
{   
    int size = 0,distance;
    double weight = 0.0;
    String strinput,method,name,name2,special;
    Parcel arr[] = new Parcel[size];

    strinput = JOptionPane.showInputDialog ( " Enter number of parcel : " );
    size = Integer.parseInt(strinput);

    for (int i = 0; i<size; i++)
    {   
        int j = 0, k = 0;

        method = JOptionPane.showInputDialog ( "Method of delivery (normal/special):  " );  

        if (method.equals("normal"))
        {
            name = JOptionPane.showInputDialog ( " Enter your name : " );
            name2 = JOptionPane.showInputDialog ( " Enter name of receiver : " );
            strinput = JOptionPane.showInputDialog(" Enter the weight of parcel " + j + " : " );  
            weight = Double.parseDouble(strinput);

            strinput = JOptionPane.showInputDialog(" Enter the distance of delivery " + j + " (km) : " );  
            distance = Integer.parseInt(strinput);

            j++;
            arr[i] = new normalDelivery(name,name2,weight,distance); 
        }     

        if (method.equals("special"))
        {
           name = JOptionPane.showInputDialog ( " Enter your name : " );
           name2 = JOptionPane.showInputDialog ( " Enter name of receiver : " ); 
           special = JOptionPane.showInputDialog(" Enter the type of delivery(airplane/ship) :" );
           strinput = JOptionPane.showInputDialog(" Enter the weight of parcel " + j + " : " ); 
           weight = Double.parseDouble(strinput);

           j++;
           arr[i] = new specialDelivery(name,name2,weight,special);
        }
    }
}    
}
publicstaticvoidmain(字符串参数[])
{   
int size=0,距离;
双倍重量=0.0;
字符串strinput、方法、名称、名称2、特殊;
包裹arr[]=新包裹[尺寸];
strinput=JOptionPane.showInputDialog(“输入地块编号:”);
size=Integer.parseInt(strinput);

对于(int i=0;i您已经声明了一个大小为
0
的数组,因为创建数组时
size
就是这个数组。因此您不能为这个数组分配任何内容。除此之外,数组的大小固定为0,因此您无法更改它的大小

将数字指定给
大小后创建数组,使其从一开始就具有正确的大小:

strinput = JOptionPane.showInputDialog ( " Enter number of parcel : " );
size = Integer.parseInt(strinput);

Parcel arr[] = new Parcel[size];  // move this line down here
从您的代码:

int size = 0;
...
Parcel arr[] = new Parcel[size];

因此,您创建了一个长度为0的数组尝试访问第一个元素,但零长度数组有零个元素,则会出现该异常。您必须为数组分配适当的非零大小,或者使用动态容器,如
ArrayList

int size=0,distance;
这是什么?这意味着数组有0个大小。这是唯一的大小不带一个元素的数组(可作为第0个索引访问)。然后,只需找出大小为何为0的问题。。请反向操作。@user2246674:不,元素为零的数组没有任何元素。因此,如果不获取和
ArrayIndexOutOfBoundsException
,包括第零个索引,则无法对数组中的任何元素进行索引。当您看到
java.lang.ArrayIndexOutOfBoundsException时选项:n
,以及索引到的数组,JVM运行时告诉您
n
不是数组的有效索引;特别是它意味着
n<0或n>array.length-1
。在您的情况下,JVM报告
n
0
。这意味着
0
不是ar的有效索引这意味着数组没有任何元素。现在,这是怎么发生的?让我们追溯到数组声明的时间:
Parcel arr[]=new Parcel[size]
好的,那么
大小
呢?
int size=0
就是这样。你需要先计算
大小
。@Kon,这是有效的语法。你可以像那样声明多个相同类型的变量,在这种情况下,
距离
有一个默认值
0
,而
大小
有一个明确声明的值
0
的值。我不确定他为什么只显式声明了其中一个,但这没有错。非常感谢这一点。我花了几个小时才找到解决方案,但我从没想过解决方案会这么简单