Page 1 of 1

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

Posted: Tue Dec 11, 2012 4:20 pm
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!

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

Posted: Tue Dec 11, 2012 7:00 pm
by admin
This explains it quite well.

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

Posted: Thu Mar 07, 2013 10:56 pm
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]);   
 } }

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

Posted: Thu Mar 07, 2013 11:04 pm
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

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

Posted: Fri Mar 08, 2013 6:30 am
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.

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

Posted: Fri Mar 08, 2013 5:02 pm
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,

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

Posted: Fri Mar 08, 2013 6:06 pm
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.

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

Posted: Sun Sep 08, 2013 6:21 am
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???

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

Posted: Sun Sep 08, 2013 6:26 am
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].

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

Posted: Sun Sep 08, 2013 6:49 am
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??

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

Posted: Sun Sep 08, 2013 7:02 am
by admin
Please see my post on Fri Mar 08, 2013 11:30 am above. It shows the breakup of the array.

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

Posted: Mon Jul 25, 2016 5:05 am
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.

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

Posted: Wed Feb 01, 2017 5:25 pm
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

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

Posted: Wed Feb 01, 2017 6:53 pm
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!

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

Posted: Wed Feb 01, 2017 9:42 pm
by admin
What happened when you tried to compile System.out.println(""+null); and System.out.println(null)?

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

Posted: Thu Feb 02, 2017 4:20 am
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!

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

Posted: Thu Feb 02, 2017 6:32 am
by admin
Very good. You got it :)

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

Posted: Sun Mar 04, 2018 3:37 pm
by goplaniaakash14

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

Posted: Mon Jul 13, 2020 10:03 am
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?

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

Posted: Mon Jul 13, 2020 10:54 am
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.

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

Posted: Sun Jan 15, 2023 7:22 am
by asi-aal
Does java call toString() when printing a string variable? If yes, I would suggest that It could throw a NullPointerException when calling a toString on String variable (null)

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

Posted: Sun Jan 15, 2023 8:21 am
by admin
The print method does call toString on which ever object you pass to it eventually, but there is also null check before invoking toString. If the reference is null, then it print the string "null" instead of invoking toString() on null reference. That is why there is no NPE. You can take a look at the source code to confirm.