What does it mean that Java arrays are homogeneous, but ArrayLists are not?

杀马特。学长 韩版系。学妹 提交于 2019-12-19 05:22:27

问题


If we have a Type[], we can only store Type or its subtypes in it. The same goes for ArrayList. So why is it said that one is homogeneous while the other is not?


回答1:


Arrays have a runtime check on the type of the added element. That is, if a new element that is not of the same type is added, an ArrayStoreException is thrown at runtime. That's why they are considered as "homegeneous".

This is not true for ArrayLists (Lists in general). Due to type erasure at runtime, it can practically hold any object.

The following throws an exception when running:

Object[] array = new String[3];
array[0] = "a";
array[1] = 1;   // throws java.lang.ArrayStoreException

unlike the following which compiles and runs without problem (although with a compiler warning as it doesn't properly use generics):

ArrayList list = new ArrayList<String>();
list.add("a");
list.add(1);    // OK
list.add(new Object());  // OK

With a correct use of generics, i.e. declaring the variable list above of type ArrayList<String> instead of ArrayList, the problem is avoided at compile-time:

ArrayList<String> list = new ArrayList<String>();
list.add("a");
list.add(1);  // compilation error
list.add(new Object());  // compilation error

But even with a generically declared list, you can have something like this work without an exception at runtime:

ArrayList<String> list = new ArrayList<String>();
list.add("a");
Method[] methods = List.class.getMethods();
for(Method m : methods) {
    if(m.getName().equals("add")) {
        m.invoke(list, 1);
        break;
    }
}
System.out.println(list.get(0));
System.out.println((Object) list.get(1));

Output:

a

1




回答2:


Yes.Java Arrays are homogeneous,because when you declare any array in Java you have to declare its type. eg:

int arr[]; //type is int
    String arr[]; //type is String
    float arr[]; //type is float

now if you try to store any other data-type in declared array,it will be a compile time error. eg:

 int arr=new int[5];
     arr[0]="I am a String not int"; //compile time error

but ArrayList are the Collection's part,they hold Objects,instead of any specific data-type[if we are not talking about generics],and because every thing in java is directly or indirectly inherited from Object class,so It will not give you compile-time error,type checking will be on run-time.

eg:

 ArrayList al=new ArrayList();//type is Object
    al.add("I am a String");  //Bacause String class in inherited from Object Class
    al.add(1);//the int 1 will first autobox into Integer class then stored in al ArrayList.Now bacause Integer class is also inherited from Object class,it will*/ allow you to store
    al.add(UserDefinedClass); //because every User defined class is also inherited from Object class,so it will also allow you.

Now did you notice,because we have not defined any data type of ArrayList al,but still we are storing different type values: this is know why ArrayList Stores Object not specific data-type,thus they are heterogeneous not homogeneous.



来源:https://stackoverflow.com/questions/36700490/what-does-it-mean-that-java-arrays-are-homogeneous-but-arraylists-are-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!