Page 1 of 1

About Question enthuware.ocajp.i.v7. 2 .1380 :

Posted: Sun May 03, 2015 12:00 am
by subhamsdalmia
Dont understand the difference between,
{{0, 1}, {2, 3}, {4, 5}};
and
{ { {0, 1}, {2, 3}, {4, 5} } };

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

Posted: Sun May 03, 2015 1:43 am
by admin
To start with, did you notice the difference in number of opening and closing brackets?

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

Posted: Sun May 03, 2015 1:46 am
by subhamsdalmia
Yes, syntactically, they are different.
So, does that mean to separate dimensions in arrays, we require {}?

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

Posted: Sun May 03, 2015 2:51 am
by admin
Yes, try this - http://stackoverflow.com/questions/1067 ... ay-in-java

BTW, your question is too basic. You should first read a book to learn the basics and then attempt the mock exams. Otherwise, it will not help you much.

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

Posted: Tue May 05, 2015 1:27 am
by subhamsdalmia
kindly explain "int[][] arr2 = {arr, {1, 2}, arr};"
how is this valid?

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

Posted: Tue May 05, 2015 4:20 am
by admin
Again, it is quite basic. You need to go through a book and read a chapter on arrays.
It is not really possible to explain the whole chapter in a post here. Briefly, arr2 is a two dimensional array of ints, so each element of arr2 must be a one dimensional array of ints, which is what {arr, {1, 2}, arr} is doing.

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

Posted: Tue May 05, 2015 4:32 am
by subhamsdalmia
Understood.
Its array of array we are looking at.
We say its 2D array, and elements are 1D.
"," separates each element.
So, the above mentioned elements form only one part of aoa.
But, {1,2}, whats that?
AND
hows {arr, {1, 2}, arr} different from {{0, 1}, {2, 3}, {4, 5}}

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

Posted: Tue May 05, 2015 4:45 am
by admin
Please go through the link I gave you above. Read all the answers there.

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

Posted: Tue May 05, 2015 4:50 am
by subhamsdalmia
Have gone thru the link multiple times but haven't been able to understand hows {arr, {1, 2}, arr} different from {{0, 1}, {2, 3}, {4, 5}}!

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

Posted: Tue May 05, 2015 5:16 am
by admin
What kind of difference are you looking for?
In the initialization part of arr2, each element of arr2 has to be either a reference to a one dimensional array of ints or a definition of another one dimensional array itself. arr is a reference to a 1 dimensional array and {0, 1} is just another one dimensional array. So what is the problem?

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

Posted: Tue May 05, 2015 5:56 am
by subhamsdalmia
Then why is {{0, 1}, {2, 3}, {4, 5}} invalid
and {arr, {1, 2}, arr} valid?

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

Posted: Tue May 05, 2015 6:20 am
by admin
You need to read the explanation. It explains exactly the question that you are asking.
{{0, 1}, {2, 3}, {4, 5}} can't be assigned to array3D because {{0, 1}, {2, 3}, {4, 5}} is only a two dim array.

The statement int[][] arr2 = {arr, {1, 2}, arr}; is valid because {arr, {1, 2}, arr} is a two dim array and arr2 is variable of type 2d array of ints.

Re: About Question enthuware.ocajp.i.v7. 2 .1380 :

Posted: Tue May 05, 2015 6:33 am
by subhamsdalmia
Thanks and sorry to bother.
Now, I understand more.