Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. /*
  2. * CSE 205: <11333> T-TH 4:30-5:45
  3. * Assignment <Assignment02>
  4. * Papa Demba Diack ASU ID 1216303829 and Abdullah Mahfuz ASU ID 1217109439
  5. * Description: MyArrayList.java
  6. */
  7. import java.util.Arrays;
  8. import java.util.NoSuchElementException;
  9. public class MyArrayList implements MyList {
  10. private int capacity = 16;
  11. private Object[] array = new Object[capacity];
  12. private int size = 0;
  13.  
  14. @Override
  15. public void add(Object o) {
  16. //Object[] pullArray = array;
  17. if (size == capacity)
  18. {
  19. ensureCapacity(capacity*2);
  20. }
  21.  
  22. size++;
  23. Object[] arrayAdder = new Object[size];
  24. int count = 0;
  25. if (array.length != 0 && array[0] != null)
  26. {
  27. for (Object obj : array)
  28. {
  29. arrayAdder[count] = obj;
  30. count++;
  31. }
  32. }
  33. arrayAdder[size-1] = o;
  34. array = arrayAdder;
  35. }
  36.  
  37. @Override
  38. public void insert(int index, Object o) {
  39. //Object[] pullArray = array;
  40. if(index<0 || index>array.length)
  41. throw new NoSuchElementException();
  42.  
  43. Object[] newArray = new Object[size+1];
  44.  
  45. int count = 0;
  46. boolean added = false;
  47. while (count < size+1)
  48. {
  49. if (count == index)
  50. {
  51. newArray[count] = o;
  52. added = true;
  53. } else {
  54. if (!added)
  55. {
  56. newArray[count] = array[count];
  57. } else {
  58. newArray[count] = array[count-1];
  59. }
  60. }
  61. count++;
  62. }
  63.  
  64. size++;
  65. array = newArray;
  66.  
  67.  
  68. /*size++;
  69. Object[] arrayInserter = new Object[size+1];
  70. for (int i = size-1; i>index;i--){
  71. arrayInserter[i] = arrayInserter[i-1];
  72. }
  73. arrayInserter[index] = o;
  74. array = arrayInserter;*/
  75. }
  76.  
  77. @Override
  78. public void remove(int index) {
  79. if(index<0 || index>=array.length)
  80. throw new NoSuchElementException();
  81.  
  82. Object [] arrayRemover = array;
  83. for (int i = index+1;i<arrayRemover.length;i++) {
  84. arrayRemover[i-1] = arrayRemover[i];
  85. }
  86. // (arrayRemover.length)--;
  87. size--;
  88. }
  89.  
  90. @Override
  91. public Object get(int index) {
  92. if(index<0 || index>=array.length)
  93. throw new NoSuchElementException();
  94. return array[index];
  95. }
  96.  
  97. @Override
  98. public int size() {
  99. return size;
  100. }
  101.  
  102. public void ensureCapacity(int minCapacity) {
  103. if (minCapacity < size)
  104. {
  105. return;
  106. } else if (minCapacity < 16) {
  107. capacity = 16;
  108. } else {
  109. capacity = minCapacity;
  110. }
  111.  
  112.  
  113.  
  114. /*if (minCapacity < size)
  115. (minCapacity < 16)
  116.  
  117. Object [] _newarray = new Object[minCapacity];
  118. for (int i = 0; i<minC && i < values.length; i++) {
  119.  
  120. }
  121. /*check to see if array was overweritten, create final size, update with previous values
  122. * Object[] newArr = new Object[minCapacity];
  123. */
  124.  
  125. }
  126.  
  127. public void trimToSize() {
  128. ensureCapacity(size);
  129. }
  130.  
  131. // Do not alter the code below
  132. @Override
  133. public MyListIterator getIterator() {
  134. return new MyArrayListIterator();
  135. }
  136.  
  137. private class MyArrayListIterator implements MyListIterator {
  138. int currentIndex = - 1;
  139.  
  140. @Override
  141. public Object next( ) {
  142. currentIndex++;
  143. return array[currentIndex];
  144. }
  145.  
  146. @Override
  147. public boolean hasNext( ) {
  148. return currentIndex +1 < size;
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement