简体   繁体   English

List和有什么不一样 <Object[]> 和清单 <Object> ?

[英]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: 所以依次回答:

  1. 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

  2. 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在JavaJava泛型类型擦除中什么意思以及何时发生什么意思


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。

列表,列表之间的区别<!--?--> , 列表<t> , 列表<e> , 和列表<object><div id="text_translate"><p> <strong>List 、 List<?> 、 List<T> 、 List<E>和List<Object>之间有什么区别?</strong></p><h1> 1.清单</h1><p> List :是原始类型,因此不是typesafe的。 它只会在转换错误时产生运行时错误。 当转换错误时,我们希望出现编译时错误。 不推荐使用。</p><h1> 2.列表<?></h1><p> List<?>是一个无限通配符。 但我不确定它的用途是什么? 我可以毫无问题地打印一个List<?> :</p><pre> public static void test(List<?> list){ System.out.println(list); // Works }</pre><p> <strong>为什么我不能将项目添加到List<?> ?</strong></p><pre> public static void test(List<?> list){ list.add(new Long(2)); // Error list.add("2"); // Error System.out.println(list); }</pre><h1> 3.列表<T></h1><pre> public static void test(List<T> list){ // T cannot be resolved System.out.println(list); }</pre><p> 我不明白这种语法。 我看到了这样的东西,它有效:</p><pre> public <T> T[] toArray(T[] a){ return a; }</pre><p> <strong>有时,我会看到<T>或<E>或<U> 、 <T,E> 。</strong> <strong>它们都是一样的还是代表不同的东西?</strong></p><h1> 4. 列表<对象></h1><p> 这给出了错误“方法test(List<Object>)不适用于参数List<String> ”:</p><pre> public static void test(List<Object> list){ System.out.println(list); }</pre><p> 如果我尝试这个然后我得到“无法从List<String>投射到List<Object> ”:</p><pre> test((List<Object>) names);</pre><p> 我很迷惑。 <strong>String是Object的子类,那么为什么List<String>不是 List List<Object>的子类?</strong></p></div></object></e></t> - Difference between List, List<?>, List<T>, List<E>, and List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 List <Object>和List <?>之间有什么区别 - What's the difference between List<Object> and List<?> 列表之间的区别<Object>并列出 - Difference between List<Object> and List 列表,列表之间的区别<!--?--> , 列表<t> , 列表<e> , 和列表<object><div id="text_translate"><p> <strong>List 、 List<?> 、 List<T> 、 List<E>和List<Object>之间有什么区别?</strong></p><h1> 1.清单</h1><p> List :是原始类型,因此不是typesafe的。 它只会在转换错误时产生运行时错误。 当转换错误时,我们希望出现编译时错误。 不推荐使用。</p><h1> 2.列表<?></h1><p> List<?>是一个无限通配符。 但我不确定它的用途是什么? 我可以毫无问题地打印一个List<?> :</p><pre> public static void test(List<?> list){ System.out.println(list); // Works }</pre><p> <strong>为什么我不能将项目添加到List<?> ?</strong></p><pre> public static void test(List<?> list){ list.add(new Long(2)); // Error list.add("2"); // Error System.out.println(list); }</pre><h1> 3.列表<T></h1><pre> public static void test(List<T> list){ // T cannot be resolved System.out.println(list); }</pre><p> 我不明白这种语法。 我看到了这样的东西,它有效:</p><pre> public <T> T[] toArray(T[] a){ return a; }</pre><p> <strong>有时,我会看到<T>或<E>或<U> 、 <T,E> 。</strong> <strong>它们都是一样的还是代表不同的东西?</strong></p><h1> 4. 列表<对象></h1><p> 这给出了错误“方法test(List<Object>)不适用于参数List<String> ”:</p><pre> public static void test(List<Object> list){ System.out.println(list); }</pre><p> 如果我尝试这个然后我得到“无法从List<String>投射到List<Object> ”:</p><pre> test((List<Object>) names);</pre><p> 我很迷惑。 <strong>String是Object的子类,那么为什么List<String>不是 List List<Object>的子类?</strong></p></div></object></e></t> - Difference between List, List<?>, List<T>, List<E>, and List<Object> List和List之间的Java差异<Object> - Java difference between List and List<Object> 声明ArrayList对象和List对象在时间和空间复杂度上有什么区别? - What is the difference in time & space complexity between declaring an ArrayList object and a List object? 在LinkedList中,:list.add(…)和object.list.add(…)有什么区别? - In a LinkedList, what is the difference between : list.add(…) and object.list.add(…)? 集合和列表有什么区别? - What is the difference between Set and List? Groovy-List、ArrayList 和 Object Array 的区别 - Groovy- Difference between List, ArrayList and Object Array 我应该如何获得2个清单之间的区别<Object> - How should I get the difference between 2 list<Object> List 和 List 有什么区别<Integer[]>并列出<List<Integer> &gt; 在 Java 中 - What is the difference between List<Integer[]> and List<List<Integer>> in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM