I am trying to write an insert function where I give the object I want to add and the index I want to add it at. For some reason it adds the object where I want but changes the number it is replacing to null.
This is homework.
Even a simple hint would help!
public void insert(Integer obj, Integer index) {
if( index > array.length){
throw new IllegalArgumentException("The index you entered was out of bounds.");
}
for(Integer i = numItems; i > index; i--){
array[i] = array[i-1];
}
array[index] = obj;
numItems++;
}
}
Here is what I am inserting
iv.insert(44, 2);
iv.insert(33, 0);
iv.insert(22, 1);
iv.insert(11, 3);
iv.insert(2 , 2);
Here are my results
33
22
2
null
11