java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 length array

后端 未结 2 1308
死守一世寂寞
死守一世寂寞 2020-12-11 18:03

i have this error:

04-02 11:03:57.922: E/AndroidRuntime(18952): FATAL EXCEPTION: main
04-02 11:03:57.922: E/AndroidRuntime(18952): java.lang.ArrayIndexOutOfB         


        
相关标签:
2条回答
  • 2020-12-11 18:27

    problem is your array may declared to have only 4 rooms, that means you can access maximum index of 3 but in above code you gonna access 4th index, then it will complain that your index bound in out,your lenth is 4,

    you should use ArrayList instead of array,because Array can't be resize after declaration, but you can add any number of objects to arraylist.

    ArrayList<Object> lista=new ArrayList<Object>();
    
    0 讨论(0)
  • 2020-12-11 18:50

    Array Index out of bound Exception comes when you try to access index which is not available in your Array.

    You have specified. Object[] lista = (Object[]) output[2];

    That's means you have two element. first is lista[0] and second is lista[1].

    Now you tring to access lista[2]. It means you try to access third element which is actually not available in your Objeact[] lista. So It gives Exception at SegmentoAnterioreFragment.onTaskComplete(SegmentoAnterioreFragment.java:471)

    Arraylist is dynamic arraylist. you can add and delete element directly. you not need to specify element limit.

    you can use this arraylist instead of Object Array

    ArrayList<Object> lista=new ArrayList<Object>();
    
    0 讨论(0)
提交回复
热议问题