File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes array initialization confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "array initialization confusion" Watch "array initialization confusion" New topic
Author

array initialization confusion

Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
This is from JQ+. I must have a brain cramp because I can not follow the array initialization. Would someone walk through this example slooowly. To me this should give an array greater than 3 dimensions. I know it compiles and gives the correct answer. Thanks
Question ID :953843498130
What will be the result of attempting to run the following program?
public class StringArrayTest
{
public static void main(String args[ ])
{
String[][][] arr =
{
{ { "a", "b" , "c"}, { "d", "e", null } },
{ {"x"}, null },
{{"y"}},
{ { "z","p"}, {} }
};
System.out.println(arr[0][1][2]);
}
}
Tom Diamond
Ranch Hand

Joined: May 10, 2001
Posts: 98
Hi Bob.
I think I've solved your problem.
Just keep in mind that arr is an array of arrays of arrays.
The top line says that position 0 of the first (in depth) array will point to a second-depth array containing two third-depth arrays :: {"a", "b", "c"} and {"d", "e", null}.
In the same manner, next line says that position 1 of the first-depth array will point to a second-depth array containing two third-depth arrays :: {"x"} and null and so on.
Finally, the whole thing says that the first-depth array will have 4 places rather than initializing an array of a greater dimension.
I am sending you an e-mail with a .jpg file showing all the procedure because I do not know wether my English, above, were good enough :-)

Tom.
Veena Rani
Ranch Hand

Joined: Mar 09, 2000
Posts: 34
hi,

String[][][] arr =
{
{ { "a", "b" , "c"}, { "d", "e", null } },
{ {"x"}, null },
{{"y"}},
{ { "z","p"}, {} }
};
here array arr is an array of four different arrays
array 1(0th element of arr)
{ { "a", "b" , "c"}, { "d", "e", null }} is again an array of two arrays

array 2 (1st element) is array of two arrays
array 3 (2nd element) is array of one array
array 4 (3rd element) is array of two arrays
when you say arr[0][1][2] first arr[0] is evaluated which returns the array { { "a", "b" , "c"}, { "d", "e", null }}
now arr[0][1] means the the first element of this array
i.e. { "d", "e", null } this is again an array
arr[0][1][2] means second element of the above array
that is null.
it seems complicated but
in java if you think multidimensional array as an array of arrays
then it is more clear becuase every array in the main array(every element of the array ) can have different dimensions.
veena
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array initialization confusion
 
Similar Threads
Problem in Arrays passing.
What is the input file?
lazy initialization
fields of visibility
Arrays