I have a class that implements an interface and I think when I try to insert an element into the array more than once the first insertions are forgotten. I really can't figure this one out. This is what I have:
public void insertElementAt(int index, E el)
throws IllegalArgumentException {
Object temp[] = new Object[data.length + 1];
for (int i = 0; i < data.length; i++) {
if (i == index){
temp[index] = el;
temp[i + 1] = data[i];
i++;
}
temp[i] = data[i];
}
data = temp;
if (index > data.length || index < 0) {
throw new IllegalArgumentException();
}
}
Then my test reports null instead of first at he last assertion.
@Test
public void testInsertToLeft() {
PriorityList<String> list = new ArrayPriorityList<String>();
list.insertElementAt(0, "First");
// Must shift array elements in this case
list.insertElementAt(0, "New First");
assertEquals("New First", list.getElementAt(0));
assertEquals("First", list.getElementAt(1));
}
List.continueat the end ofifblock.data?ArrayList=