ArrayList.add抛出ArrayIndexOutOfBoundsException

编程入门 行业动态 更新时间:2024-10-18 01:42:40
本文介绍了ArrayList.add抛出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.uimon.web.bean.inbox.InboxSearchBean.populateInboxResultHolder(InboxSearchBean.java:388) [3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at com.ml.fusion.uimon.web.bean.inbox.InboxSearchBean.searchInboxErrors(InboxSearchBean.java:197) [3/7/12 15:41:26:721 UTC] 00000045 SystemErr R at com.ml.fusion.uimon.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

本文发布于:2023-11-06 23:47:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   ArrayList   add   ArrayIndexOutOfBoundsException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!