Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. package net.mrpaul.ads.mh050.ps17;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /* ****************************************************************
  6. *
  7. *
  8. * STARTER CODE
  9. *
  10. *
  11. **************************************************************** */
  12.  
  13. import java.util.Iterator;
  14. import java.util.NoSuchElementException;
  15.  
  16. import structures.RandomAccessListI;
  17.  
  18. /**
  19. * Describe here
  20. * @author insert here
  21. *
  22. */
  23. public class MyArrayList<E> implements RandomAccessListI<E>, Iterable<E> {
  24. public static final int CAPACITY_DEFAULT = 10; //initial default size of array
  25. public static final int CAPACITY_MULTIPLIER = 2; //initial default multiplier when resizing array
  26. private E[] store; //array
  27. private int numElements; //logical size of array
  28.  
  29.  
  30.  
  31. /**
  32. * Constructor. Note the cast: We can't make arrays of a type parameter, so we make
  33. * an array of objects and cast it to E[]. Create new array with given size.
  34. * @param initialCapacity
  35. */
  36. @SuppressWarnings("unchecked")
  37. public MyArrayList(int initialCapacity){
  38. store = (E[]) new Object[initialCapacity];
  39. }
  40.  
  41.  
  42. /**
  43. * Create ArrayList with default capacity
  44. */
  45. @SuppressWarnings("unchecked")
  46. public MyArrayList(){
  47. numElements = 0;
  48. store=(E[])new Object[CAPACITY_DEFAULT];
  49. }
  50.  
  51.  
  52.  
  53. /**
  54. * Adds an element to the array. Resize if necessary.
  55. */
  56. public MyArrayList<E> add(E toAdd) {
  57. if(checkResize()){
  58. resize();
  59. }
  60. store[numElements] = toAdd;
  61. numElements ++;
  62.  
  63. return this;
  64. }
  65.  
  66.  
  67. /**
  68. * Get the logical size of the array
  69. */
  70. public int size() {
  71. return numElements;
  72. }
  73.  
  74.  
  75. /**
  76. * Checks whether the parameter element is in the array. Use an iterator.
  77. */
  78. public boolean contains(Object val) {
  79. ArrayListIterator iterator = new ArrayListIterator();
  80. while(iterator.hasNext()){
  81. if(iterator.next().equals(val)){
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87.  
  88.  
  89.  
  90. /**
  91. * Returns an Object[] of the LOGICAL SIZE of the instance variable array.
  92. * i.e. a perfectly sized array of elements.
  93. *
  94. */
  95. public Object[] toArray() {
  96. Object[] ret = new Object[numElements];
  97. for(int i = 0; i < numElements; i++){
  98. ret[i] = store[i];
  99. }
  100. return ret;
  101. }
  102.  
  103.  
  104. /**
  105. * Is the list empty?
  106. */
  107. public boolean isEmpty() {
  108. return numElements == 0;
  109. }
  110.  
  111.  
  112. /**
  113. * Compares logical size and physical size of array. Do we need a resize?
  114. * There must always be space for a new element.
  115. * @return true if resize is required (no capacity left)
  116. */
  117. private boolean checkResize(){
  118. return numElements == store.length;
  119. }
  120.  
  121.  
  122. /**
  123. * Create a new, larger array via Arrays.copyOf (google it) and copy everything
  124. * from the array into store. New array should be larger by a factor of CAPACITY_MULTIPLIER
  125. */
  126. private void resize(){
  127. store = Arrays.copyOf(store, store.length * CAPACITY_MULTIPLIER);
  128. }
  129.  
  130.  
  131.  
  132. /**
  133. * Adds this element at the given index. Shift everything else and resize as necessary.
  134. * If trying to add an element with an invalid index, throw an IndexOutOfBoundsException. For
  135. * example, if you are trying to add an element at index 7 to an ArrayList with only 3 elements.
  136. * Adding an element at index 3 to an ArrayList of 3 elements should not throw an exception.
  137. */
  138. public MyArrayList<E> add(int i, E value) {
  139. if(i > numElements){
  140. throw new IndexOutOfBoundsException();
  141. }
  142. if(checkResize()){
  143. resize();
  144. }
  145.  
  146. for(int j = store.length; j > i; j--){
  147. store[j] = store[j - 1];
  148. }
  149. store[i] = value;
  150.  
  151. return this;
  152. }
  153.  
  154.  
  155. /**
  156. * Set the element at the given index to this value.
  157. * Throw an IndexOutOfBounds Exception if the index is out of range
  158. */
  159. public E set(int i, E value) {
  160. if(i >= numElements){
  161. throw new IndexOutOfBoundsException();
  162. }
  163. E ret = store[i];
  164. store[i] = value;
  165. return ret;
  166. }
  167.  
  168.  
  169. /**
  170. * Remove the element at the given index; shift everything else and resize as necessary.
  171. * Return the element that was removed from the list.
  172. * Throw an IndexOutOfBounds Exception if the index is out of range
  173. */
  174. public E remove(int i) {
  175. if(i >= numElements){
  176. throw new IndexOutOfBoundsException();
  177. }
  178. E ret = store[i];
  179. for(int j = i; j < numElements + 1; j++){
  180. store[j] = store[j+1];
  181. }
  182. numElements--;
  183. return ret;
  184. }
  185.  
  186.  
  187.  
  188. /**
  189. * Return the element at the given index.
  190. * Throw an IndexOutOfBounds Exception if the index is out of range
  191. */
  192. public E get(int i) {
  193. if(i >= numElements){
  194. throw new IndexOutOfBoundsException();
  195. }
  196. return store[i];
  197. }
  198.  
  199.  
  200. /**
  201. * Remove the first appearance of the given value.
  202. * Return true if the list contained the specified element.
  203. */
  204. public boolean remove(E val) {
  205. for(int i = 0; i < store.length; i++){
  206. if(get(i).equals(val)){
  207. remove(i);
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213.  
  214.  
  215. /**
  216. * Empty the array list. Think: Do you NEED to set everything to null?
  217. */
  218. public void clear() {
  219. for(int i = 0; i < store.length; i++) {
  220. store[i] = null;
  221. }
  222. numElements = 0;
  223. }
  224.  
  225.  
  226. /**
  227. * create and return a new instance of the inner class ArrayListIterator
  228. */
  229. public Iterator<E> iterator() {
  230. return new ArrayListIterator();
  231. }
  232.  
  233.  
  234.  
  235. /**
  236. * This is an inner class that defines an iterator for the MyArrayList
  237. *
  238. */
  239. public class ArrayListIterator implements Iterator<E>{
  240.  
  241. private int at = 0; //index of current element
  242. private boolean readyToDelete = false; //ready to remove an element?
  243.  
  244.  
  245. /**
  246. * Compare where we are to the logical size of the array. Is there a next element?
  247. * Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
  248. */
  249. public boolean hasNext() {
  250. if(at < numElements){
  251. return true;
  252. }
  253. return false;
  254. }
  255.  
  256.  
  257. /**
  258. * If the iteration has no more elements, throw a NoSuchElementException
  259. * Otherwise, increment the current element tracker, flip your ready to delete flag, and
  260. * return the previous (current-1) element.
  261. */
  262. public E next() {
  263. if(!hasNext()){
  264. throw new NoSuchElementException();
  265. }
  266. at++;
  267. readyToDelete = !readyToDelete;
  268. return store[at - 1];
  269. }
  270.  
  271.  
  272. /**
  273. * Removes from the underlying collection the last element returned by this iterator.
  274. * This method can be called only once per call to next(). Again: A call to remove MUST
  275. * be preceded by a call to next. That's when we throw the exception.The behavior of an
  276. * iterator is unspecified if the underlying collection is modified while the iteration
  277. * is in progress in any way other than by calling this method.
  278. * <p>
  279. * If we've never before called next OR if we've already called remove since the last call to next,
  280. * throw an IllegalStateException
  281. * Else adjust your delete flag and remove the appropriate element.
  282. *
  283. * Remember, you can call an outer-class method via: OuterClass.this.method()
  284. *
  285. */
  286. public void remove() {
  287. if(!readyToDelete){
  288. throw new IllegalStateException();
  289. }
  290. readyToDelete = !readyToDelete;
  291. }
  292.  
  293. }
  294.  
  295.  
  296. public String toString(){
  297. StringBuilder sb = new StringBuilder();
  298. for(E value : store){
  299. sb.append(value + ", ");
  300. }
  301. return sb.toString().substring(0, sb.toString().length() - 2);
  302. }
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement