Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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.  
  21. /**
  22. * privat metode som sjekker om arrayet er fullt.
  23. * @return true dersom arrayet er fullt og false hvis ikke.
  24. */
  25. private boolean fullArray() {
  26. if (size == capacity) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32.  
  33. /**
  34. * privat metode som sjekker om arrayet er tom.
  35. * @return true dersom arrayet er tomt og false hvis ikke.
  36. */
  37. private boolean emptyArray() {
  38. if (size == 0) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44.  
  45. public int size() {
  46. return size;
  47. }
  48.  
  49. public void addFirst(E elem) throws DequeFullException {
  50. if (fullArray()) {
  51. throw new DequeFullException("Array is full");
  52. } array[first] = elem;
  53. first = (first + 1) % (capacity);
  54. size++;
  55. }
  56.  
  57. public E pullFirst() throws DequeEmptyException {
  58. if(emptyArray()) {
  59. throw new DequeEmptyException("Array is empty");
  60. }if(first == 0) {
  61. first = capacity;
  62. first = (first - 1) % (capacity);
  63. size--;
  64. }
  65. return array[first];
  66. }
  67.  
  68.  
  69.  
  70. public E peekFirst() throws DequeEmptyException {
  71. if (emptyArray()){
  72. throw new DequeEmptyException("Array is empty");
  73. } else {
  74. return array[0];
  75. }
  76. }
  77.  
  78. public void addLast(E elem) throws DequeFullException {
  79. if (fullArray()){
  80. throw new DequeFullException("Array is full");
  81. } array[last] = elem;
  82. last = (last - 1) % (capacity);
  83. size++;
  84. }
  85.  
  86. public E pullLast() throws DequeEmptyException {
  87. if (emptyArray()) {
  88. throw new DequeEmptyException("Array is empty");
  89. } else {
  90. last = (last + 1) % (capacity);
  91. size--;
  92. return array[last];
  93. }
  94. }
  95.  
  96. public E peekLast() throws DequeEmptyException{
  97. if (emptyArray()) {
  98. throw new DequeEmptyException("Array is empty");
  99. } else {
  100. return array[(last + 1) % capacity];
  101. }
  102. }
  103.  
  104. /**
  105. * Metode som fjerner elementer i arrayet, blir brukt av clear().
  106. * @return resultate etter fjerningen.
  107. */
  108.  
  109. private E remove(){
  110. E result = null;
  111. if (size > 0){
  112. result = array[size - 1];
  113. array[size - 1] = null;
  114. size--;
  115. }return result;
  116. }
  117.  
  118. public void clear() {
  119. while (!emptyArray()){
  120. remove();
  121. }
  122. }
  123.  
  124.  
  125.  
  126. public boolean contains(Object elem){
  127. boolean found = false;
  128. int index = 0;
  129. while (!found && (index <= array.length)) {
  130. if (elem.equals(array[index])){
  131. found = true;
  132. }
  133. index++;
  134. }
  135. return found;
  136. }
  137.  
  138. @SuppressWarnings("unchecked")
  139. public <T> T[] toArray(T[] a) {
  140. return (T[]) Arrays.copyOf(array, array.length, a.getClass());
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement