File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multidimensional arrays 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 "Multidimensional arrays" Watch "Multidimensional arrays" New topic

Multidimensional arrays

Shweta R Dhaneshwar
Ranch Hand

Joined: May 27, 2005
Posts: 35
this example is from K&B
Given the following,

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

o/p is 123.

Doubt:argCopy is a 2d array.This array has two elements where each element is again a single dimensional array of two elements.This array is assigned to arguments passed at command line.the size of the first element of the array is 2,then how can we assign it 3 elements...i.e...can array size grow?

Shweta Dhaneshwar.<br />SCJP 1.4 90%
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
yes shewta array size can grow.

"There are no mistakes, only lessons"
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
No Aray size cannot grow.

You passed in 3 command line arguments so that Array has a size of 3.

With this line:

You replaced the Array at argCopy[0] with the args array which has a size of 3.

Java does not have true 2D Array's. What you have is an Array that holds other Arrays.

What you are doing is iterating over the args[].
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
This is easier to understand if you approach it gradually. Consider this code:

In your imagination, the array might be a box with three compartments (like a bookcase lying down), with one int in each compartment. Now consider:

It's tempting to picture this array as a box with three compartments, with a String in each compartment. But that's not exactly right. A String is an object, not a primitive, and in Java we always relate to objects via references. So the array's compartments actually contain references to Strings, rather than strings.

Ok, now for the "2D" case:
String[][] argCopy = new String[2][2];

This declares that argCopy is an array that contains two elements. Each is a reference to an array of strings. So argCopy[0] is a reference to an array of strings. (And that's just a convenient way of talking about it. We understand that really there's no such thing as an array of strings ... there's really an array containing references to strings.)

When the "argCopy[0] = args" statement executes, the reference in argCopy[0] is replaced by another reference. That new reference has to be of type String[], otherwise the code won't compile. But the new String array can be of any size. In the code you sited, the new size was 3.

Hope this helps.
-- Phil

Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
I agree. Here's the link:
subject: Multidimensional arrays
It's not a secret anymore!