SimpleArrayList.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import java.util.NoSuchElementException;
  2. /**
  3. * An implementation of the SimpleList interface using a circular array.
  4. *
  5. * @param <E> component type
  6. */
  7. public class SimpleArrayList<E> implements SimpleList<E> {
  8. // Main method to test the addFirst method.
  9. // Add to this to test other methods.
  10. public static void main(String[] args) {
  11. SimpleList<Integer> list = new SimpleArrayList<Integer>(20);
  12. for (int i = 2; i <= 40; i += 2) list.addFirst(i);
  13. System.out.println(list);
  14. }
  15. private E[] store; // Array where list's elements are stored.
  16. private int head; // Index of the first element in the list
  17. private int tail; // Index of next available array cell at end of list
  18. private int size; // Number of elements in the list
  19. /**
  20. * Capacity constructor - creates an empty list with an
  21. * array of the given capacity.
  22. *
  23. * @param initialCapacity initial capacity (length) of the array
  24. */
  25. public SimpleArrayList(int initialCapacity) {
  26. store = (E[]) new Object[initialCapacity];
  27. }
  28. /**
  29. * Default constructor - creates an empty list with an
  30. * array of capacity 10.
  31. */
  32. public SimpleArrayList() {
  33. this(10);
  34. }
  35. @Override
  36. /**
  37. * Adds the given element to the start of the list.
  38. * (Increases array capacity if array is full.)
  39. * @param elem element to add
  40. */
  41. public void addFirst(E elem) {
  42. if (size == store.length) increaseCapacity();
  43. head = (head - 1 + store.length) % store.length;
  44. store[head] = elem;
  45. size++;
  46. }
  47. @Override
  48. /**
  49. * Removes the last element from the list.
  50. * @return element that was removed
  51. * @throws NoSuchElementException if there is no last element
  52. */
  53. public E removeLast() {
  54. if (size == 0) throw new NoSuchElementException("list empty");
  55. tail = (tail - 1 + store.length) % store.length;
  56. E doomed = store[tail];
  57. size--;
  58. return doomed;
  59. }
  60. @Override
  61. /**
  62. * Returns the element in the list at the given index.
  63. *
  64. * (NOTE: The given index is the list index, not the
  65. * array index.)
  66. *
  67. * @param index index of element to return
  68. * @return element at given index
  69. * @throws IndexOutOfBoundsException if given index is invalid
  70. */
  71. public E get(int index) {
  72. if (index < 0 || index >= size) throw new IndexOutOfBoundsException(
  73. "invalid index"
  74. );
  75. int arrayIndex = (head + index) % store.length;
  76. return store[arrayIndex];
  77. }
  78. @Override
  79. /**
  80. * Returns true of this list is equal to the given list.
  81. * Two lists are equal if they contain the same elements in the same order.
  82. * @param otherList list to compare to this list
  83. * @return true if lists are equal
  84. */
  85. public boolean equals(SimpleList<E> otherList) {
  86. if (otherList instanceof SimpleArrayList) {
  87. SimpleArrayList<E> otherArrayList = (SimpleArrayList<E>) otherList;
  88. if (this.size != otherArrayList.size) return false;
  89. for (int i = 0; i < size; i++) {
  90. E thisEl = this.get(i);
  91. E otherEl = otherArrayList.get(i);
  92. if (!thisEl.equals(otherEl)) return false;
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. @Override
  99. public void addLast(E elem) {
  100. if (size == store.length) increaseCapacity();
  101. store[tail] = elem;
  102. tail = (tail + 1) % store.length;
  103. size++;
  104. }
  105. @Override
  106. public E removeFirst() {
  107. if (size == 0) throw new NoSuchElementException("list empty");
  108. E doomed = store[head];
  109. head = (head + 1) % store.length;
  110. size--;
  111. return doomed;
  112. }
  113. @Override
  114. public int size() {
  115. return size;
  116. }
  117. @Override
  118. public String toString() {
  119. if (size == 0) return "[]";
  120. else {
  121. String s = "[";
  122. for (
  123. int i = 0, j = head;
  124. i < size;
  125. i++, j = (j + 1) % store.length
  126. ) s += store[j] + ", ";
  127. return s.substring(0, s.length() - 2) + "]";
  128. }
  129. }
  130. private void increaseCapacity() {
  131. E[] bigger = (E[]) new Object[size + size / 2];
  132. for (int i = 0; i < size; i++) {
  133. bigger[i] = store[head];
  134. head = (head + 1) % size;
  135. }
  136. head = 0;
  137. tail = size;
  138. store = bigger;
  139. }
  140. }