int [] ia1 = {1,2,3};
int [][] ia2;
Object o = ia1;
ia2 = new int [3][3];
ia2[0] = (int[])o;
ia2[0][0] = (int[])o;

Answer is "Compilation fails in last line"

Explanation: Arrays are objects, and that each array dimension is a separate type. So, for instance, ia2 is of type "two dimensional int array", which is a different type than ia1. Last line attempts to assign a one-dimensional array into an int.

Doubt: Isn't the second last line also trying to assign a one dimensional int array to a two dimensional one?
I'm terribly confused in the last two lines only. Rest I understood.


4 Years
Discussion Span
Last Post by stultuske

This is not a site where you can market/advertise your stuff suhad12 by posting links irrelevant to the original posted content!!


ia2[0][0] = (int[])o;

here you are trying to store a one dimensional array of ints as single int.

ia2[0] = (int[])o;
here, ia2 is just one array, the one that is found

ia2 is a two dimensional array
ia2[0] is the first element of this two-dimensional array, being an one dimensional array itself
ia2[0][0] is the first element of the previous found one dimensional array, and is just a single int, not an array

Votes + Comments
Great got it stultuske. Thanks
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.