Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Asymmetric Arrays

 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 9929
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 9929
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic