# Two dimensional array

• 06-12-2010, 10:26 PM
niu_niu
Two dimensional array
Code:

char[ ][ ] arrayTwo;
arrayTwo = new char [1][ ];
arrayTwo[0] = new char [3];
arrayTwo[1] = new char [4];

arrayTwo[0][2] ='z';

I guess this is a simple question but I fail to find the answers in my books.

I want to know, what "arrayTwo[0] = new char [3]" means. So I declared a two dimensional array called "ArrayTwo" which contains two rows. But by declaring "arrayTwo[0] = new char [3]", does it mean that "the first row will have 3 columns"??? Or does it mean something else?
• 06-12-2010, 10:35 PM
j2me64
Quote:

Originally Posted by niu_niu
I want to know, what "arrayTwo[0] = new char [3]" means. So I declared a two dimensional array called "ArrayTwo" which contains two rows. But by declaring "arrayTwo[0] = new char [3]", does it mean that "the first row will have 3 columns"???

exactly. this way you can declare a different number of elements/columns per row.
• 06-13-2010, 12:12 AM
berkeleybross
I think you meant

Code:

char[ ][ ] arrayTwo;
arrayTwo = new char [2][ ];
arrayTwo[0] = new char [3];
arrayTwo[1] = new char [4];

arrayTwo[0][2] ='z';

You get a null pointer with that code because you are trying to assign to row two but you only made one row.

And yes, as j2me64 says, you can declare different array lengths for a 2d array. You can even do it for 3d arrays if you want - although thats will start to hurt your brain.

Berkeleybross
• 06-13-2010, 12:20 AM
niu_niu
every time I am stuck in the java pot, my brain goes to 100F...but thanks for pouring the cool water in!
• 06-13-2010, 12:34 AM
berkeleybross
Well seeing as the human body is 98.6F i dont think 100F will do too much harm ;)