I have this:
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;
}
}
and it throws an ArrayIndexOutOfBoundsException.
I don’t know why. Could someone help me?
You can’t set an element in a
Vectoror any otherListif that index isn’t already occupied. By usingnew Vector<Node<Key, Elem>>(2)you’re ensuring that the vector initially has the capacity for two elements, but it is still empty and sogetting orsetting using any index won’t work.In other words, the list hasn’t grown big enough for that index to be valid yet. Use this instead:
You could also do: