File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array of Objects

 
saied ims
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Praveen Babu
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one say how to distinguish between arrays of arrays and just arrays in the above case

Bye & Regards,
Praveen
 
Joel Arnold
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too why??
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
d jones
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone help with this?

Many Thanks
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic