public String codeGeneration() {
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));
}
}
String methodsCode = "";
for(Node m : methods) {
methodsCode+=m.codeGeneration();
MethodNode mnode = (MethodNode) m;
dispatchTable.add(mnode.getOffset(), mnode.getLabel());
}
addDispatchTable(dispatchTable);
String codeDT = "";
for(String s : dispatchTable) {
codeDT+= "push " + s + "\n"
+ "lhp\n"
+ "sw\n"
+ "lhp\n"
+ "push 1\n"
+ "add\n"
+ "shp\n";
}
return "lhp\n"
+ codeDT;
}
我得到了以下异常:
线程"main“中的
异常java.lang.IndexOutOfBoundsException:索引: 1,大小:0位于java.util.ArrayList.rangeCheckForAdd(未知源)的java.util.ArrayList.add(未知源)
导致错误的行是:dispatchTable.add(mnode.getOffset(), mnode.getLabel());
有人能帮我解决这个问题吗?提前谢谢。
发布于 2018-05-29 06:56:59
引用List#void add(int index, E element)
的javadoc
Throws:
...
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
在本例中,索引== 1和size()返回,因为dispatchTable列表仍然为空。
你最好改变这一点:
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);
https://stackoverflow.com/questions/50573884
复制相似问题