This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# doubt in arrays...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
SOURCE:www.danchisholm.net

output is:112...

i have not understand what is happened at line2?

can anyone explain me?

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9435

20

at line 2, you are creating a clone of the array. then you assign that clone to an Object[]. As you might know, that int[] and every other primitive array is a sub-class of Object. But since this is a 2-D primitive array, so you are able to assign to an Object[]. You can think of a 1-D int array as an Object. So a 2-d int array as an array of 1-d int array. This is why the assignment is allowed...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
ok but which values can be assigned to 1-d Object array?

how values in 2-d array can b converted in to 1-d array?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9435

20

ok but which values can be assigned to 1-d Object array?

1-d object array can store any type of an array except 1-d primitive arrays.

how values in 2-d array can b converted in to 1-d array?

values in 2-d array cannot be converted into 1-d array (at least directly without using any loop or something). I think you are confused here. The 2-d array converted into a 1-d array because we changed the type. We did not assign a 2-d int array into 1-d int array. We assigned a 2-d int array into a 1-d object array...
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
sry not cleared...

can you tell me the values in 1-d object array ?

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9435

20

the object array here contains 1-d int arrays...
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
Here I have added extra displays to show what 'a' and 'obj' really look like inside....they are very similar.

So obj is really an array of 3 arrays.

The for loop executes 3 times.
Line 4 serves up the 1st, 2nd, + 3rd array from inside of obj.
Line 5 serves up 1st, 2nd, + 3rd value from the array produced in line 4.

Does this help?

int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
System.out.println("a = " + a.length); // 1.5
System.out.println("a = " + Arrays.toString(a[0])); // 1.6
System.out.println("a = " + Arrays.toString(a[1])); // 1.7
System.out.println("a = " + Arrays.toString(a[2])); // 1.8
Object[] obj = (Object[])a.clone(); // 2
System.out.println("obj = " + obj.length); // 2.5
System.out.println("obj = " + Arrays.toString((int[])obj[0])); // 2.6
System.out.println("obj = " + Arrays.toString((int[])obj[1])); // 2.7
System.out.println("obj = " + Arrays.toString((int[])obj[2])); // 2.8
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 5
}

RESULT:

a = 3
a = [1, 2]
a = [0, 1, 2]
a = [-1, 0, 2]
obj = 3
obj = [1, 2]
obj = [0, 1, 2]
obj = [-1, 0, 2]
112

[ December 06, 2008: Message edited by: patrick avery ]
[ December 06, 2008: Message edited by: patrick avery ]

SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Patrick and Ankit I found your explanation very helpful. But I don't understand why the output is 112. Could you please explain where that is coming from?
Thanks,
Meera
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Thanks Ankit and Patrick. I have been getting to understand the multidimensional array's topic.

Naveen Katoch
Greenhorn

Joined: Dec 07, 2008
Posts: 25
Thanks ankit and patrick....it really helped me out ..

Regards,<br />Satnam(scjp 5-94%)
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Originally posted by meera kanekal:
Patrick and Ankit I found your explanation very helpful. But I don't understand why the output is 112. Could you please explain where that is coming from?
Thanks,
Meera

Because, i doesn't remain constant. It changes from 0 to 1 to 2 with each iteration of the for loop. Therefore, obj[0] = 1, obj[1] = 1 and obj[2] = 2.

Does this make sense?

SCJP 1.4 85%
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
So Prateek am I correct in saying that
1 --> 0th element of ia[0] = [1,2]
1 --> 1st element of ia[1] = [0,1,2]
2 --> 2nd element of ia[2] = [-1,0,2]
Thanks,
Meera
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Yes. Basically, obj[i] contains the same element as ia[i] except for the fact that it needs to be cast to an int[].

I agree. Here's the link: http://aspose.com/file-tools

subject: doubt in arrays...