| Author |
Asymmetric Arrays
|
Mala Gupta
Author
Ranch Hand
Joined: Sep 27, 2002
Posts: 196
|
|
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 book OCA Java SE 7 Programmer I Certification Guide from Manning
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4355
|
|
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: 4355
|
|
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.
|
 |
 |
|
|
subject: Asymmetric Arrays
|
|
|