java 索引越界错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2534413/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:38:51  来源:igfitidea点击:

Index out of bounds error

javaexceptiongwtarraylist

提问by suprasad

I am working on a program where i am recreating the saved widgets back on to the boundary panel. When i am creating them i am also trying to put the values into the ArrayList so that if i want to update and save the opened project i should be able to do so by getting the values from the ArrayList.

我正在开发一个程序,我正在将保存的小部件重新创建回边界面板。当我创建它们时,我也试图将值放入 ArrayList 以便如果我想更新和保存打开的项目,我应该能够通过从 ArrayList 获取值来做到这一点。

Here is how the code looks like:

下面是代码的样子:

for(int i = 0; i < result.length; i++){
                if(ename.contains(result[i].getParticipateEntityName())){
                        ername.add(ename.indexOf(result[i].getParticipateEntityName()), result[i].getParticipateRelatioshipName());
                        etotalpartial.add(ename.indexOf(result[i].getParticipateEntityName()), result[i].getTotalPartial());
                }else if(wename.contains(result[i].getParticipateEntityName())){
                        wrname.add(wename.indexOf(result[i].getParticipateEntityName()), result[i].getParticipateRelatioshipName());
                }
}

Here ename, ername, etotalpartial, wename and wrname are all ArrayList. This piece of code is included in an asynchronous class method.

这里ename、ername、etotalpartial、wename和wrname都是ArrayList。这段代码包含在一个异步类方法中。

When i run the code i get error at "ername.add(ename......".

当我运行代码时,我在“ername.add(ename......”处出现错误。

Here is the error stack:

这是错误堆栈:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.add(ArrayList.java:367)
    at com.e.r.d.client.ERD1.onSuccess(ERD1.java:898)
    at com.e.r.d.client.ERD1.onSuccess(ERD1.java:1)
    at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:216)
    at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
    at com.google.gwt.http.client.RequestBuilder.onReadyStateChange(RequestBuilder.java:393)
    at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1713)
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
    at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
    at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
    at java.lang.Thread.run(Thread.java:619)

I am not sure what i am doing wrong.

我不确定我做错了什么。

Any input will be of great help.

任何输入都会有很大帮助。

Thank you.

谢谢你。

采纳答案by karlipoppins

Well it looks like ername or wrname is an empty array list. ername.size() will probably return 0 by the way...

那么看起来 ername 或 wrname 是一个空数组列表。ername.size() 可能会返回 0 顺便说一句...

You either want to use the add(Object object) method, or you can initialize a capacity by inserting the following code:

您要么想使用 add(Object object) 方法,要么可以通过插入以下代码来初始化容量:

for (int i = 0; i < CAPACITY; i++){
   ername.add(null);
}

and then your code should work...

然后你的代码应该可以工作......

回答by Jo?o Silva

You can't use the public void add(int index, E element)method of Listif you can not guarantee that the size of your list is greater than index.

如果不能保证列表的大小大于public void add(int index, E element)List则不能使用 的方法index

That is what happening with your code. You have an empty list (ername), and ename.indexOf(result[i].getParticipateEntityName())is returning 1, thus the IndexOutOfBoundsException. In other words, you have a list of size 0, and you are trying to insert an element at index 1.

这就是您的代码发生的情况。您有一个空列表 ( ername),并且ename.indexOf(result[i].getParticipateEntityName())返回 1,因此IndexOutOfBoundsException. 换句话说,您有一个大小为 0 的列表,并且您正试图在索引 1 处插入一个元素。

Consider using the public boolean add(E e)method of ArrayListwhich appends the new element to the end of the list.

考虑使用public boolean add(E e)的方法ArrayList,其附加到列表的末尾的新元素。