What does it mean by java.lang.ArrayIndexOutOfBoundsException : 0

后端 未结 2 1350
刺人心
刺人心 2021-01-28 01:37

my compiler keep pointing at this line:

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

and this :

arr[i] = new specia         


        
2条回答
  •  星月不相逢
    2021-01-28 02:00

    From your code:

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

    Therefore, you've created an array with length 0. Since arr[0] attempts to access the first element, but a zero-length array has zero elements, you get that exception. You will have to either allocate the array with an appropriate non-zero size, or use a dynamic container such as an ArrayList.

提交回复
热议问题