public class Bag extends List public class Bag{ private List bag; public T remove(){ bag.remove(0); } } public class List { T[] list; int numEntries; public T getEntry(int position) { return list[position-1]; } public void add(int newPosition, T newValue) { newPosition = newPosition - 1; //make sure there's room in the array if (numEntries == list.length) { doubleCapacity(); } makeRoom(newPosition); list[newPosition] = newValue; numEntries++; } public T remove (int givenPosition) { if (numEntries == 0) throw new IndexOutOfBoundsException("..."); if (givenPosition > numEntries) throw new IndexOutOfBoundsException("..."); if (givenPosition <= 0) throw new IndexOutOfBoundsException("..."); givenPosition--; T removedValue = list[givenPosition]; removeGap(givenPosition); numEntries--; return removedValue; } private void removeGap(int position) { //assert to ensure position is valid for (int i = position; i < numEntries-1; i++) { list[i] = list[i+1]; } return; } private void makeRoom(int newPosition) { //ensure newPosition is a valid location for makeRoom: assert (newPosition >= 1) && (newPosition <= numberOfEntries+1); // move each entry to next higher index, starting at end of // list and continuing until the entry at newPosition is moved int newIndex = newPosition; int lastIndex = numberOfEntries; for (int index = lastIndex; index >= newIndex; index--) { list[index+1] = list[index]; } } } list.getEntry(2); list.add(2, "cat"); list.remove(2);