File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes minheap and iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "minheap and iterator " Watch "minheap and iterator " New topic

minheap and iterator

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45

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:

Thanks a lot, Anders
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Hi Anders,

Welcome to The Ranch!

Your post is much easier to read if you UseCodeTags. I've added them this time.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I see two things very wrong with the iterator right off the bat. One is that "a" is never initialized to point to anything. It will always be null, so your iterator will fail trying to access its elements with a NullPointerException.

The other one is that if this is a heap-based container, then the iterator should be calling fixup/fixdown methods as it extracts the elements, to reform the heap -- either the original (if iteration is destructive) or a copy (if it's not).

You don't really tell us where things fail, so I don't know which of these things you're worried about -- or perhaps there are even others.

[Jess in Action][AskingGoodQuestions]
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45

Thank you for answering and I will add code tags this time. Hopefully it works.

Back to my problem: Yes, it is correct that a is null. I do get NPE in the hasNext method and myListIterator gives an NPE. And now I understand that a is never initialized. But how do I do that?
Eclipse doesn't take this:

Eclipse says: cannot create a generic array out of MinHeap.HeapEntry<E>.

Do you have any clue ?

Thanks for taking your time!
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Anders, please BeForthrightWhenCrossPostingToOtherSites

luck, db
There are no new questions, but there may be new answers.
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45

Sorry about that. I didn't know about the rules. Next time I will tell you if I've posted my problem at another site. B ut anyway. Thank you for the help. I managed to solve the problem.

I had an array []a in my MyListIterarator class. It shouldn't be there at all.

Thank you again.
I agree. Here's the link:
subject: minheap and iterator
jQuery in Action, 3rd edition