Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Array of Objects

 
Ranch Hand
Posts: 109
  • 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??
}
}
 
Ranch Hand
Posts: 152
  • 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.
 
Ranch Hand
Posts: 138
  • 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
 
Greenhorn
Posts: 9
  • 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
  • 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
 
Ranch Hand
Posts: 76
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me too why??
 
Ranch Hand
Posts: 809
  • 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
  • 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
 
Ranch Hand
Posts: 165
  • 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
  • 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
  • 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
  • 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
  • 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
 
Ranch Hand
Posts: 84
  • 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
  • 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
  • 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
 
Let nothing stop you! Not even this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic