This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "minheap and iterator " Watch "minheap and iterator " New topic
Author

minheap and iterator

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
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:

Thanks a lot, Anders
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Hello!

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
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Anders, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/40953-iterator-inner-class-minheap.html


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

Joined: Mar 21, 2011
Posts: 45
Hello!

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: http://aspose.com/file-tools
 
subject: minheap and iterator
 
Similar Threads
MinHeap
comparator example needed
can anyone please help me spot my errors?
Why the need for two-step array initialization?