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

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

Moderator: admin

Post Reply
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Ok, thank you.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Does [] have the lowest operator precedence?
And () the highest?

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

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

Post by admin »

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

daveyj
Posts: 2
Joined: Wed Oct 14, 2020 1:10 pm
Contact:

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

Post 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

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

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

Post by admin »

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.

daveyj
Posts: 2
Joined: Wed Oct 14, 2020 1:10 pm
Contact:

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

Post 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.

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

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

Post 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!
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 51 guests