jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 2 dimension array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "2 dimension array " Watch "2 dimension array " New topic

2 dimension array

Bakul Bernard

Joined: Feb 03, 2007
Posts: 3
I am planning to give my SCJP 5 exam.
When i was trying to solve one of the questions i got stuck at this one:
public class CommandArgsThree {
public static void main(String [] args) {
String [][] argCopy = new String[2][2];
int x;
argCopy[0] = args;
x = argCopy[0].length;
for (int y = 0; y < x; y++) {
System.out.print(" " + argCopy[0][y]);

What should be the output if ran with :
java CommandArgsThree 1 2 3
I thought it should have thrown a runtime exception ArrayIndexOutOfBound as the array size is 2 by 2.
but it gives the output as 1 2 3
If I change the command to
java CommandArgsThree 1 2 3 4 5 6 7, it gives me 1 2 3 4 5 6 7 as the output.
Can you tell me as to how is this working so?

Thanks in advance,
Boini Naveen

Joined: Feb 05, 2007
Posts: 2
As your are storing the reference of the args into the array i.e
in argscopy[0] then your array is trying to refer to the command line argument i.e the String array that is why your dont get any exceptions and it tries to print all the values from the command line.
Bakul Bernard

Joined: Feb 03, 2007
Posts: 3
When I say String[2][2] it means i should not be allowed to access [0][2] or [0][3] which I can do when i pass 1 2 3 4 as the command line args.

Is it that at runtime the secound argument can have any length regardless of what it has during compile time?
I mean String[2][] and String[2][2] both equivalent at runtime with the variable length in the second bracket ?

Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Bakul, remember there are not multidimensional arrays in Java, they are handled as arrays of arrays.

String[][] a = new String[3][3]; is not a 2 dimensional array, it's an array of 3 elements and every one of those 3 elements has an array of another 3 elements associated with it.

In practice, most of the times you would treat an array of arrays as a multidimensional array. But the difference comes when arrays of arrays allow using them as sparse Matrices.

Sparse matrices are what you tried to simulate with an array in your example. This is also a sparse matrix:


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
marc weber

Joined: Aug 31, 2004
Posts: 11343

As Javier pointed out, "multi-dimensional arrays" in Java are really just arrays of arrays.

String[][] argCopy = new String[2][2];

This line creates a single-dimension array of length 2. But the elements of this array are also arrays. In particular, argCopy[0] and argCopy[1] both reference String arrays. At this point, these both reference arrays of length 2 (and these arrays contain null references).

But then you reassign the String array argCopy[0] to reference a different array...

argCopy[0] = args;

Consider this...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Bakul Bernard

Joined: Feb 03, 2007
Posts: 3
Thanks for clearing my doubt. The explanation is excellent.
Thanks once again.
I agree. Here's the link: http://aspose.com/file-tools
subject: 2 dimension array
It's not a secret anymore!