Question: there are 2 errors in the ListArrayBased that causes ArrayIndexOutOfBoundsException find the 2 errors and fix it 1 implementation error Attach c omment: //fixes implementation
there are 2 errors in the ListArrayBased that causes ArrayIndexOutOfBoundsException find the 2 errors and fix it
1 implementation error Attach comment: //fixes implementation errors
1 memory leak error Attach comment: //fixes memory leak
1 programming style error that would also prevent you from extending the class and using the inherited code properly (as described below) Attach comment: //fixes programming style
// Array-based implementation of the ADT list public class ListArrayBased implements ListInterface {
private static final int MAX_LIST = 3; protected Object []items; // an array of list items protected int numItems; // number of items in list
public ListArrayBased() { items = new Object[MAX_LIST]; numItems = 0; } // end default constructor
public boolean isEmpty() { return (numItems == 0); } // end isEmpty
public int size() { return numItems; } // end size
public void removeAll() { // Creates a new array; marks old array for // garbage collection. items = new Object[MAX_LIST]; numItems = 0; } // end removeAll
public void add(int index, Object item) throws ListIndexOutOfBoundsException { if (numItems > MAX_LIST) { throw new ListException("ListException on add"); } // end if if (index >= 0 && index <= numItems) { // make room for new element by shifting all items at // positions >= index toward the end of the // list (no shift if index == numItems+1) for (int pos = numItems-1; pos >= index; pos--) //textbook code modified to eliminate logic error causing ArrayIndexOutOfBoundsException { items[pos+1] = items[pos]; } // end for // insert new item items[index] = item; numItems++; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on add"); } // end if } //end add
public Object get(int index) throws ListIndexOutOfBoundsException { if (index >= 0 && index < numItems) { return items[index]; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get"); } // end if } // end get
public void remove(int index) throws ListIndexOutOfBoundsException { if (index >= 0 && index < numItems) { // delete item by shifting all items at // positions > index toward the beginning of the list // (no shift if index == size) for (int pos = index+1; pos < numItems; pos++) //textbook code modified to eliminate logic error causing ArrayIndexOutOfBoundsException
{ items[pos-1] = items[pos]; } // end for numItems--; } else { // index out of range throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on remove"); } // end if } //end remove }
// Interface ListInterface for the ADT list. public interface ListInterface { boolean isEmpty(); int size(); void add(int index, Object item) throws ListIndexOutOfBoundsException; Object get(int index) throws ListIndexOutOfBoundsException; void remove(int index) throws ListIndexOutOfBoundsException; void removeAll(); String toString(); } // end ListInterface
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
