Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package Oblig1_nid005;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class ArrayDeque<E> implements IDeque<E> {
  6.  
  7. private int capacity;
  8. private E[] array;
  9. private int size;
  10. private int first;
  11. private int last;
  12.  
  13. @SuppressWarnings("unchecked")
  14. public ArrayDeque(int capacity) {
  15. this.array = (E[]) new Object[capacity];
  16. this.capacity = capacity;
  17. size = 0;
  18. }
  19.  
  20. /** public static<T> int find(T[] a, T target){
  21. for (int i = 0; i < array.length; i++)
  22. if(target.equals(a[i])){
  23. return i;
  24. }
  25. return -1;
  26. }*/
  27.  
  28.  
  29.  
  30.  
  31. /**
  32. * privat metode som sjekker om arrayet er fullt.
  33. * @return true dersom arrayet er fullt og false hvis ikke.
  34. */
  35. private boolean fullArray() {
  36. if (size == capacity) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42.  
  43. /**
  44. * privat metode som sjekker om arrayet er tom.
  45. * @return true dersom arrayet er tomt og false hvis ikke.
  46. */
  47. private boolean emptyArray() {
  48. if (size == 0) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54.  
  55. public int size() {
  56. return size;
  57. }
  58.  
  59. public void addFirst(E elem) throws DequeFullException {
  60. if (fullArray()) {
  61. throw new DequeFullException("Array is full");
  62. } array[first] = elem;
  63. first = (first + 1) % (capacity);
  64. size++;
  65. }
  66.  
  67. //Fjerner ikke, Size-- ein plass inni her
  68. public E pullFirst() throws DequeEmptyException {
  69. if (emptyArray()) {
  70. throw new DequeEmptyException("Array is empty");
  71. }
  72. for (int i = 0; i < capacity; i++) {
  73. if (array[i] == null) {
  74. continue;
  75. } else {
  76. E temp = array[i];
  77. array[i] = null;
  78. for (int e=i; e< capacity; e++) {
  79. if (array[e] == null) {
  80. continue;
  81. } else {
  82. array[e - 1] = array[e];
  83. }
  84. }return temp;
  85.  
  86. }
  87. }return null;
  88. }
  89.  
  90.  
  91.  
  92. public E peekFirst() throws DequeEmptyException {
  93. if (emptyArray()){
  94. throw new DequeEmptyException("Array is empty");
  95. } else {
  96. return array[0];
  97. }
  98. }
  99.  
  100.  
  101. public void addLast(E elem) throws DequeFullException {
  102. if (fullArray()){
  103. throw new DequeFullException("Array is full");
  104. } array[last] = elem;
  105. last = (last - 1) % (capacity);
  106. size++;
  107. }
  108.  
  109. //funker ikke
  110. public E pullLast() throws DequeEmptyException {
  111. if (emptyArray()) {
  112. throw new DequeEmptyException("Array is empty");
  113. } else {
  114. last = (last + 1) % (capacity);
  115. size--;
  116. return array[last];
  117. }
  118. }
  119.  
  120. //funker ikke
  121. public E peekLast() throws DequeEmptyException{
  122. if (emptyArray()) {
  123. throw new DequeEmptyException("Array is empty");
  124. } else {
  125. return array[(last + 1) % capacity];
  126. }
  127. }
  128.  
  129. /**
  130. * Metode som fjerner elementer i arrayet, blir brukt av clear().
  131. * @return resultate etter fjerningen.
  132. */
  133.  
  134.  
  135. //funker snart
  136. public void clear() {
  137. this.array = (E[]) new Object[capacity];
  138. /** last =...
  139. first = ...*/
  140. size=0;
  141. }
  142.  
  143.  
  144. //funker
  145. public boolean contains(Object elem){
  146. boolean found = false;
  147. int index = 0;
  148. while (!found && (index < array.length)) {
  149. if (elem.equals(array[index])){
  150. found = true;
  151. }
  152. index++;
  153. }
  154. return found;
  155. }
  156.  
  157. @SuppressWarnings("unchecked")
  158. public <T> T[] toArray(T[] a) {
  159. return (T[]) Arrays.copyOf(array, array.length, a.getClass());
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement