我正在做功课,要求我们为BTree实现split_child功能。
这是我的解决方案:
void split_child(int i, int t) {
BTreeNode y = children.get(i);
ArrayList<Integer> key1 = new ArrayList<Integer>(y.keys.subList(0, t - 1));
ArrayList<BTreeNode> children1 = new ArrayList<BTreeNode>(y.children.subList(0, t));
ArrayList<Integer> key2 = new ArrayList<Integer>(y.keys.subList(t, 2 * t - 1));
ArrayList<BTreeNode> children2 = new ArrayList<BTreeNode>(y.children.subList(t, t - 1));
BTreeNode y1 = new BTreeNode(key1, children2, y.is_leaf);
BTreeNode y2 = new BTreeNode(key2, children2, y.is_leaf);
int midian = y.keys.get(t - 1);
y.insert(midian, t);
}
在此之后,我可以成功编译项目,但是当我运行它时,它会抛出这些错误:
java.lang.IndexOutOfBoundsException: toIndex = 3
at java.util.ArrayList.subListRangeCheck(ArrayList.java:1004)
at java.util.ArrayList.subList(ArrayList.java:996)
at BTreeNode.split_child(BTreeNode.java:45)
at BTree.insert(BTree.java:30)
at BTree.main(BTree.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
似乎我的代码有一些问题,但我真的无法找到它。第45行是这段代码:
ArrayList<BTreeNode> children1 = new ArrayList<BTreeNode>(y.children.subList(0, t));