• Post Reply Bookmark Topic Watch Topic
  • New Topic

The weirdest array index out of bounds error i have ever seen.  RSS feed

 
Christopher Diebold
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am trrying to implement Stressen's Algorithm for matrix multiplication and I have the weridest error when trying to pad my matrix with zeros. So in my padZeros method I am trying to create a bigger matrix to hold all the values of the next power of 2. SO a 5 x 5 matrix will do matrix multiplication on an 8 x 8 matrix. but when I go to set my matrix it will only iterate 4 times instead of 8 times which is werid because I print out the size of the matrix right before setting it as a debugging check. I dont know if this is maybe an inheritance thing or what. can someone please look at my code and help a fellow coder out? becasue I use inheritance I will post both classes but the error is in the StrassenMatrix.java and in the PadZero method. The comments explain the error. The error is on line 218 of the StrassenMatrix.java In case you dont know Strassens Algorithm here is a wiki link http://en.wikipedia.org/wiki/Strassen_algorithm

Matrix.java

StrassenMatrix.java
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an awful lot of code to read, so I'm not even going to try.

If you're getting an AIOOBE after 4 iterations on a length-8 array, then you're probably incrementing twice for each iteration. At the very least, you're incrementing more often of by a larger amount than you think.

So just before the line that's giving the exception, add a println() statement that shows you the length of the array and the index you're about to access. And then you may want to go add println()s everywhere you increment that index and print the before and after.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't just print getRowSize(). You need to print out all of these



I can't say I much like variables named X and ZZ either.
 
Christopher Diebold
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:That's an awful lot of code to read, so I'm not even going to try.

If you're getting an AIOOBE after 4 iterations on a length-8 array, then you're probably incrementing twice for each iteration. At the very least, you're incrementing more often of by a larger amount than you think.

So just before the line that's giving the exception, add a println() statement that shows you the length of the array and the index you're about to access. And then you may want to go add println()s everywhere you increment that index and print the before and after.


It is a lot. I understand. How about I just post the one method maybe 30 lines. Tell me if it looks right. Its just a double for loop.

>
 
Christopher Diebold
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Also, don't just print getRowSize(). You need to print out all of these



I can't say I much like variables named X and ZZ either.


ok i'll try it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Diebold wrote:Tell me if it looks right. Its just a double for loop.


Rather than asking a lazy, fallible human what's wrong, why not use print statements to let the computer tell you what the various values are?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Diebold wrote:It is a lot. I understand. How about I just post the one method maybe 30 lines. Tell me if it looks right. Its just a double for loop.

Erm..."Tell me if it looks right"?

Don't you know?

I'd never heard of "Stressen's algorithm" before I read this thread, but I can assure you, if I wrote a program (or method) to implement it, I'd know if it was right - and certainly if it "looked" right - because I'd know WHAT it was supposed to do.

I suspect strongly that you're too fixated on the mechanics of this method (and indeed, probably the program as a whole) - ie, HOW you're going to code it - rather than concentrating on WHAT it needs to do.

Don't worry; it's a common beginner's trap - even for smart guys (which I suspect you are) - but unless you're a Binar, "thinking in code" just doesn't work.

My advice: Read the StopCoding and WhatNotHow pages, and come back if you still have problems after following their advice.

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about I just post the one method maybe 30 lines. Tell me if it looks right. Its just a double for loop.

The only thing that does leap out at me is you have nested loops, both of which loop up to getRowSize(). Maybe one of them should loop up to something like getColumnSize().
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!