[英]What is the difference between List<Object[]> and List<Object>?
When a method is taking List of Object[] as input, even at the run-time the actual input is List of Object, it is still okay for the method. 当某个方法将Object []的列表作为输入时,即使在运行时实际输入的是Object的列表,该方法仍然可以使用。 Question is, for generics in Java, what does List<something>
really mean and what is the difference between List<Object[]>
and List<Object>
? 问题是,对于Java中的泛型来说, List<something>
真正含义是什么? List<Object[]>
和List<Object>
之间有什么区别?
/*
* if name is "A", then the input is a List<Object[]>
* if name is "B", then the input actually is a List<Object>
*/
public List<String> convert(List<Object[]> objectList, String name) {
List<String> resultList = new ArrayList<String>();
if (StringUtils.equals(name, "A")) {
for (Object[] object : objectList) {
String code = StringUtils.join((String) object[0], (String) object[1]);
resultList.add(code);
}
return resultList;
}
else if(StringUtils.equals(name, "B")) {
for (Object object : objectList) {
String code = (String) object;
resultList.add(code);
}
return resultList;
}
}
Actually during the run-time, this method works fine and takes both List<Object[]>
and List<Object>
. 实际上,在运行时,此方法可以正常工作,并且同时使用List<Object[]>
和List<Object>
。
Just wondering anyone could give some explanation of how this could work. 只是想知道任何人都可以对它如何工作进行一些解释。
So to answer in order: 所以依次回答:
For generics in Java, what does List<something>
really mean? 对于Java中的泛型, List<something>
真正含义是什么? The <>
bracket notation allows you to specify what specific type of a generic class you want. 使用<>
括号符号可以指定所需的泛型类的特定类型 。 Basically, a List
can be a List<A>
, List<B>
, etc.: its contents can be anything. 基本上, List
可以是List<A>
, List<B>
等:其内容可以是任何内容。 Writing List<Q>
says that it is a List
which contains things of type Q
. 写List<Q>
表示它是一个包含Q
类型内容的List
。
What is the difference between List<Object[]>
and List<Object>
? List<Object[]>
和List<Object>
什么区别? []
is the array operator. []
是数组运算符。 A List<Object[]>
is a list containing arrays of Object
. List<Object[]>
是包含Object
数组的列表。 A List<Object>
is a list containing Object
s. List<Object>
是包含Object
的列表。 In Java, almost everything is an Object
, including arrays! 在Java中, 几乎所有东西都是一个Object
,包括数组!
There's another thing to note here: Java generics are complicated (and interesting), and there's a thing called type erasure which means that, at run time, List<Object>
and List<XYZ>
both become List
- no specific type! 这里还有另一件事要注意:Java泛型很复杂(而且很有趣),并且有一种叫做类型擦除的东西,这意味着在运行时, List<Object>
和List<XYZ>
都成为List
没有特定的类型! The things in the brackets are only used at compile time - basically, to check that what you've written is correct. 括号中的内容仅在编译时使用-基本上是用来检查您编写的内容是否正确。 See What does angle brackets T mean in Java and Java generics type erasure when and what happens 请参阅尖括号T在Java和Java泛型类型擦除中什么意思以及何时发生什么意思
Why does your code run? 为什么您的代码运行? Well, in the "A"
case it's straightforward. 好吧,在"A"
情况下很简单。 At runtime, the parameter is just a List
, but your code expects to find Object[]
s in that list, and it does. 在运行时,该参数只是一个List
,但是您的代码希望在该列表中找到Object[]
,并且确实可以。 In the "B"
case it's more complicated: the parameter is just a List
, and you iterate over its contents treating them as Object
. 在"B"
情况下,情况更加复杂:参数只是一个List
,您遍历其内容将其视为Object
。 This is fine because as we said above (almost) everything is an Object
. 这很好,因为如上所述(几乎)所有内容都是Object
。 You then take those objects and cast them to String
- this is only weird if we don't take into account that at runtime the input List
is untyped. 然后,您将这些对象转换为String
-仅当我们不考虑在运行时输入List
未输入类型时,这才很奇怪。 As far as Java is concerned, there could be anything in that list, and you tell it that there are String
s. 就Java而言,该列表中可以包含任何内容,并且您告诉它存在String
。 Because the input was in fact a list of String
, it all works out. 因为输入实际上是String
的列表,所以一切都可以解决。
The interesting bit (which you didn't include in your code) is how you're calling a method that takes List<Object[]>
with a List<String>
without Java complaining at compile time. 有趣的一点(您的代码中未包括)是如何调用一种方法,该方法将List<Object[]>
与List<String>
而在编译时不会抱怨Java。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.