Multidimensional Arrays + Me + OOP = Fear

node.java

class node{

int[][] grid =newint[0][0];

void detect(){

System.out.println(grid);

}

}

game.java

class game{

publicstaticvoid main(String args[]){

node a =new node();

a.detect();

}

}

The output of this code is:

http://img237.imageshack.us/img237/6556/outputxy7.png

I am trying to create an imaginary grid so I can implement A* algorithm into my game which will later be an applet.

Why did the output of this come out so weird? I do not understand multidimensional arrays and I struggle to understand object oriented programming.

I have known Java since Nov 2006, but I have always dodge some of this difficult stuff. I am totally self taught and I have been learning through the Internet and a very old Java 2 book. So please speak "dumb" to me.

Thank you,

Andrew.

[1467 byte] By [Aprza] at [2007-11-27 2:05:05]
# 1

Here is an example

int[][] myArray = new int[2][3];

for (int i=0; i < myArray.length; i++) {

for (int j=0; j < myArray[0].length; j++) {

System.out.println("i=" + i + ",j=" + j + ",element=" + myArray[i][j]);

}

}

i=0,j=0,element=0

i=0,j=1,element=0

i=0,j=2,element=0

i=1,j=0,element=0

i=1,j=1,element=0

i=1,j=2,element=0

rym82a at 2007-7-12 1:49:58 > top of Java-index,Java Essentials,Java Programming...
# 2

Well, for one thing given the output in that image, I'm not convinced that the code you posted would produce that output.

But generally that's what an array looks like if you try to print it like that. When you call println on an object (arrays are objects) it invokes the toString method on the object and prints the result. The default toString method isn't very interesting. It prints what you saw.

Basically you have to iterate through your arrays and print the values out yourself.

[add]

Too slow.

Yeah, like that.

Message was edited by:

paulcw

paulcwa at 2007-7-12 1:49:58 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks rym. I used the for-loop statement to create an imaginary 10 x 10 grid graph which worked fairly well. I did not think about using the for-loop earlier.

Paul, I know you mean well, but I am trying to learn here. Editing an image is as easy as editing the text I type so either way, it could have been altered. If you do not believe me, you probably should have tried out the code yourself which I did provide. I feel partially insulted.

Well, I am done with my question. Thanks. I am not sure if you guys close threads or not, but I am done here. Wont respond to anything else within this post.

Aprza at 2007-7-12 1:49:58 > top of Java-index,Java Essentials,Java Programming...
# 4

Oh, grow up. If you get insulted this easily, you'll be inconsolable when you have to get a real job. I'm so sick of these children who want help but also want their genius to remain unquestioned.

Anyway my point wasn't that you altered the image, but rather that you were careless in what you presented as data. Some of the stuff on that image couldn't possibly have worked with the code you posted. (In particular "java node" would not have even run. It wouldn't produce any output at all other than an error message.)

Part of debugging is gathering real, useful, accurate information. And if you want help debugging you have to share real, useful, and accurate information. If you gather output and then change your code, you should at the very least make this clear.

paulcwa at 2007-7-12 1:49:58 > top of Java-index,Java Essentials,Java Programming...