About Question enthuware.ocajp.i.v7.2.987 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
3 D arrays

About Question enthuware.ocajp.i.v7.2.987 :

Post by 3 D arrays »

Can somebody explain me 3 dimensional arrays please!
I didn't get how it will print null... Any link would be helpful...

Thanks in advance!

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

This explains it quite well.
If you like our products and services, please help us by posting your review here.

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by lordnovas »

Hey Admin,

Thanks for the link it explained a fare bit about multi-dimensional arrays however I'm still confused by the code in the question. I've been extremely challenged with translating the code in the question into plain English so I can understand it and make it my own. I read the descriptions/answers provided in the link but its very hard to conceptualize it all mentally. My top questions for the following code is as follows:

What's the length of arr[0] ? What I see is a total of 3 strings, which to me indicates that position arr[0] has a length of 3.

How do you know which array box you are in(Like are you in arr[0][1][2]) when looking at the initialization on the right side of the assignment operator?

If multi-arrays are just arrays within arrays then how do I know that I'm in an array within an array and its length, when I look at the code on the test I don't see where I would find an indication of that?

I read that a good trick is to count the brackets on the outside and make sure they match with the brackets on the inside, however there is like 3 brackets at the start and 2 brackets at the end?

What does the "," comma represent in-between the brackets in the initialization? I thought it meant that its declaring another Array but that would give the multi-array a length of 5.

What's the deal with the following code in the question
{ {"x"}, null }
Are you saying that x is it's own array and that "null" is it's own array making the total number of arrays within this little area 2? Also the code here
{ { "a", "b" , "c"}, { "d", "e", null } }
has equal number of brackets but the following code after it does not, does this mean I'm in the first box, second or third?

As you can see I'm terribly confused, no matter how much I read I just don't understand it, Please help?

Code: Select all

public class StringArrayTest{
 public static void main(String args[]){
      String[][][] arr  ={{ { "a", "b" , "c"}, { "d", "e", null } },{ {"x"}, null },{{"y"}},{ { "z","p"}, {} } };   System.out.println(arr[0][1][2]);   
 } }

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by lordnovas »

Just wanted to add that I've tried coding it and was successful, however I'm still at a loss as to what I'm doing

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

Try to format the array declaration so it will be easier to comprehend:

Code: Select all

{   <---- This is the top array arr
  {   <---- This is arr[0]
     {  <---- This is arr[0][0]
        "a", <---- This is arr[0][0][0]
        "b" , <---- This is arr[0][0][1] 
        "c" <---- This is arr[0][0][2]
     },  
     {  "d", "e", null }  <---- This is arr[0][1]
  },
//There are only two elements within arr[0], so arr[0].length = 2
  {  <---- This is arr[1]
     {"x"}, 
     null 
  },
  {
      {"y"}
  },
  { 
      { "z","p"},
      {} 
  }
}
You can also just copy paste the given program and put a print statement to print the length of the array element you want.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by lordnovas »

Hey Paul,

Thanks for breaking it down, I think I'm close to understanding this concept however I need just a bit more help.

So I've been going over how multi-Arrays work but I have a few more questions.

So if I was to do the following:

Code: Select all

String strMulti[][][] = {{{"A", "B", "C"}}};
for(int i =0;i<strMulti.length;i++){
     sout(strMulti.length);
}
//prints 1

Why does strMulti.length print the size of the multi-array as 1 instead of printing a length of 3?

I apologize in advance if my questions seem redundant but for some reasons multi-arrays just escape me and I'm determine to get it right before moving on.

Cheers,

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by lordnovas »

Figured it out!!!

This video http://youtu.be/ctab5xPv-Vkhelped a lot - I did not realize that the comma (,) indicated a new row and the elements within the row are the columns.

bashar
Posts: 3
Joined: Sun Sep 01, 2013 2:27 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by bashar »

hi sir

public class StringArrayTest{
public static void main(String args[]){
String[][][] arr ={{ { "a", "b" , "c"}, { "d", "e", null } },{ {"x"}, null },{{"y"}},{ { "z","p"}, {} } }; System.out.println(arr[0][1][2]);
} }

isn't [1]="x"

and

[2]=null

??
need explanation???

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

Bashar, first, point out what is arr[0], then when you get that array, point out what is [1] , and then apply [2].
If you like our products and services, please help us by posting your review here.

bashar
Posts: 3
Joined: Sun Sep 01, 2013 2:27 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by bashar »

hi sir

did you mean that arran no.[0]={ "a", "b" , "c"}, { "d", "e", null }

