Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# Multi-d array

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
1int [] [] x = new int [2] [];

2x[0] = new int [2];
3x[0][0]=3;
4x[0][1]=7;

tells me ,
5line 1 makes a base array with elements -->0,1<--
6line 2 makes 2nd dimension array, orginating from the '0' element [line 5 in this mail ]
and the 2nd dimension array gets filled with values 3 and 7.

[I'm not going into the 2nd element of the base array, i.e line 1 ]

Now completely detaching myself from the above example , how shall I interpret something like :-

int [][] x = new int[2][2]; //...element wise

TIA
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Let me change your example just a little so that it's easier to discuss:

By making the dimensions different, it's a little easier to ensure we're all on the same page.

In this case, you've created an array with 2 elements. You can access those elements using x[0] or x[1]. Each of those elements contains an array of 3 elements. To get at the first element of each of those arrays, you could use x[0][0] or x[1][0].

I hope that helps.

SCJP Tipline, etc.
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
int [][] x = new int[2][2];
Here x is a two dimensional array. x can hold 2 arrays of length 2 in the positions x[0] and x[1].

x[0] can hold an int array of length 2.
x[1] can hold an int array of length 2.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Excellent stuff...!
naggy doubt cleared here !

and now back to the books
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
sorry for being back again so soon, after signing off so cool-ly

and the command-line invocation,
java CommandArgsThree 1 2 3
what is the result?

I'm not being able to interpret this code.
from intial feedbacks this is how I'm seeing the picture

____________
| 0 | 1 |
|____|_____|

____________
| 0 | 1 |
|____|_____|

____________
| 0 | 1 |
|____|_____|
argcopy

each element in "argcopy" contains an array of 2 elements as shown
above 'argcopy' [ could not draw the linking lines....too painful ]
considering x = argCopy[0].length; should be 3 , what is happening with

System.out.print(" " + argCopy[0][y]); // in terms of the boxes I have
drawn

I agree. Here's the link: http://aspose.com/file-tools

subject: Multi-d array