Wrote own indexOf

This commit is contained in:
Arnaud Vergnet 2020-03-25 17:28:21 +01:00
parent 1ea28ab806
commit 3d095626ab

View file

@ -135,10 +135,14 @@ public class BinaryHeap<E extends Comparable<E>> implements PriorityQueue<E> {
@Override
public void remove(E x) throws ElementNotFoundException {
int index = this.array.indexOf(x);
System.out.println(x);
System.out.println(index);
if (isEmpty() || index == -1 || index >= this.currentSize)
int index;
try {
index = this.indexOf(x);
} catch(EmptyPriorityQueueException e) {
index = -1;
}
if (index == -1)
throw new ElementNotFoundException(x);
else if (index == 0)
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
public E findMin() throws EmptyPriorityQueueException {
if (isEmpty())