and array no.[1]={ "d", "e", null }

and the value of [2] points to null??

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

Please see my post on Fri Mar 08, 2013 11:30 am above. It shows the breakup of the array.
If you like our products and services, please help us by posting your review here.

Paulus
Posts: 5
Joined: Fri Jul 15, 2016 8:55 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by Paulus »

admin wrote:

Code: Select all

{   <---- This is the top array arr
  {   <---- This is arr[0]
     {  <---- This is arr[0][0]
        "a", <---- This is arr[0][0][0]
        "b" , <---- This is arr[0][0][1] 
        "c" <---- This is arr[0][0][2]
     },  
     {  "d", "e", null }  <---- This is arr[0][1]
  },
//There are only two elements within arr[0], so arr[0].length = 2
  {  <---- This is arr[1]
     {"x"}, 
     null 
  },
  {
      {"y"}
  },
  { 
      { "z","p"},
      {} 
  }
}
Hi admin,

I think it would be good to tell in the explanation that the first { and last } are the top array. They are used to initialize the array and do not represent the 0-level. I got confused until I read the above part and suddenly realized that the first parentheses represent the top array.

jsubirat
Posts: 3
Joined: Mon Jan 30, 2017 6:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by jsubirat »

Hi,

I have one question: would it have printed "null" if arr had been of type Integer[][][] and you tried to print arr[0][1][2] (also null)? I assume not, and that it actually printed "null" because it knew that the type of this reference was a String, so it called the toString() of the String class. Is that correct?

In other questions, System.out.println("" + null) simply concatenated "" with null, which was converted to String, I think.

Many thanks in advance,

Josep

jsubirat
Posts: 3
Joined: Mon Jan 30, 2017 6:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by jsubirat »

I just found the explanation in another question:

"Now, the other feature of print/println methods is that if they get null as input parameter, they print "null". They do not try to call toString() on null. So, if you have, Object o = null; System.out.println(o); will print null and will not throw a NullPointerException."

The thing is, why there is System.out.println(""+null) in many questions? What difference there is between System.out.println(""+null) and System.out.println(null)?

Many thanks!

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

What happened when you tried to compile System.out.println(""+null); and System.out.println(null)?
If you like our products and services, please help us by posting your review here.

jsubirat
Posts: 3
Joined: Mon Jan 30, 2017 6:54 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by jsubirat »

I get this:

Main.java:13: error: reference to println is ambiguous
System.out.println(null);
^
both method println(char[]) in PrintStream and method println(String) in PrintStream match

I suppose it's due that Java doesn't know which "type of null it is", so it can't determine whether to use the char[] or String overloaded method. But if I do...

String a = null;
System.out.println(a);

...it prints out "null". I suppose that now it knows it's a String null reference and that's why it works?

Thank you!

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

Very good. You got it :)
If you like our products and services, please help us by posting your review here.


Sieusc
Posts: 15
Joined: Mon Mar 02, 2020 3:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by Sieusc »

I too struggled with this excersise. In light of that I decided to first work it out in notepad and then try and execute the array access.

Code: Select all

String[][][] arr = {{{ "a", "b" , "c"}, { "d", "e", null } },{ {"x"}, null },{{"y"}},{ { "z","p"}, {} }

arr[0][0] = { "a", "b" , "c"}
arr[0][0][0] = a
arr[0][0][1] = b
arr[0][0][2] = c


arr[0][1] = { "d ", "e", null }
arr[0][1][0] = d
arr[0][1][1] = e
arr[0][1][2] = null


arr[1][0] = {"x"}
arr[1][0][0] = x

arr[1][1] = null

arr[2][0] = {"y"}
arr[2][0][0] = y

arr[3][0] = { "z","p"}
arr[3][0][0] = z
arr[3][0][1] = p

arr[3][1] = {}   -> empty (String?) array
arr[3][1][0] = this is an NPE 
The last part is interesting. As there are two null values in the array that system.out will print happily without problems. But when trying to print arr[3][1][0] it will give a NullPointer.

Paul could you shed some light into this for me please as to why the null values are printed but the last {} gives an NPE. Is it because it's trying to access an empty array, and in the case of null it's accessing a value wihin an existing array?

admin
Site Admin
Posts: 9116
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.987 :

Post by admin »

Printing a null references is not a problem but accessing a null reference is a problem. A null reference doesn't point to any object. So, there is nothing to access within a null. So, as soon as you do null.something (or a nullref.something ), a NPE is generated by the JVM.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests