这是什么意思java.lang.ArrayIndexOutOfBoundsException:0 [英] What does it mean by java.lang.ArrayIndexOutOfBoundsException : 0

查看:160
本文介绍了这是什么意思java.lang.ArrayIndexOutOfBoundsException:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编译器保持在该行指出:

 改编[I] =新specialDelivery(姓名,名称2,重量,特殊的);

和这样的:

 改编[I] =新specialDelivery(姓名,名称2,重量,特殊的);

该错误的标题说明

 公共静态无效的主要(字符串ARGS [])
{
    INT大小= 0,距离;
    双体重= 0.0;
    串strinput,法,名,名称2,特殊的;
    包裹ARR [] =新地块【尺寸】;    strinput = JOptionPane.showInputDialog(输入包裹的数量:);
    大小=的Integer.parseInt(strinput);    的for(int i = 0; I<大小;我++)
    {
        诠释J = 0,K = 0;        方法= JOptionPane.showInputDialog(交付方法(正常/特):);        如果(method.equals(正常))
        {
            名称= JOptionPane.showInputDialog(输入您的姓名:);
            名称2 = JOptionPane.showInputDialog(输入接收者的名字:);
            strinput = JOptionPane.showInputDialog(输入包裹的重量+ J +:);
            体重= Double.parseDouble(strinput);            strinput = JOptionPane.showInputDialog(输入输送的距离+ J +(公里):);
            距离=的Integer.parseInt(strinput);            J ++;
            改编[I] =新normalDelivery(姓名,名称2,重量,距离);
        }        如果(method.equals(特))
        {
           名称= JOptionPane.showInputDialog(输入您的姓名:);
           名称2 = JOptionPane.showInputDialog(输入接收者的名字:);
           特殊= JOptionPane.showInputDialog(输入型交付(飞机/船):);
           strinput = JOptionPane.showInputDialog(输入包裹的重量+ J +:);
           体重= Double.parseDouble(strinput);           J ++;
           改编[I] =新specialDelivery(姓名,名称2,重量,特殊的);
        }
    }
}
}


解决方案

您已经声明大小的数组 0 ,因为这是尺寸<创建阵列时/ code>了。所以,你不能分配任何此数组。最重要的是,数组的大小是固定为0,所以你不能做任何事情来改变它的大小。

创建数组分配了号码尺寸之后,所以它从一开始就适当的大小:

  strinput = JOptionPane.showInputDialog(输入包裹的数量:);
大小=的Integer.parseInt(strinput);包裹ARR [] =新地块【尺寸】; //此处向下移动这条线

my compiler keep pointing at this line:

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

and this :

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

the error is stated in the title

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

解决方案

You have declared an array of size 0, because that's what size was when the array was created. So you can't assign anything to this array. On top of that, the array's size is fixed at 0, so you can't do anything to change the size of it.

Create your array after you have assigned a number to size, so it has the proper size from the start:

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

Parcel arr[] = new Parcel[size];  // move this line down here

这篇关于这是什么意思java.lang.ArrayIndexOutOfBoundsException:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