我有这个:
public class DoubleList<Key, Elem> implements ADTDoubleList<Key, Elem> {
private Vector<Node<Key, Elem>> leftRight = new Vector<Node<Key, Elem>>(2);
private int[] numLeftNumRight = new int[2];
public DoubleList() {
this.leftRight.set(0, null);
this.leftRight.set(1, null);
this.numLeftNumRight[0] = 0;
this.numLeftNumRight[1] = 0;
}
}
它会抛出 ArrayIndexOutOfBoundsException。
我不知道为什么。有人可以帮助我吗?
您不能在 a 中设置元素Vector
或任何其他List
如果该索引尚未被占用。通过使用new Vector<Node<Key, Elem>>(2)
你要确保向量最初有capacity对于两个元素,但它仍然是空的,所以get
ting or set
使用任何索引都不起作用。
换句话说,该列表还没有增长到足以使该索引有效的程度。使用这个代替:
this.leftRight.add(null); //index 0
this.leftRight.add(null); //index 1
你还可以这样做:
this.leftRight.add(0, null);
this.leftRight.add(1, null);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)