Page 1 of 1

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

Posted: Mon Jun 18, 2012 10:51 pm
by ETS User
Hi,
I dont understand why the answer is number E.

wouldnt must be... ia[0].length + ", " + ia[0][0].length + ", " + ia[0][0][0].length ?
|| || ||
4 4 3

Thanks in advance.

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

Posted: Tue Jun 19, 2012 5:42 am
by admin
Because while initializing the second dimension, it assigns 3 to i first before creating that dimension. So ia[0][0].length is 3 instead of 4.

HTH,
Paul

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

Posted: Sat Sep 20, 2014 6:36 pm
by Daniel Clinton
Hello, I get the question and solution but
I don't quite follow the note at the end:

"Note that if evaluation of a dimension expression completes abruptly, no part of any dimension expression to its right will appear to have been evaluated."

Can't figure this bit out...
Can you tell me what an expression evaluation completing abruptly is?

Thanks v much
Danny
:)

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

Posted: Sat Sep 20, 2014 8:30 pm
by admin
completing abruptly means there was an error or exception while evaluating the expression. For example, let's say you have a line of code like this:
int value = arrayVariable[getIndex()];
Now, getIndex() method was supposed to return an integer value but instead of that it throws an exception. In that case, the array access evaluation will not succeed. In other words, it will complete abruptly.

HTH,
Paul.

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

Posted: Sun Sep 21, 2014 5:50 am
by Daniel Clinton
Thanks Paul, I get it now
Cheers

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

Posted: Wed Apr 15, 2015 3:11 pm
by Patrick75
I do not get this.

Given the following code (v8.2.938):
class Test {
public static void main(String[] args) {
int i = 4;
int ia[][][] = new int;
System.out.println(ia.length + ", " + ia[0].length + ia[0][0].length);
}
}

I would have thougth that 3, 4, 3 would have been printed instead of the valid answer: "4, 3, 3".
The variable ia has 3 arrays, so why is ia.length not 3?

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

Posted: Wed Apr 15, 2015 6:39 pm
by admin
Why do you think ia has three arrays?

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

Posted: Thu Apr 16, 2015 11:27 am
by Patrick75
Because of this line:
int ia[][][] = new int;

I see the [] 3 times.

*edit*
I thought the array lengths would be:
ia.length: 3
ia[0].length: 4
ia[0][0].length: 3
ia[0][0][0].length: 3

I think I see my mistake now, finally ;)
ia[0][0][0] does not exists and ia is the first array of course, there is only ia, ia[0] and ia[0][0].

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

Posted: Thu Apr 16, 2015 7:46 pm
by admin
Good. Another way to think about it is - int[] ia = { 1, 2, 3 }; There is only 1 set of [] in this. Does this mean ia.length is 1 or 3?

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

Posted: Wed Jul 15, 2015 4:31 am
by Roibeard
Hi,

I don't understand.


