Results 1 to 6 of 6
Thread: iterator inner class in minheap
- 03-21-2011, 04:51 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
iterator inner class in minheap
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
- 03-22-2011, 05:25 AM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
This is because "myItr" is null. This can be caused due to "mh".
What is "mh"? Where is it declared?
Maybe you didn't post this. If so, check what [return new MyListIterator();] is actually returning.
- 03-22-2011, 05:39 AM #3
Looks like the NPE is actually in the hasNext method since the array a is never created.
- 03-22-2011, 10:19 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Hello, thanks for answering.
mh is declared in the JUnit class:
public class testMinHeap extends TestCase {
MinHeap<Integer>mh;
protected void setUp() throws Exception {
super.setUp();
mh = new MinHeap<Integer>();
}
And it is as you say that the NPE is in the hasNext method and myListIterator is returning an Exception. I am running the debugger and when I'm coming down to the the iterator method which is supposed to return the myListIterator object it becomes an exception.
There is propably something wrong with my declarations. MyListIterator is An inner class. Is it something here?
Junky says that array a is never created but should I create a new array?
Eclipse doesn't take this:
private HeapEntry<E>[]a;
private int pos;
public MyListIterator(){
pos = 0;
a = new HeapEntry<E>[];
}
Eclipse says: cannot create a generic array out of MinHeap.HeapEntry<E>.
Do you have any clue ?
Thanks for taking you time!
- 03-22-2011, 11:06 AM #5
Cross posted
minheap and iterator (Java in General forum at JavaRanch)
db
- 03-24-2011, 02:52 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Iterator
By Dayanand in forum New To JavaReplies: 2Last Post: 03-10-2011, 12:17 PM -
for..iterator
By jon80 in forum New To JavaReplies: 2Last Post: 11-28-2010, 02:12 PM -
Iterator next import class?
By daydreamer in forum New To JavaReplies: 7Last Post: 10-04-2010, 11:32 PM -
Iterator as return and argument or Iterator-String(not visible in the test file)
By Aldarius in forum New To JavaReplies: 0Last Post: 05-18-2010, 12:53 AM -
Iterator
By eva in forum New To JavaReplies: 0Last Post: 01-31-2008, 02:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks