Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个循环中的java.lang.IndexOutOfBoundsException 公共字符串代码生成(){ ArrayList dispatchTable=新的ArrayList(); 如果(superEntry!=null){ ArrayList superDispatchTable=getDispatchTable(superEntry.getOffset()); 对于(int i=0;i_Java_Indexoutofboundsexception - Fatal编程技术网

每个循环中的java.lang.IndexOutOfBoundsException 公共字符串代码生成(){ ArrayList dispatchTable=新的ArrayList(); 如果(superEntry!=null){ ArrayList superDispatchTable=getDispatchTable(superEntry.getOffset()); 对于(int i=0;i

每个循环中的java.lang.IndexOutOfBoundsException 公共字符串代码生成(){ ArrayList dispatchTable=新的ArrayList(); 如果(superEntry!=null){ ArrayList superDispatchTable=getDispatchTable(superEntry.getOffset()); 对于(int i=0;i,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我得到以下例外情况: 线程“main”java.lang.IndexOutOfBoundsException中的异常:索引: 1,大小:0位于java.util.ArrayList.rangeCheckForAdd(未知源) 位于java.util.ArrayList.add(未知源) 导致错误的行是:dispatchTable.add(mnode.getOffset(),mnode.getLabel()) 谁能帮我解决这个问题?提前感谢。引用javadoc的列表#void add(int in

我得到以下例外情况:

线程“main”java.lang.IndexOutOfBoundsException中的异常:索引: 1,大小:0位于java.util.ArrayList.rangeCheckForAdd(未知源) 位于java.util.ArrayList.add(未知源)

导致错误的行是:
dispatchTable.add(mnode.getOffset(),mnode.getLabel())


谁能帮我解决这个问题?提前感谢。

引用javadoc的
列表#void add(int index,E element)

抛出:
...
IndexOutOfBoundsException-如果索引超出范围(索引<0 | |索引>大小())
在您的情况下,index==1size()返回0,因为dispatchTable列表仍然为空

您最好更改此选项:

Throws:
...
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
ArrayList dispatchTable=new ArrayList();
如果(superEntry!=null){
ArrayList superDispatchTable=getDispatchTable(superEntry.getOffset());
对于(int i=0;i
为此:

ArrayList<String> dispatchTable = new ArrayList<String>();

if(superEntry != null) { 
    ArrayList<String> superDispatchTable = getDispatchTable(superEntry.getOffset());

    for(int i = 0; i < superDispatchTable.size(); i++) {
        dispatchTable.add(i, superDispatchTable.get(i));
    }
}
List superDispatchTable=superEntry!=无效的getDispatchTable(superEntry.getOffset()):Collections.EMPTY\u列表;
List dispatchTable=新建ArrayList(超级dispatchTable);

如果
列表为空,则无法在索引
1
处添加元素。使用数组(首先需要找到最大偏移量)。或者一张
地图
“索引:1,大小:0”--这就是你所需要知道的。既然如此,为什么你要使用
添加(数字,对象)
而不仅仅是
添加(对象)
ArrayList<String> dispatchTable = new ArrayList<String>();

if(superEntry != null) { 
    ArrayList<String> superDispatchTable = getDispatchTable(superEntry.getOffset());

    for(int i = 0; i < superDispatchTable.size(); i++) {
        dispatchTable.add(i, superDispatchTable.get(i));
    }
}
    List<String> superDispatchTable = superEntry != null ? getDispatchTable(superEntry.getOffset()) : Collections.EMPTY_LIST;
    List<String> dispatchTable = new ArrayList<>(superDispatchTable);