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
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Asymmetric Arrays

Author
Posts: 375
22
• 1
• Number of slices to send:
Optional 'thank-you' note:

Questions on asymmetric arrays can get tricky in the exam. Here are two simple ways to determine what an array element stores. Let's use the following asymmetric array for an example:

First approach: Indent the code to make it more readable:

Second approach: Represent the array using an image:

Now, can you determine the output of each of these lines of code?

With respect,
Mala

Sheriff
Posts: 11604
178
• 1
• Number of slices to send:
Optional 'thank-you' note:

Mala Gupta wrote:Now, can you determine the output of each of these lines of code?

Yes, I can. But I won't spoil the fun for others

And just for completeness, these lines are all equivalent:

String multiStrArr[][] = new String[][]{{"A", "B"},null,{"Jan", "Feb", null}};
String[] multiStrArr[] = new String[][]{{"A", "B"},null,{"Jan", "Feb", null}};
String []multiStrArr[] = new String[][]{{"A", "B"},null,{"Jan", "Feb", null}};
String[][] multiStrArr = new String[][]{{"A", "B"},null,{"Jan", "Feb", null}};
String multiStrArr[][] = {{"A", "B"},null,{"Jan", "Feb", null}};
String[] multiStrArr[] = {{"A", "B"},null,{"Jan", "Feb", null}};
String []multiStrArr[] = {{"A", "B"},null,{"Jan", "Feb", null}};
String[][] multiStrArr = {{"A", "B"},null,{"Jan", "Feb", null}};

Another (important) note: the brackets identify the array type and should appear with the type designation, so it's preferred to use String[][] multiStrArr, but the other forms (e.g. String[] multiStrArr[]) will also definitely appear on the exam. Don't let them fool you

Ranch Hand
Posts: 598
• Number of slices to send:
Optional 'thank-you' note:
@Mala,
Could you please tell me how to print A,B,Jan,Feb,March separately which you are displaying in the image as you have explained.

Do the below print A,B,Jan,Feb,March respectively.Please suggest :-)

System.out.println(multiStrArr[0][0]); // line1
System.out.println(multiStrArr[1][2]); // line2
System.out.println(multiStrArr[1]); // line3
System.out.println(multiStrArr[1].toString()); // line4
System.out.println(multiStrArr[2].toString()); // line5

Roel De Nijs
Sheriff
Posts: 11604
178
• Number of slices to send:
Optional 'thank-you' note:

Deepak Lal wrote:Could you please tell me how to print A,B,Jan,Feb,March separately which you are displaying in the image as you have explained.

JavaRanch is NotACodeMill. Why don't you write a little program to try to print these values seperately? It's a much better learning experience than Mala (or me) providing you the code.

Deepak Lal wrote:Do the below print A,B,Jan,Feb,March respectively.Please suggest :-)

System.out.println(multiStrArr[0][0]); // line1
System.out.println(multiStrArr[1][2]); // line2
System.out.println(multiStrArr[1]); // line3
System.out.println(multiStrArr[1].toString()); // line4
System.out.println(multiStrArr[2].toString()); // line5

Definitely not! Please ShowSomeEffort! Mala made a nice graphical drawing of the array which makes it not that hard to determine the output of these lines. And keep in mind that on the actual exam you'll be on your own, there won't be someone to suggest the correct answers to you.

 Not so fast naughty spawn! I want you to know about the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising