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;
}
我得到以下异常:
线程中的异常&#34; main&#34; java.lang.IndexOutOfBoundsException:索引: 1,大小:0,java.util.ArrayList.rangeCheckForAdd(未知来源) 在java.util.ArrayList.add(未知来源)
导致错误的行是:dispatchTable.add(mnode.getOffset(), mnode.getLabel());
任何人都可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:-1)
来自List#void add(int index, E element)
Throws:
...
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())
在您的情况下 index == 1 和 size()会返回 0 ,因为 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);