# 2d arrays? (Object[ ][ ])

Page 1 of 1

## 7 Replies - 897 Views - Last Post: 27 August 2012 - 10:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290206&amp;s=f21e57addc9fbdabc8306dd5f7e6bad4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Courtney2012

Reputation: 0
• Posts: 50
• Joined: 16-March 12

# 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 08:49 PM

I cant figure out how these arrays work. I am trying to figure out how to make one of these arrays but dont know what does what.

new Object[][]

does the above mean that you have 2 arrays or does it mean that you have a set of arrays inside of the Object[][] array?
I thought it was a set of arrays almost like a chess board (Object[8][8], 8 lines each holding 8 squares) but I am becoming more and more confused as I try to figure it out.

So if I was to say...
Object[][] chessBoard = new Object[8][8];
chessBoard[2][4] = "Rook";
...Would that mean that the forth square of the second line now holds "Rook"?

Is This A Good Question/Topic? 0

## Replies To: 2d arrays? (Object[ ][ ])

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 09:03 PM

Except that the indices are 0-based so that [2][4] is actually referring to the 5th column of the 3rd row. I think your statement is correct.

### #3 Courtney2012

Reputation: 0
• Posts: 50
• Joined: 16-March 12

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 09:12 PM

thanks,

If I wanted to create an array with a pre-existing set of rows and columns what would the syntax be?

I have gotten this far...

array = new Object[][]{new String[]{"Date","Arrival","Departure","Regular Hours","Vacation Hours","Stat Holiday","Total Hours"},{???rows???}};

I cant figure out how to label each of the row numbers and also dont know if I am going about the columns right? could you please point me in the right direction?

### #4 ILoveJava

• D.I.C Regular

Reputation: 29
• Posts: 389
• Joined: 12-March 12

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 09:31 PM

### #5 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 09:48 PM

With experience you'll develop a style that works well for you. Especially when starting out, it's helpful to declare and initialize 2D arrays (or arrays of arrays) in a way that is easily visualized. For example, the following is okay:
```int[][] board = new int[][] {{1,0,0},{0,1,0},{1,2,1}};
```

But this might be even better:
```int[][] board = new int[][] { {1,0,0},
{0,1,0},
{1,2,1} };
```

Better (for some) because it's easier to see what's been built and make sense of it. We're all pretty familiar with 2 dimensions and can easily declare/initialize them in this 2D world so that we can easily visualize them, but it gets increasingly difficult as the number of dimensions increase.

This post has been edited by GregBrannon: 27 August 2012 - 09:50 PM

### #6 ILoveJava

• D.I.C Regular

Reputation: 29
• Posts: 389
• Joined: 12-March 12

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 10:01 PM

Sure, 2d arrays are easy to read & use. But classes are a lot neater.

### #7 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 10:13 PM

ILoveJava, on 28 August 2012 - 01:01 AM, said:

Sure, 2d arrays are easy to read & use. But classes are a lot neater.

If we were debating the relative merits of arrays versus classes, your convincing argument would be hard to beat.

### #8 ILoveJava

• D.I.C Regular

Reputation: 29
• Posts: 389
• Joined: 12-March 12

## Re: 2d arrays? (Object[ ][ ])

Posted 27 August 2012 - 10:17 PM

It's pretty convincing, right?