wood burning stoves 2.0*
The moose likes Associate Certification (OCAJP 7) and the fly likes Asymmetric Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Asymmetric Arrays" Watch "Asymmetric Arrays" New topic
Author

Asymmetric Arrays

Mala Gupta
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 248
    
    9

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


Author of Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

@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


When The Going Gets Tougher,The Tougher gets Going
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Asymmetric Arrays
 
Similar Threads
Values of Hash Map to be 2-dimensional array
2 dimension array
field declaration question
Simple problem
array question?