class Test{
public static void main(String[] args){
int i = 4;
int ia[][][] = new int[i = 3];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
Why does System.out.println return 4 for ia.length?
(it looks to me like it should be 3, because
ia has [ ][ ][ ].)

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

Posted: Wed Jul 15, 2015 4:38 am
by admin
You need to understand that length and dimensions are two different things. Sq Brackets denote dimensions while the number inside the bracket is the length of that dimension. I would suggest you to go through any basic tutorial on arrays before attempting these questions.
HTH,
Paul.

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

Posted: Thu Jul 16, 2015 4:20 am
by Roibeard
Ok, thank you.

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

Posted: Sun May 27, 2018 9:18 am
by flex567
Does [] have the lowest operator precedence?
And () the highest?

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

Posted: Mon May 28, 2018 8:22 pm
by admin

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

Posted: Sun Oct 18, 2020 5:46 am
by daveyj
I have gone through this question enthuware.ocajp.i.v8.2.938 and the comments, and I still couldn't get why ia.length is 4, after looking through books and online I'm just not getting it so I decided to write a program to display everything about a multi-dimensional array and it seems to only back up what I thought. Obviously your questions is correct, so can someone please tell me what I am missing? In my program the result of ia.length is 3 yet the only thing that has 3 is the declaration [][][]

class Test{
public static void main(String[] args){
String ia[][][] = { // ia has length 3 as it has 3 multi-dimensional arrays
{ // array ia[0] length 2, it has 2 arrays
{"1a","2a","3a","4a","5a","6a","7a"}, // array [0][0] length is 7, it has 7 elements
{"1b","2b","3b","4b","5b","6b","7b"} // array [0][1] length is 7, it has 7 elements
},
{ // array ia[1] length 4, it has 4 arrays
{"1c","2c","3c","4c","5c"}, // array [1][0] length is 5, it has 5 elements
{"1d","2d","3d","4d","5d"}, // array [1][1] length is 5, it has 5 elements
{"1e","2e","3e","4e","5e"}, // array [1][2] length is 5, it has 5 elements
{"1f","2f","3f","4f","5f"} // array [1][3] length is 5, it has 5 elements
},
{ //array is[2] length 2, it has 2 arrays
{"1g","2g"}, //array [2][0] length is 2, it has 2 elements
{"1h","2h"} //array [2][1] length is 2, it has 2 elements
}
};
System.out.println("ia.length = " + ia.length);
System.out.println("ia[0].length = " + ia[0].length);
System.out.println("ia[1].length = " + ia[1].length);
System.out.println("ia[2].length = " +ia[2].length);
System.out.println("ia[0][0].length = " + ia[0][0].length+", ia[0][1].length = "+ ia[0][1].length);
System.out.println("ia[1][0].length = " + ia[1][0].length+", ia[1][1].length = "+ ia[1][1].length+", ia[1][2].length = "+ ia[1][2].length);
System.out.println("ia[2][0].length = " + ia[2][0].length+", ia[2][1].length = "+ ia[2][1].length);
System.out.println( ia[0][0][0] + ia[0][0][1] + ia[0][0][5] + ia[0][0][6]);
System.out.println( ia[0][1][0] + ia[0][1][1] + ia[0][1][5] + ia[0][1][6]);
System.out.println( ia[1][0][0] + ia[0][0][1] + ia[1][0][3] + ia[0][0][4]);
System.out.println( ia[1][1][0] + ia[1][1][1] + ia[1][1][3] + ia[1][1][4]);
System.out.println( ia[1][2][0] + ia[1][2][1] + ia[1][2][3] + ia[1][2][4]);
System.out.println( ia[1][3][0] + ia[1][3][1] + ia[1][3][3] + ia[1][3][4]);
System.out.println( ia[2][0][0] + ia[2][0][1]);
System.out.println( ia[2][1][0] + ia[2][1][1]);
}
}

Results :
ia.length = 3
ia[0].length = 2
ia[1].length = 4
ia[2].length = 2
ia[0][0].length = 7, ia[0][1].length = 7
ia[1][0].length = 5, ia[1][1].length = 5, ia[1][2].length = 5
ia[2][0].length = 2, ia[2][1].length = 2
1a2a6a7a
1b2b6b7b
1c2a4c5a
1d2d4d5d
1e2e4e5e
1f2f4f5f
1g2g
1h2h

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

Posted: Sun Oct 18, 2020 8:51 am
by admin
Your code is different from the one given in the question. You are missing the line var i = 4;

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

Posted: Wed Oct 28, 2020 7:38 am
by daveyj
Hi admin,

Thank you for your reply. I know mine is different, I wasn't trying to replicate it exactly, but just to map out all the elements. What I don't understand is in my code ia.length is 3 which to my understanding is due to the face that it is a 3 dimensional array and the 3 arrays within it are of different lengths, 2, 4 & 2 as below, which is confirmed by the results of running my code.

Results :
ia.length = 3
ia[0].length = 2
ia[1].length = 4
ia[2].length = 2

But then the question is throwing me because I was expecting ia.length to be 3, regardless of what the value of i is equal to, my thinking is that ia.length should always be 3 because there are 3 [][][] and that it doesn't relate to the value of i. Obviously this is not the case but that seems to be what my code is showing, and I can't understand why.

Any help with understanding this would be greatly appreciated, my exam is scheduled for next week and I want to get my head around this before then.

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

Posted: Sat Oct 31, 2020 11:36 am
by admin
Dimensions is not the same as number of elements! Just because it is a three dimensional array doesn't mean its length is three. By that logic, do you think a two dimensional array will have only two elements and a one dimensional array will have only 1 element?
int[] a = {1, 2, 3, 4}. Here, a.length is 4, not 1!