Java 数组是同构的,而 ArrayLists 不是,这意味着什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36700490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean that Java arrays are homogeneous, but ArrayLists are not?
提问by lfk
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?
如果我们有一个 Type[],我们只能在其中存储 Type 或其子类型。ArrayList 也是如此。那么为什么说一个是同质的,而另一个则不是?
回答by M Anouti
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".
数组对添加元素的类型进行运行时检查。也就是说,如果添加了不属于同一类型的新元素,则ArrayStoreException
在运行时会抛出an 。这就是为什么他们被认为是“本土的”。
This is not true for ArrayList
s (List
s in general). Due to type erasure at runtime, it can practically hold any object.
这不适用于ArrayList
s(List
通常为s )。由于运行时的类型擦除,它实际上可以容纳任何对象。
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:
通过正确使用泛型,即在list
上面声明类型ArrayList<String>
而不是变量,ArrayList
可以在编译时避免该问题:
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
一个
1
回答by Mehraj Malik
Yes.Java
Arrays
are homogeneous,because when you declare any array in Java you have to declare its type
.
eg:
是的。Java
Arrays
是同构的,因为当你在 Java 中声明任何数组时,你必须声明它的type
. 例如:
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
.
但是ArrayList
是Collection
's 的一部分,他们持有Objects
,而不是任何特定的data-type
[如果我们不谈论generics
],并且因为 java 中的每件事都是直接或间接继承自Object
class
,所以它不会给你compile-time error,
类型检查将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.
现在你注意到了吗,因为我们还没有定义任何数据类型ArrayList al
,但是我们仍然存储不同类型的值:这就是为什么ArrayList
StoresObject
不是特定的数据类型,因此它们是异构的而不是同构的。