首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >java.lang.ArrayIndexOutOfBoundsException :0是什么意思

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

Stack Overflow用户
提问于 2013-08-13 08:21:39
回答 2查看 2.5K关注 0票数 0

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

代码语言:javascript
复制
arr[i] = new specialDelivery(name,name2,weight,special);

还有这个:

代码语言:javascript
复制
arr[i] = new specialDelivery(name,name2,weight,special);

错误在标题中说明

代码语言:javascript
复制
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);
        }
    }
}    
}
EN

Stack Overflow用户

发布于 2013-08-13 08:23:34

您已经声明了一个大小为0的数组,因为在创建数组时size就是这个大小。所以你不能给这个数组赋值。最重要的是,数组的大小固定为0,因此您无法更改它的大小。

在为size分配了一个数字之后,创建您的数组,以便它从一开始就具有适当的大小:

代码语言:javascript
复制
strinput = JOptionPane.showInputDialog ( " Enter number of parcel : " );
size = Integer.parseInt(strinput);

Parcel arr[] = new Parcel[size];  // move this line down here
票数 9
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18198767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档