File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array of Objects" Watch "Array of Objects" New topic
Author

Array of Objects

saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
class Dims {
public static void main(String[] args) {
int[][] a = {{1,2,}, {3,4}};

Object[][] o1 = a;//why this illegal?

but

Object[]o1 = a;//legal??
}
}
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Remember in Java you don't have multidimesional arrays, you have arrays of arrays.

int[][] a = {{1,2,}, {3,4}}; // This is just an array, and every of it's elements is an array. But is is just and array.

Object[][] o1 = a; // You are trying to assing an array of arrays to an array. Different Types!!!

Object[]o1 = a; // You are trying to assing an array to an array. That's OK.


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
Can some one say how to distinguish between arrays of arrays and just arrays in the above case

Bye & Regards,
Praveen
Joel Arnold
Greenhorn

Joined: Jun 21, 2006
Posts: 9
Originally posted by saied ims:
class Dims {
public static void main(String[] args) {
int[][] a = {{1,2,}, {3,4}};

Object[][] o1 = a;//why this illegal?

but

Object[]o1 = a;//legal??
}
}


An int is not an Object, so you can't assign an array of array of ints to an array of array of Objects.

An array is an Object (Array extends Object), so you can assign an array of Arrays to an array of Objects.

Hope this is clear (too many arrays...)

Cheers !
--JA
Joel Arnold
Greenhorn

Joined: Jun 21, 2006
Posts: 9
Originally posted by Javier Sanchez Cerrillo:
Remember in Java you don't have multidimesional arrays, you have arrays of arrays.

int[][] a = {{1,2,}, {3,4}}; // This is just an array, and every of it's elements is an array. But is is just and array.

Object[][] o1 = a; // You are trying to assing an array of arrays to an array. Different Types!!!

Object[]o1 = a; // You are trying to assing an array to an array. That's OK.


a IS an array of array...

Cheers!
--JA
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

I don't fully understand this.


This is a 2 dimensional array of ints


This code won't compile because you are trying to assign a 2-D array of ints to a 2-D array of Objects.


This is the part I don't understand. Why is it legal to assign a 2-D int array to a 1-D Object array?

Thanks
saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
me too why??
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
This is the part I don't understand. Why is it legal to assign a 2-D int array to a 1-D Object array?



If p is a primitive type, then: Object > p[]. It means p[] array can be assigned to Object.

What if, p is 2-D primitive array and Object is an 1-D array. (Your case)

In that case we can say that Object[] > p[][]. So here again 2-D p array can be assigned to 1-D Object array.

Similary this also holds. Object [][] > p[][][]


Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Thanks Naseem,

I still don't understand this.

Every array, regarless of dimensions is essentially just an object so I can understand why it would be legal to assign an array of any dimension to an Object reference variable.

However, why would it make sense to assign a 2-D int array to a 1-D Object array? What would we be trying to achieve when doing this? Does the 1-D Object array become transformed into a 2-D int array?
How could we access the 2-D int array from the 1-D Object array again afterwards.

Sorry, lots of questions but I am really confused!!

Thanks
Kishore Balla
Ranch Hand

Joined: Jun 08, 2005
Posts: 165
Originally posted by d jones:

How could we access the 2-D int array from the 1-D Object array again afterwards.



In this case, the elements of the object array will be "int array references".

Check the Below Code



SCJP 5.0 : 88% My Story, SCWCD 1.4 : 94% My Story
kishoreballa.com
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by d jones:
However, why would it make sense to assign a 2-D int array to a 1-D Object array? What would we be trying to achieve when doing this? Does the 1-D Object array become transformed into a 2-D int array?


JLS says if S and T are both reference types, then S[] > T[] iff S > T. [Source: JLS 4.10.3 Subtyping among Array Types]

So you can assign T in S. This is legal S = T;

e.g.,



Now fit all these cases on S[] and T[].

1. Can you assign int[] to Object[]? Certainly No. as both are not reference types. One is array of primitive and other is of object references.

2. What about this? int[][] to Object[]. 1-D int array is an object. So
int[][] is nothing but T[] where T is reference of int[]. Now can you assign T[] to Object[]. Yes of course.

How could we access the 2-D int array from the 1-D Object array again afterwards.


Just go through this code.



Naseem
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76

1-D int array is an object. So
int[][] is nothing but T[] where T is reference of int[]. Now can you assign T[] to Object[].


What do you mean by this?

Could you recommend any extra reading about what is going on here. I really don't undersand this.

Is this relevant for SCJP 1.4?

Many Thanks
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

Can anyone help with this?

Many Thanks
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
From JLS 4.10.3, if S and T are both reference types, then S[] > T[] iff S > T. it means T can be assigned to S.

In the same way a 2-D primitive array (int[][]) can be assigned to 1-D object array as int[][] is nothing but a 1-D array of object references.

Naseem
vinod balaji
Ranch Hand

Joined: May 18, 2006
Posts: 84
class casting
{
public static void main(String args[])
{
int[][] a=new int[3][2];
int[] b=new int[2];
Object o;
b=a[2];
o=a[2]; // How this line works
o=a; // Even this line works
a=(int[][])o;
}
}
The above code works fine.
How can
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi Vinod,

Every array is an object.

So
o=a[2];
a[2] is a 1-D array which holds an array. Every array extends from Object so we can assign any array to an object reference.

a is a 2-D array, which is just an array of arrays. Again this extens from Object so we can assign the 2-D array to the object reference variable.
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Just to clarify this ....

Can somebody please correct me if I am wrong:

int[][] intArray={{2,3,4}, {10,11,12}};

Object[] objArray=intArray;

Here will the objArray have 2 elements, each holding a 1-D array

And in the case:
int[][][] intArray={{{1,2},{1,2},{1,2}}, {{1,2},{1,2},{1,2}}};
Object[] objArray=intArray;

Here will the objArray have 2 elements, each holding a 2-D array

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of Objects