Hello!
I have a minheap class that is implemented as a priorityqueue and it works fine as far as to the iterator that I have to implement as a innerclass. But I can't get it to work properly.
Here is part of the code:
public class MinHeap<E> extends AbstractQueue<E> implements Queue<E> {
public HeapEntry<E>[] table;
private static final int CAPACITY = 10;
private int currentSize = 0;
Comparator<E> comparator = null;
@SuppressWarnings("unchecked")
public MinHeap() {
table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
}
@SuppressWarnings("unchecked")
public MinHeap(Comparator<E> comp) {
comparator = comp;
table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
}
In order to make the priority queue to work properly we implement this inner class
HeapEntry
public static class HeapEntry<E> {
private E element;
public int index;
public HeapEntry(E element) {
this.element = element;
}
public int getIndex(){
return index;
}
}
Here is my iterator class that I don't know if it's correct
private class MyListIterator implements Iterator<E>{
private HeapEntry<E>[]a;
private int pos;
private MyListIterator(){
pos = 0;
}
public boolean hasNext(){
if (pos < a.length)
return true;
else
return false;
}
public E next(){
if (this.hasNext())
return a[pos++].element;
else throw new NoSuchElementException();
}
public void remove(){
throw new UnsupportedOperationException();
}
}
Here is the iterator method in the outer class. There is of course many more methods but I don't think that I have to show them here. They work properly.
@Override
public Iterator<E> iterator() {
return new MyListIterator();
}
I do my testing in JUnit
posted Today 17:26:17 0
Hello!
I have a minheap class that is implemented as a priorityqueue and it works fine as far as to the iterator that I have to implement as a innerclass. But I can't get it to work properly.
Here is part of the code:
public class MinHeap<E> extends AbstractQueue<E> implements Queue<E> {
public HeapEntry<E>[] table;
private static final int CAPACITY = 10;
private int currentSize = 0;
Comparator<E> comparator = null;
@SuppressWarnings("unchecked")
public MinHeap() {
table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
}
@SuppressWarnings("unchecked")
public MinHeap(Comparator<E> comp) {
comparator = comp;
table = (HeapEntry<E>[]) new HeapEntry[CAPACITY];
}
In order to make the priority queue to work properly we implement this inner class HeapEntry
public static class HeapEntry<E> {
private E element;
public int index;
public HeapEntry(E element) {
this.element = element;
}
public int getIndex(){
return index;
}
}
Here is my iterator class that I don't know if it's correct
private class MyListIterator implements Iterator<E>{
private HeapEntry<E>[]a;
private int pos;
private MyListIterator(){
pos = 0;
}
public boolean hasNext(){
if (pos < a.length)
return true;
else
return false;
}
public E next(){
if (this.hasNext())
return a[pos++].element;
else throw new NoSuchElementException();
}
public void remove(){
throw new UnsupportedOperationException();
}
}
Here is the iterator method in the outer class. There is of course many more methods but I don't think that I have to show them here. They work properly.
@Override
public Iterator<E> iterator() {
return new MyListIterator();
}
I do my testing in JUnit
@Test
public final void testIterator() {
mh.offer(4);
mh.offer(1);
mh.offer(25);
mh.offer(14);
mh.offer(11);
Iterator<Integer> myItr = mh.iterator();
while (myItr.hasNext()){
Integer i = new Integer(1);
//HeapEntry<Integer>table[];
i = myItr.next();
mh.poll();
}
assertEquals("key not found in map: ", new Integer(1), mh.peek());
assertFalse("isEmpty true for non empty set", mh.isEmpty());
assertEquals("wrong size():", 4, mh.size());
}
But here I get a NullPointerException at while(myItr.hasNext()) Can anyone tell me what is wrong here?
Thanks a lot, Anders