java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 长度数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22806781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4 length array
提问by Carlos Manzo
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.ArrayIndexOutOfBoundsException: length=4; index=4
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.fragment.SegmentoAnterioreFragment.onTaskComplete(SegmentoAnterioreFragment.java:471)
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.asynctask.LookupTask.onPostExecute(LookupTask.java:279)
04-02 11:03:57.922: E/AndroidRuntime(18952): at it.nad.cartellecliniche.asynctask.LookupTask.onPostExecute(LookupTask.java:1)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask.access0(AsyncTask.java:177)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.os.Looper.loop(Looper.java:137)
04-02 11:03:57.922: E/AndroidRuntime(18952): at android.app.ActivityThread.main(ActivityThread.java:5414)
04-02 11:03:57.922: E/AndroidRuntime(18952): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:03:57.922: E/AndroidRuntime(18952): at java.lang.reflect.Method.invoke(Method.java:525)
04-02 11:03:57.922: E/AndroidRuntime(18952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-02 11:03:57.922: E/AndroidRuntime(18952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-02 11:03:57.922: E/AndroidRuntime(18952): at dalvik.system.NativeStart.main(Native Method)
this is the code:
这是代码:
else if (typeTask.equals("LookupSegmentoAnteriore")) {
switch (resultOperation) {
case 200: {
Object[] lista = (Object[]) output[2];
listaBulbo = (String[])lista[0];
Arrays.sort(listaBulbo);
listaApparatoLacrimale = (String[])lista[1];
Arrays.sort(listaApparatoLacrimale);
listaPalpebre = (String[])lista[2];
Arrays.sort(listaPalpebre);
listaCongiuntiva = (String[])lista[3];
Arrays.sort(listaCongiuntiva);
listaCornea = (String[])lista[4];
Arrays.sort(listaCornea);
listaAngolo = (String[])lista[5];
Arrays.sort(listaAngolo);
listaCamera_anteriore = (String[])lista[6];
Arrays.sort(listaCamera_anteriore);
listaIride = (String[])lista[7];
Arrays.sort(listaIride);
listaPupilla = (String[])lista[8];
Arrays.sort(listaPupilla);
listaCristallino = (String[])lista[9];
Arrays.sort(listaCristallino);
break;
}
}
}
i have added new elements to the list starting
我已将新元素添加到列表中
from:
从:
listaCornea = (String[])lista[4];
Arrays.sort(listaCornea);
to:
到:
listaCristallino = (String[])lista[9];
Arrays.sort(listaCristallino);
somebody can help me to resolve this problem? i don't know how to manage the array length problem. I just added the new elements to the list, but i don't know if this is the right form to do it. Do i need to resize my array? how i can do that?
有人可以帮我解决这个问题吗?我不知道如何管理数组长度问题。我刚刚将新元素添加到列表中,但我不知道这是否是正确的形式。我需要调整数组的大小吗?我该怎么做?
thanks a lot
多谢
采纳答案by Dehan Wjiesekara
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,
问题是您的数组可能声明只有 4 个房间,这意味着您可以访问最大索引为 3,但在上面的代码中,您将访问第 4 个索引,然后它会抱怨您的索引超出,您的长度为 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 而不是数组,因为 Array 不能在声明后调整大小,但您可以向 arraylist 添加任意数量的对象。
ArrayList<Object> lista=new ArrayList<Object>();
回答by Jitendra ramoliya
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];
您已指定。
Object[] lista = (Object[]) output[2];
That's means you have two element. first is lista[0]
and second is lista[1]
.
这意味着你有两个元素。第一个是lista[0]
,第二个是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)
现在您尝试访问lista[2]
. 这意味着您尝试访问实际上在您的Objeact[] lista
. 所以它给出了异常SegmentoAnterioreFragment.onTaskComplete(SegmentoAnterioreFragment.java:471)
Arraylist is dynamic arraylist. you can add and delete element directly. you not need to specify element limit.
Arraylist 是动态数组列表。您可以直接添加和删除元素。您不需要指定元素限制。
you can use this arraylist instead of Object Array
您可以使用此数组列表而不是对象数组
ArrayList<Object> lista=new ArrayList<Object>();