public T removeMethod(int id) { // remove and return element at position index /
ID: 3745869 • Letter: P
Question
public T removeMethod(int id)
{
// remove and return element at position index
// shift elements to remove any gap in the list
// throw IndexOutOfBoundsException for invalid index
// halve capacity if the number of elements falls below 1/3 of the capacity
// capacity should NOT go below INITIALCAPACITY
// O(N) where N is the number of elements in the list
if (id < 0 )
{
throw new RuntimeException("invalid Index " + index );
}
T ri = box[id];
for (int i = id; i < sizeofElements - 1; i++)
{
box[i] = box{i + 1];
}
sizeofElements--;
if(sizeofElements < (Capacity/3))
{
Capacity = Capacity/2;
if(Capacity < INITIALCAPACITY)
{
Capacity = INITIALCAPACITY;
}
}
return ri;
}
CAN SOMEONE DOUBLE CHECK THIS METHOD AND IF THE SHRINKING IS RIGHT? THE CAPACITY NEEDS TO HALFED IF THE NUMBER OF ELEMENTS FALLS BELOW 1/3 OF THE CAPACITY. I AM NOT SURE ABOUT THE SHRINKING
Explanation / Answer
public T removeMethod(int id) { // remove and return element at position index // shift elements to remove any gap in the list // throw IndexOutOfBoundsException for invalid index // halve capacity if the number of elements falls below 1/3 of the capacity // capacity should NOT go below INITIALCAPACITY // O(N) where N is the number of elements in the list if (id < 0) { throw new RuntimeException("invalid Index " + index); } T ri = box[id]; for (int i = id; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.