Question: Design and write a complete test program to test if the MyArrayList class in Listing 24.3 meets all requirements. Listing 1 public class MyArrayList extends

Design and write a complete test program to test if the MyArrayList class in Listing 24.3 meets all requirements.

Listing

1 public class MyArrayList extends MyAbstractList { public static final int INITIAL_CAPACITY

= 16; private E[] data = (E[]) new Object[INITIAL_CAPACITY]; 4 2 3

5 /** Create a default list */ public MyArrayList() { 8 /**

1 public class MyArrayList extends MyAbstractList { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[]) new Object[INITIAL_CAPACITY]; 4 2 3 5 /** Create a default list */ public MyArrayList() { 8 /** Create a list from an array of objects */ public MyArrayList(E[] objects) { for (int i = 0; i < objects.length; i++) add(objects[i]); // Warning: don't use super(objects)! 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 @Override /** Add a new element at the specified index */ public void add(int index, E e) { ensureCapacity(); // Move the elements to the right after the specified index for (int i = size - 1; i >= index; i--) data[i + 1] = data[i]; // Insert new element to data[index] data[index] - e; // Increase size by 1 size++; 26 27 28 29 /** Create a new larger array, double the current size + 1 */ private void ensureCapacity() { if (size >= data.length) { E[] newData = (E[])(new Object[size * 2 + 1]); System.arraycopy(data, 0, newData, 0, size); data = 30 31 32 33 34 35 newData; 36 37 38 39 @Override /** Clear the list */ public void clear() { data = (E[])new Object[INITIAL_CAPACITY]; size = 0; 40 41 42 43 44 45 @Override /** Return true if this list contains the element */ public boolean contains(E e) { for (int i = 0; i < size; i++) if (e.equals(data[i])) return true; 46 47 48 49 50 return false; 51 52 53 GOverride /** Return the element at the specified index */ public E get(int index) { checkIndex (index); return data[index]; 54 55 56 57 58 59 private void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOut0fBoundsException ("index " + index + " out of bounds"); 60 61 62 63 64 65 @Override /** Return the index of the first matching element * in this list. Return -1 if no match. */ public int index0f(E e) { for (int i = 0; i < size; i++) if (e.equals(data[i])) return i; 66 67 68 69 70 71 return -1; 72 73 74 @Override /** Return the index of the last matching element in this list. Return -1 if no match. */ public int lastIndex0f (E e) { for (int i = size - 1; i >= 0; i--) if (e.equals(data[i])) return i; 75 76 77 78 79 80 return -1; 81 82 83 @Override /** Remove the element at the specified position * in this list. Shift any subsequent elements to the left. Return the element that was removed from the list. */ public E remove(int index) { checkIndex (index); 84 85 86 87 88 E e - data[index]; 89 90 // Shift data to the left for (int j = index; j < size - 1; j++) data[j] = data[j + 1]; 91 92 93 94 data[size - 1] = null; // This element is now null 95 96 // Decrement size size--; 97 98

Step by Step Solution

3.33 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Program Plan Create an ArrayListTesting class for test the MyArrayList class This MyArrayList class extends MyAbstractList class and that class implements MyList interface These three classes are refe... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!