每个循环中的java.lang.IndexOutOfBoundsException 公共字符串代码生成(){ ArrayList dispatchTable=新的ArrayList(); 如果(superEntry!=null){ ArrayList superDispatchTable=getDispatchTable(superEntry.getOffset()); 对于(int i=0;i
我得到以下例外情况: 线程“main”java.lang.IndexOutOfBoundsException中的异常:索引: 1,大小:0位于java.util.ArrayList.rangeCheckForAdd(未知源) 位于java.util.ArrayList.add(未知源) 导致错误的行是:每个循环中的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
dispatchTable.add(mnode.getOffset(),mnode.getLabel())代码>
谁能帮我解决这个问题?提前感谢。引用javadoc的列表#void add(int index,E element)
抛出:
...
IndexOutOfBoundsException-如果索引超出范围(索引<0 | |索引>大小())
在您的情况下,index==1和size()返回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);