ArrayList.add抛出ArrayIndexOutOfBoundsException [英] ArrayList.add throws ArrayIndexOutOfBoundsException
问题描述
我正在尝试将一个对象添加到ArrayList并抛出ArrayIndexOutOfBoundsException
以下是代码
I am trying to add a object to a ArrayList and its throwing ArrayIndexOutOfBoundsException Following is the code
private void populateInboxResultHolder(List inboxErrors){
inboxList = new ArrayList();
try{
inboxHolder = new InboxResultHolder();
//Lots of Code
inboxList.add(inboxHolder);
}catch(Exception e){
e.printStackTrace();
}
}
例外是
[3/7/12 15:41:26:715 UTC] 00000045 SystemErr R java.lang.ArrayIndexOutOfBoundsException
[3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at java.util.ArrayList.add(ArrayList.java:378)
[3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at com.ml.fusion.ui.common.web.bean.inbox.InboxSearchBean.populateInboxResultHolder(InboxSearchBean.java:388)
[3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at com.ml.fusion.ui.common.web.bean.inbox.InboxSearchBean.searchInboxErrors(InboxSearchBean.java:197)
[3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at com.ml.fusion.ui.common.web.bean.inbox.InboxSearchBean.viewInbox(InboxSearchBean.java:207)
但是根据ArrayList.add的签名,它不应该抛出这个异常。
请帮忙。
But according to the signature of ArrayList.add it should not throw this exception. Please help.
推荐答案
ArrayList.add()
永远不要抛出 ArrayIndexOutOfBoundsException
如果正确使用,那么你似乎正在以一种方式使用你的 ArrayList
它不支持。
ArrayList.add()
should never throw an ArrayIndexOutOfBoundsException
if used "properly" so it seems that you're using your ArrayList
in a way which it does not support.
很难从你发布的代码中判断出来但我的猜测是你正在访问你的 ArrayList
来自多个线程。
It's hard to tell from just the code you've posted but my guess is that you're accessing your ArrayList
from multiple threads.
ArrayList
未同步,因此不是线程安全的。如果这是问题,您可以使用列表来修复它。 java / util / Collections.html#synchronizedList%28java.util.List%29> Collections.synchronizedList()
。
ArrayList
isn't synchronised and so isn't thread safe. If this is the problem you can fix it by wrapping your List
using Collections.synchronizedList()
.
这篇关于ArrayList.add抛出ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!