ListArrayBasedPlus.java (1266B)
1 package lab2; 2 3 public class ListArrayBasedPlus extends ListArrayBased { 4 5 public ListArrayBasedPlus() { 6 super(); 7 } 8 9 public void add(int index, Object item) 10 throws ListIndexOutOfBoundsException 11 { 12 if (numItems == items.length ) //fixes implementation errors and programming errors. 13 { 14 resize(); 15 } // end if 16 if (index >= 0 && index <= numItems) 17 { 18 super.add(index, item); 19 } 20 } //end add 21 22 public void reverse() { 23 for(int i = 0; i < numItems/2; i++) { 24 Object tempObject = items[i]; 25 items[i] = items[numItems - i - 1]; 26 items[numItems - i - 1] = tempObject; 27 } 28 System.out.println("List reversed"); 29 } 30 31 public void resize() { 32 Object []tmp; 33 tmp = new Object[numItems*2 + 1]; 34 for (int i = 0; i < numItems; i++) { 35 tmp[i] = items[i]; 36 } 37 items = tmp; 38 } 39 40 @Override 41 public String toString() { 42 String result = "List of size " + size() + " has the following items : "; 43 44 for (int i = 0; i < size(); i++) { 45 if (items[i] != null) 46 result += items[i] + " "; 47 } 48 49 return result; 50 } 51 }