Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Oblig1_nid005;
- import java.util.Arrays;
- public class ArrayDeque<E> implements IDeque<E> {
- private int capacity;
- private E[] array;
- private int size;
- private int first;
- private int last;
- @SuppressWarnings("unchecked")
- public ArrayDeque(int capacity) {
- this.array = (E[]) new Object[capacity];
- this.capacity = capacity;
- size = 0;
- }
- /** public static<T> int find(T[] a, T target){
- for (int i = 0; i < array.length; i++)
- if(target.equals(a[i])){
- return i;
- }
- return -1;
- }*/
- /**
- * privat metode som sjekker om arrayet er fullt.
- * @return true dersom arrayet er fullt og false hvis ikke.
- */
- private boolean fullArray() {
- if (size == capacity) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * privat metode som sjekker om arrayet er tom.
- * @return true dersom arrayet er tomt og false hvis ikke.
- */
- private boolean emptyArray() {
- if (size == 0) {
- return true;
- } else {
- return false;
- }
- }
- public int size() {
- return size;
- }
- public void addFirst(E elem) throws DequeFullException {
- if (fullArray()) {
- throw new DequeFullException("Array is full");
- } array[first] = elem;
- first = (first + 1) % (capacity);
- size++;
- }
- //Fjerner ikke, Size-- ein plass inni her
- public E pullFirst() throws DequeEmptyException {
- if (emptyArray()) {
- throw new DequeEmptyException("Array is empty");
- }
- for (int i = 0; i < capacity; i++) {
- if (array[i] == null) {
- continue;
- } else {
- E temp = array[i];
- array[i] = null;
- for (int e=i; e< capacity; e++) {
- if (array[e] == null) {
- continue;
- } else {
- array[e - 1] = array[e];
- }
- }return temp;
- }
- }return null;
- }
- public E peekFirst() throws DequeEmptyException {
- if (emptyArray()){
- throw new DequeEmptyException("Array is empty");
- } else {
- return array[0];
- }
- }
- public void addLast(E elem) throws DequeFullException {
- if (fullArray()){
- throw new DequeFullException("Array is full");
- } array[last] = elem;
- last = (last - 1) % (capacity);
- size++;
- }
- //funker ikke
- public E pullLast() throws DequeEmptyException {
- if (emptyArray()) {
- throw new DequeEmptyException("Array is empty");
- } else {
- last = (last + 1) % (capacity);
- size--;
- return array[last];
- }
- }
- //funker ikke
- public E peekLast() throws DequeEmptyException{
- if (emptyArray()) {
- throw new DequeEmptyException("Array is empty");
- } else {
- return array[(last + 1) % capacity];
- }
- }
- /**
- * Metode som fjerner elementer i arrayet, blir brukt av clear().
- * @return resultate etter fjerningen.
- */
- //funker snart
- public void clear() {
- this.array = (E[]) new Object[capacity];
- /** last =...
- first = ...*/
- size=0;
- }
- //funker
- public boolean contains(Object elem){
- boolean found = false;
- int index = 0;
- while (!found && (index < array.length)) {
- if (elem.equals(array[index])){
- found = true;
- }
- index++;
- }
- return found;
- }
- @SuppressWarnings("unchecked")
- public <T> T[] toArray(T[] a) {
- return (T[]) Arrays.copyOf(array, array.length, a.getClass());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement