• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Popping from an overfilled MaxHeap. keep getting ArrayIndexOutOfBoundsException error

 
Ranch Hand
Posts: 50
1
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tasked to find the (K) largests numbers in an array of 50 million integers, the number range from 1-10 digits in length (0- 9,999,999,999)
there is a constraint to not use more than 500 Kilobytes of memory and identify the top 5000 numbers in under 1.75 seconds.
looking at performance programming i found that the best approach is to convert the array into a maxheap size (K) and as it is filled with ever larger number the smaller ones pop themselves out.
I got the max heap working so when I store the whole array in the heap i can find the top K numbers.

here is what I have so far;

Error message from cmd line:

Any help greatly appreciated. Thanks
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Starting backwards from your stacktrace...

Stan Austin wrote:


This line ...

says that the main() method will call a method at line 82 of the TestClass.java file.... which leads to this line...

which says that the extractMin() method, that was called from main() method will do an operation at line 41 of the TestClass.java file... that triggers this...

This is an array out of bounds index.... specifically, the code is trying to access the element at index -1. And since Java doesn't support negative indexes, it is clear why this is an error.  Anyway, going to line 41 of the TestClass.java file, you get this...

... and ... from this line, it is quite clear where the issue is. There are two array dereferences, but one of them is clearly index zero. The other is dependent on the position variable... and...

If you print out the position variable, I will easily bet that the value of the position at this point is zero...

Henry
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic