ArrayList.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * ArrayList represents an array implementation of a list. The front of
  3. * the list is kept at array index 0. This class will be extended
  4. * to create a specific kind of list.
  5. *
  6. * @author Dr. Lewis
  7. * @author Dr. Chase
  8. * @version 1.0, 08/13/08
  9. */
  10. import java.util.Iterator;
  11. public class ArrayList<T> implements ListADT<T>, Iterable<T>
  12. {
  13. protected final int DEFAULT_CAPACITY = 100;
  14. private final int NOT_FOUND = -1;
  15. protected int rear;
  16. protected T[] list;
  17. /**
  18. * Creates an empty list using the default capacity.
  19. */
  20. public ArrayList() {
  21. rear = 0;
  22. list = (T[])(new Object[DEFAULT_CAPACITY]);
  23. }
  24. /**
  25. * Creates an empty list using the specified capacity.
  26. *
  27. * @param initialCapacity the integer value of the size of the array list
  28. */
  29. public ArrayList (int initialCapacity)
  30. {
  31. rear = 0;
  32. list = (T[])(new Object[initialCapacity]);
  33. }
  34. /**
  35. * Removes and returns the last element in this list.
  36. *
  37. * @return the last element in the list
  38. * @throws EmptyCollectionException if an empty collection exception occurs
  39. */
  40. public T removeLast () throws EmptyCollectionException
  41. {
  42. T result;
  43. if (isEmpty())
  44. throw new EmptyCollectionException ("list");
  45. rear--;
  46. result = list[rear];
  47. list[rear] = null;
  48. return result;
  49. }
  50. /**
  51. * Removes and returns the first element in this list.
  52. *
  53. * @return the first element in the list
  54. * @throws EmptyCollectionException if an empty collection exception occurs
  55. */
  56. public T removeFirst() throws EmptyCollectionException
  57. {
  58. if (isEmpty())
  59. throw new EmptyCollectionException ("list");
  60. T result = list[0];
  61. rear--;
  62. /** shift the elements */
  63. for (int scan=0; scan < rear; scan++)
  64. list[scan] = list[scan+1];
  65. list[rear] = null;
  66. return result;
  67. }
  68. /**
  69. * Removes and returns the specified element.
  70. *
  71. * @param element the element to be removed and returned
  72. * from the list
  73. * @return the removed elememt
  74. * @throws ElementNotFoundException if an element not found exception occurs
  75. */
  76. public T remove (T element)
  77. {
  78. T result;
  79. int index = find (element);
  80. if (index == NOT_FOUND)
  81. throw new ElementNotFoundException ("list");
  82. result = list[index];
  83. rear--;
  84. /** shift the appropriate elements */
  85. for (int scan=index; scan < rear; scan++)
  86. list[scan] = list[scan+1];
  87. list[rear] = null;
  88. return result;
  89. }
  90. /**
  91. * Returns a reference to the element at the front of this list.
  92. * The element is not removed from the list. Throws an
  93. * EmptyCollectionException if the list is empty.
  94. *
  95. * @return a reference to the first element in the
  96. * list
  97. * @throws EmptyCollectionException if an empty collection exception occurs
  98. */
  99. public T first() throws EmptyCollectionException
  100. {
  101. if (isEmpty())
  102. throw new EmptyCollectionException ("list");
  103. return list[0];
  104. }
  105. /**
  106. * Returns a reference to the element at the rear of this list.
  107. * The element is not removed from the list. Throws an
  108. * EmptyCollectionException if the list is empty.
  109. *
  110. * @return a reference to the last element of this list
  111. * @throws EmptyCollectionException if an empty collection exception occurs
  112. */
  113. public T last() throws EmptyCollectionException
  114. {
  115. if (isEmpty())
  116. throw new EmptyCollectionException ("list");
  117. return list[rear-1];
  118. }
  119. /**
  120. * Returns true if this list contains the specified element.
  121. *
  122. * @param target the element that the list is searched for
  123. * @return true if the target is in the list, false if otherwise
  124. */
  125. public boolean contains (T target)
  126. {
  127. return (find(target) != NOT_FOUND);
  128. }
  129. /**
  130. * Returns the array index of the specified element, or the
  131. * constant NOT_FOUND if it is not found.
  132. *
  133. * @param target the element that the list will be searched for
  134. * @return the integer index into the array containing the target
  135. * element, or the NOT_FOUND constant
  136. */
  137. private int find (T target)
  138. {
  139. int scan = 0, result = NOT_FOUND;
  140. boolean found = false;
  141. if (! isEmpty())
  142. while (! found && scan < rear)
  143. if (target.equals(list[scan]))
  144. found = true;
  145. else
  146. scan++;
  147. if (found)
  148. result = scan;
  149. return result;
  150. }
  151. /**
  152. * Returns true if this list is empty and false otherwise.
  153. *
  154. * @return true if the list is empty and false if otherwise
  155. */
  156. public boolean isEmpty()
  157. {
  158. return (rear == 0);
  159. }
  160. /**
  161. * Returns the number of elements currently in this list.
  162. *
  163. * @return the integer representation of the number of elements in the list
  164. */
  165. public int size()
  166. {
  167. return rear;
  168. }
  169. /**
  170. * Returns an iterator for the elements currently in this list.
  171. *
  172. * @return an iterator for the elements in this list
  173. */
  174. public Iterator<T> iterator()
  175. {
  176. return new ArrayIterator<T> (list, rear);
  177. }
  178. /**
  179. * Returns a string representation of this list.
  180. *
  181. * @return the string representation of this list
  182. */
  183. public String toString()
  184. {
  185. String result = "";
  186. for (int scan=0; scan < rear; scan++)
  187. result = result + list[scan].toString() + "\n";
  188. return result;
  189. }
  190. /**
  191. * Creates a new array to store the contents of this list with
  192. * twice the capacity of the old one.
  193. */
  194. protected void expandCapacity()
  195. {
  196. T[] larger = (T[])(new Object[list.length*2]);
  197. for (int scan=0; scan < list.length; scan++)
  198. larger[scan] = list[scan];
  199. list = larger;
  200. }
  201. public void addToRear (T element) {
  202. list[rear] = element;
  203. }
  204. }