About Question enthuware.ocajp.i.v7.2.938 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.938 :
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.
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.
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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
HTH,
Paul
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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
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
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Fri Aug 08, 2014 11:22 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Thanks Paul, I get it now
Cheers
Cheers
-
- Posts: 4
- Joined: Sun Apr 12, 2015 10:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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?
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?
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Why do you think ia has three arrays?
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sun Apr 12, 2015 10:34 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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].
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].
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Jul 12, 2015 6:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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 [ ][ ][ ].)
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 [ ][ ][ ].)
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Jul 12, 2015 6:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Ok, thank you.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Does [] have the lowest operator precedence?
And () the highest?
And () the highest?
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Not really. Check this out: https://introcs.cs.princeton.edu/java/11precedence/
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Oct 14, 2020 1:10 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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
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
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
Your code is different from the one given in the question. You are missing the line var i = 4;
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Oct 14, 2020 1:10 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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.
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.
-
- Site Admin
- Posts: 10101
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.938 :
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!
int[] a = {1, 2, 3, 4}. Here, a.length is 4, not 1!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 21 guests