• 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

Asymmetric Arrays

 
Author
Posts: 375
22
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic