开发者

Assign two dimensional string array to one dimensional object array

开发者 https://www.devze.com 2023-02-16 13:19 出处:网络
I get confused with this code String[][] str=new String[2][3]; 开发者_StackOverflow中文版Object[] obj=str;

I get confused with this code

String[][] str=new String[2][3];
开发者_StackOverflow中文版Object[] obj=str;

This compiles fine. because Object is a super class String is a sub class

but the following code produce errors

class Ex { } 
class Ex1 extends Ex { } 

class ExTest {
  public static void main(String[] args) 
  { 
    Ex1[][] ex1=new Ex1[2][3]; 
    Ex[] ex=ex1; 
  } 
} 

but this code produce error why


It compiles because obj will now hold two one-dimensional String[] objects.

All the below will compile:

String[][] str = new String[2][3];
Object obj1 = str;
Object[] obj2 = str;
Object[][] obj3 = str;


It makes sense when you realize that java arrays, no matter how many dimesions they have, are ... Objects in the terms of IS-A relationship. So array of objects can hold pretty much any array since every array is an Object.


All array instances are also instance of java.lang.Object. Therefore all below assignments are legal. Elements of objArr1 array are one dimensional String array.

     Object obj1 = new String[3];
     Object obj2 = new int[3];
     Object obj3 = new String[3][3];
     Object[] objArr1 = new String[3][4];

UPDATE :

class Ex { } 
class Ex1 extends Ex { } 
class ExTest {
 public static void main(String[] args) { 
    Ex1[][] ex1=new Ex1[2][3]; 
    Ex1[][][] exArr=new Ex1[2][3][3]; 
    Object obj = exArr; //valid
    Object objArr[] = ex1; // valid assignment. every array instances are also object
    Object[][] obj2Arr = exArr;  // valid 
    Object[][][] obj3Arr = exArr; //valid
    Obejct[][][][] obj4Arr = exArr; //compiler error. obj4Arr is 4 dimensional, whereas exArr is three..
    Ex[] ex=ex1;  // compiler error
    Ex1[] ex1Arr = ex1 ; // compiler error
  } 
} 

You can not assign different dimensional array to each other, even if their element types are the same.. You can assign any array with any dimension to an Object array whose dimension is less than or equal to assigned array dimension. Because every array instance is also Object instance..


First of all i want to highlight:

Object is the direct super class for all the array types

whether it is single dimension array or multi dimension

or array of String or array of any other derived type.

I get confused with this code

String[][] str=new String[2][3];
Object[] obj=str;

This compiles fine. because Object is a super class String is a sub class

here suppose we have

Object[] objArray = new Object[2];
objArray[0]= // it can hold something which is of type object
            // as we know String[] IS A Object, so I can assign it here like below
String[] strArray = new String[3];
objArray[0] = strArray ;

here I am assigning a single dimension String array as element of objArray, so we can say objArray can hold 2d String array i.e String[][] i.e.

Object[] objArray = new String[2][];

but the following code produce errors

 class Ex { } 
class Ex1 extends Ex { } 
class ExTest {
 public static void main(String[] args) 
{ Ex1[][] ex1=new Ex1[2][3]; Ex[] ex=ex1; 
} } 

but this code produce error why

it is because ex[0] can hold something which IS A Ex.

suppose if Ex[] ex=ex1; is allowed

then ex[0] should be able to hold 1D array of Ex1

ex[0] = new Ex1[3];// but this can not be possible as Ex1[] IS A Object NOT Ex
0

精彩评论

暂无评论...
验证码 换一张
取 消