• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array initialization confusion

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
}
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic