• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2 dimension array

 
Bakul Bernard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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,
Bakul
 
Boini Naveen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

Thanks,
Bakul
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

1,2,3,4
1,
3,2,3,4,5,6
2,1,2
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Bakul Bernard
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing my doubt. The explanation is excellent.
Thanks once again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic