List,List<>,List< T>,List< E>和List< Object>之间的差 [英] Difference between List, List<?>, List<T>, List<E>, and List<Object>

查看:101
本文介绍了List,List<>,List< T>,List< E>和List< Object>之间的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List List <?>< / code>, List< T> List< E> List< Object>

现在我不会盲目地问这个问题,所以请不要关闭这个线程。让我先介绍一下基本代码:

  private static List< String> names = new ArrayList< String>(); 
static {
names.add(Tom);
names.add(Peter);
names.add(Michael);
names.add(Johnson);
names.add(Vlissides);


public static void test(List< String> set){
System.out.println(set);
}
public static void main(String args []){
test(names);
}

我明白:

<1> List :是一个原始类型,因此不是 typesafe 。只会在投射不良时产生运行时错误。当演员阵容糟糕时,我们需要编译时错误。不建议使用。



2。 List<?> :是一个无界通配符。但不知道这是什么?因此,如果我将 test 方法更改为

  public static void test List<> set){
System.out.println(set);
}

它仍然有效。 如果您可以解释此用法,我将非常感激。


$ b 编辑:如果我这样做:

  public static void test(List<?> set){
set.add(new Long(2 )); // - >错误
set.add(2); // - >错误
System.out.println(set);
}

但是如果我更改 test to this:

  public static void test(List< String> set){
set.add(new长(2)); // - >错误
set.add(2); // - >工作
System.out.println(set);
}



<3> 列表< T>
pre $ public static void test(List< T> set){// T无法解析
的System.out.println(组);
}

我想我不明白这个语法。我看到类似这样的东西,它可以工作:

  public< T> T [] toArray(T [] a){
return a;
}

请给我解释一下吗?有时,我看到< T> < E> < U> < T,E>

4。 List< Object> 它们都是相同的还是代表不同的东西?

  public static void test(List< Object> set){
System.out.println(set);
}

然后我得到错误方法测试(List< ;对象>)不适用于下面代码的参数List< String> 。我很困惑。我认为 String 是 Object的一个子集?

  public static void main(String args []){
test(names);

编辑:如果我试试这个

  test((List< Object>)names); 

然后我得到无法从List< String> to List< Object>

解决方案

<1>正确

2)您可以将其视为只读列表,您不关心物品的类型。例如由正在返回列表长度的方法使用。 3)T,E和U是相同的,但人们倾向于使用例如T表示类型,E表示元素,V表示键值,K表示键值。编译的方法说它需要一个特定类型的数组,并返回一个相同类型的数组。



4)你不能混合使用桔子和苹果。如果您可以将字符串列表传递给期望对象列表的方法,那么您可以将对象添加到字符串列表中。 (并非所有对象都是字符串)


What are the differences between List, List<?>, List<T>, List<E>, and List<Object>?

Now I do not blindly ask this question, so please don't close this thread. Let me first introduce the base code:

private static List<String> names = new ArrayList<String>();
static {
    names.add("Tom");
    names.add("Peter");
    names.add("Michael");
    names.add("Johnson");
    names.add("Vlissides");
}

public static void test(List<String> set){
    System.out.println(set);
}
public static void main(String args[]){
    test(names);
}

I do understand that:

1.List: is a raw type, therefore not typesafe. It will only generate a runtime error when the casting is bad. We want a compile time error when the cast is bad. Not recommended to use.

2.List<?>: is an unbounded wildcard. But not sure what this is for? So if I change the test method to

public static void test(List<?> set){
    System.out.println(set);
}

it still works good. If you can explain the usage of this, I would greatly appreciate it.

EDIT: If I do this:

public static void test(List<?> set){
    set.add(new Long(2)); //--> Error
    set.add("2");    //--> Error
    System.out.println(set);
}

but if I change test to this:

public static void test(List<String> set){
    set.add(new Long(2)); //--> Error
    set.add("2");    //--> Work
    System.out.println(set);
}

3.List<T>:

public static void test(List<T> set){   //T cannot be resolved
    System.out.println(set);
}

I guess I don't understand this syntax. I saw something like this, and it works:

public <T> T[] toArray(T[] a){
    return a;   
}

Please explain this for me please? Sometimes, I see <T>, or <E>, or <U>, <T,E>. Are they all the same or do they represent something different?

4.List<Object>

public static void test(List<Object> set){
    System.out.println(set);
}

Then I got the error The method test(List<Object>) is not application for the argument List<String> for the below code. I am confused. I thought String was a subset of Object?

public static void main(String args[]){
    test(names); 
}

EDIT: If I try this

test((List<Object>)names);

then I got Cannot cast from List<String> to List<Object>

解决方案

1) Correct

2) You can think of that one as "read only" list, where you don't care about the type of the items.Could e.g. be used by a method that is returning the length of the list.

3) T, E and U are the same, but people tend to use e.g. T for type, E for Element, V for value and K for key. The method that compiles says that it took an array of a certain type, and returns an array of the same type.

4) You can't mix oranges and apples. You would be able to add an Object to your String list if you could pass a string list to a method that expects object lists. (And not all objects are strings)

这篇关于List,List&lt;&gt;,List&lt; T&gt;,List&lt; E&gt;和List&lt; Object&gt;之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