I am having trouble with this one method in my ArraryList I
I am having trouble with this one method in my ArraryList. I can\'t seem to get the removeItem(index int):E throws invalidoutofBoundExceotion to work. Here is the UML for the class I only need help with removeItem. this is for java
UML DIAGRAM AND DISCUSSION FOR ArrayList
ArrayList<E extends Comparable<E>>
DEFCAP : int final = 50
origCap : int
numElements : int
list : Object[]
<<constructor>>ArrayList()
<<constructor>>ArrayList(size : int) throws InvalidSizeException
+ addItem(item : E) throws MaximumCapacityException
+ getItem(index int) : E throws IndexOutOfBoundsException
+ setItem(index int, item E) throws IndexOutOfBoundsException
+ removeItem(index int) : E throws IndexOutOfBoundsException
+ findItem(item E) : int
+ isEmpty() : Boolean
+ lengthIs() : int
+ clear()
+ toString() : String
+ sort()
- enlarge() throws MaximumCapacityException
public E removeItem(int index)
Attempts to remove the item at the given index from the ArrayList. If that index does not exist within the ArrayList an IndexOutOfBoundsException is thrown with the message: “Index out of Range”.
| ArrayList<E extends Comparable<E>> |
| DEFCAP : int final = 50 origCap : int numElements : int list : Object[] |
| <<constructor>>ArrayList() <<constructor>>ArrayList(size : int) throws InvalidSizeException + addItem(item : E) throws MaximumCapacityException + getItem(index int) : E throws IndexOutOfBoundsException + setItem(index int, item E) throws IndexOutOfBoundsException + removeItem(index int) : E throws IndexOutOfBoundsException + findItem(item E) : int + isEmpty() : Boolean + lengthIs() : int + clear() + toString() : String + sort() - enlarge() throws MaximumCapacityException |
Solution
Hi, Please find my implementation.
Please let men know in case of any issue.
public E removeItem(int index){
// base case, if index is out of range
if(index >= numElements){
throw new IndexOutOfBoundsException(\"Index bound exception\");
}
// storing items to be removed
E item = list[index];
int i = index;
// removing list[index]
while((i+1) < numElements){
list[i] = list[i+1];
i++;
}
// decreasing numElements
numElements = numElements - 1;
return item;
}


