Weird SonyEricsson J2ME matrix behaviour

Started by
2 comments, last by bornander 19 years, 6 months ago
Hi all, In my j2me game (mipd1.0) I'm writing a single integer to an array of integers declared as: public static int MATRIX_WIDTH = 8; public static int MATRIX_HEIGHT = 16; private int[][] data = new int[ MATRIX_HEIGHT ][ MATRIX_WIDTH ]; The method used for writing looks like this: public void writeData(int x, int y, int dataToWrite) { if ( x < 0 || x >= MATRIX_WIDTH ) return; if ( y < 0 || y >= MATRIX_HEIGHT ) return; data[ y ][ x ] = dataToWrite; } The weirdness is that sometimes more than just one value gets written to the array data. When debugging I checked how many values in the array there was that were greater than 0 and got (for example) 18, after the call to this method the same calculation return 29 instead of the expected 19. The behaviour exists on my Z600 as well as in the emulator. regards, /bornander
Advertisement
Don't see anything wrong with that code there.. maybe it's the code that's calling it which is causing problems? Maybe you're calling it with writeData(y, x, ...) instead of writeDate(x,y,...) ?

Also, I would recommend removing these two lines:
if ( x < 0 || x >= MATRIX_WIDTH )return;if ( y < 0 || y >= MATRIX_HEIGHT )return;

I don't know exactly how your program works, but most likely the code should not be trying to write to indices that are out of bounds. And if it IS trying to do that, then you *want* it to throw an error, so you can find out about it and fix it. So if you remove those two lines, you'll start getting very helpful ArrayIndexOutOfBounds exceptions if that's what's happening.
Ontop of removing the if statements i would also remove the function all together and just have

data[ y ][ x ] = dataToWrite;

inserted straight into your code. Remember you are working on small devices with limited memory and file size.

Other than that your code looks fine and should work. Maybe if you posted more of what you are trying to do.
Do not remove a fly from your friend's forehead with a hatchet.Chinese Proverb
Hi,

Thanks for the comments guys.

regards,
bornander

This topic is closed to new replies.

Advertisement