Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

minheap and iterator

 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anders, please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/40953-iterator-inner-class-minheap.html
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic