1/*2 * ArrayList.java3 *4 * Computer Science S-22, Harvard University5 */6 7import java.util.*;8 9/*10 * A class that implements our simple List interface using an array.11 */12public class ArrayList implements List {13 private Object[] items; // the items in the list14 private int length; // # of items in the list15 16 /*17 * Constructs an ArrayList object with the specified maximum size18 * for a list that is initially empty.19 */20 public ArrayList(int maxSize) {21 if (maxSize <= 0) {22 throw new IllegalArgumentException("max size must be positive");23 }24 items = new Object[maxSize];25 length = 0;26 }27 28 /*29 * Constructs an ArrayList object containing the items in the specified30 * array, and with a max size that is twice the size of that array 31 * (to allow room for growth).32 */33 public ArrayList(Object[] initItems) {34 items = new Object[2 * initItems.length]; 35 for (int i = 0; i < initItems.length; i++) {36 items[i] = initItems[i];37 }38 39 length = initItems.length;40 }41 42 /*43 * length - returns the number of items in the list 44 */45 public int length() {46 return length;47 }48 49 /* 50 * isFull - returns true if the list is full, and false otherwise51 */52 public boolean isFull() {53 return (length == items.length);54 }55 56 /* getItem - returns the item at position i in the list */57 public Object getItem(int i) {58 if (i < 0 || i >= length) {59 throw new IndexOutOfBoundsException();60 }61 62 return items[i];63 }64 65 /* 66 * addItem - adds the specified item at position i in the list,67 * shifting the items that are currently in positions i, i+1, i+2,68 * etc. to the right by one. Returns false if the list is full,69 * and true otherwise.70 */71 public boolean addItem(Object item, int i) {72 if (item == null || i < 0 || i > length) {73 throw new IllegalArgumentException();74 } else if (isFull()) {75 return false;76 }77 78 // make room for the new item79 for (int j = length - 1; j >= i; j--) {80 items[j + 1] = items[j];81 }82 83 items[i] = item;84 length++;85 return true;86 }87 88 /* 89 * removeItem - removes the item at position i in the list,90 * shifting the items that are currently in positions i+1, i+2,91 * etc. to the left by one. Returns a reference to the removed92 * object.93 */94 public Object removeItem(int i) {95 if (i < 0 || i >= length) {96 throw new IndexOutOfBoundsException();97 }98 99 Object removed = items[i];100 101 // fill in the "hole" left by the removed item102 for (int j = i; j < length - 1; j++) {103 items[j] = items[j + 1];104 }105 items[length - 1] = null;106 107 length--;108 return removed;109 }110 111 /*112 * toString - converts the list into a String of the form 113 * {item0, item1, ...}114 */115 public String toString() {116 String str = "{";117 118 for (int i = 0; i < length; i++) {119 str = str + items[i];120 if (i < length - 1) {121 str = str + ", ";122 }123 }124 125 str = str + "}";126 return str;127 }128 129 /*130 * iterator - returns an iterator for this list131 */132 public ListIterator iterator() {133 // still needs to be implemented134 return null;135 }136}
Object[] items
Field defined at line 13.
int length
Field defined at line 14.
ArrayList(maxSize)
Constructor defined at line 20.
ArrayList(initItems)
Constructor defined at line 33.
length()
Method defined at line 45.
isFull()
Method defined at line 52.
getItem(i)
Method defined at line 57.
addItem(item, i)
Method defined at line 71.
removeItem(i)
Method defined at line 94.
toString()
Method defined at line 115.
iterator()
Method defined at line 132.