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

Recommended Answers

All 3 Replies

declare and initalize variable for mh.offer(4);

Thank you for answering,

but how do I declare and initalize this varaible for mh.offer(4)?

offer is boolean and MyListIterator is an inner class

Eclipse doesn't like this:

MinHeap.MyListIterator myI = mh.new MyListIterator();
myI = mh.offer(4);

Here eclipse says that I have to change type of myI to boolean. Obviously since offer is boolean. But if I change myI to boolean like this:

boolean myI = mh.new MyListIterator();

It doesn't work either. I can't figure it out!?

Do you have any idea?

Thanks a lot for taking your time

I managed to solve it. I shouldn't have declared array a[] in the MyIterator class. So I just took it away.
This is the final version:

public class MyListIterator implements Iterator<E>{
    private int pos;
    public MyListIterator(){
        pos = 0;
    }
    public boolean hasNext(){
        if (pos < currentSize)   
            return true;    
        else    
            return false;
    }
    public E next(){
        if (this.hasNext()) 
            return table[pos++].element;    
        else throw new NoSuchElementException();
    }
    public void remove(){
        throw new UnsupportedOperationException();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.