7 Replies - 565 Views - Last Post: 27 August 2012 - 10:17 PM Rate Topic: -----

#1 Courtney2012  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 Courtney2012  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 ILoveJava  Icon User is offline

  • D.I.C Regular

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

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

Posted 27 August 2012 - 09:31 PM

http://www.dreaminco...arallel-arrays/
Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 27 August 2012 - 10:13 PM

View PostILoveJava, 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.
Was This Post Helpful? 0
  • +
  • -

#8 ILoveJava  Icon User is offline

  • D.I.C Regular

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

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

Posted 27 August 2012 - 10:17 PM

It's pretty convincing, right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1