# Thread: Two dimensional array

1. Member
Join Date
Jun 2010
Posts
48
Rep Power
0

## Two dimensional array

Java 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?

2. 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.

3. I think you meant

Java 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

4. Member
Join Date
Jun 2010
Posts
48
Rep Power
0
every time I am stuck in the java pot, my brain goes to 100F...but thanks for pouring the cool water in!

5. Well seeing as the human body is 98.6F i dont think 100F will do too much harm ;)

Glad to help though
Berkeleybross

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•