programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Multi-d array

Netty poestel
Ranch Hand
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
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.

Atul Chandran
Greenhorn
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
Posts: 131
Excellent stuff...!
naggy doubt cleared here !

and now back to the books

Netty poestel
Ranch Hand
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