Wrote own indexOf

Cette révision appartient à :
Arnaud Vergnet 2020-03-25 17:28:21 +01:00
Parent 1ea28ab806
révision 3d095626ab

Voir le fichier

@ -135,10 +135,14 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override @Override
public void remove(E x) throws ElementNotFoundException { public void remove(E x) throws ElementNotFoundException {
int index = this.array.indexOf(x); int index;
System.out.println(x); try {
System.out.println(index); index = this.indexOf(x);
if (isEmpty() || index == -1 || index >= this.currentSize) } catch(EmptyPriorityQueueException e) {
index = -1;
}
if (index == -1)
throw new ElementNotFoundException(x); throw new ElementNotFoundException(x);
else if (index == 0) else if (index == 0)
deleteMin(); deleteMin();
@ -155,6 +159,19 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
} }
} }
private int indexOf(E x) {
if(this.isEmpty())
throw new EmptyPriorityQueueException();
int index = -1;
for(int i = 0; i < this.currentSize; ++i) {
if(x.compareTo(this.array.get(i)) == 0) {
index = i;
break;
}
}
return index;
}
@Override @Override
public E findMin() throws EmptyPriorityQueueException { public E findMin() throws EmptyPriorityQueueException {
if (isEmpty()) if (isEmpty())