List,List<>,List< T>,List< E>和List< Object>之间的差 [英] Difference between List, List<?>, List<T>, List<E>, and List<Object>
问题描述
现在我不会盲目地问这个问题,所以请不要关闭这个线程。让我先介绍一下基本代码: 我明白: 2。 它仍然有效。 如果您可以解释此用法,我将非常感激。 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
。只会在投射不良时产生运行时错误。当演员阵容糟糕时,我们需要编译时错误。不建议使用。
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);
}
我想我不明白这个语法。我看到类似这样的东西,它可以工作: 请给我解释一下吗?有时,我看到 4。 然后我得到错误 编辑:如果我试试这个 然后我得到 <1>正确 2)您可以将其视为只读列表,您不关心物品的类型。例如由正在返回列表长度的方法使用。 3)T,E和U是相同的,但人们倾向于使用例如T表示类型,E表示元素,V表示键值,K表示键值。编译的方法说它需要一个特定类型的数组,并返回一个相同类型的数组。 4)你不能混合使用桔子和苹果。如果您可以将字符串列表传递给期望对象列表的方法,那么您可以将对象添加到字符串列表中。 (并非所有对象都是字符串) What are the differences between Now I do not blindly ask this question, so please don't close this thread. Let me first introduce the base code: I do understand that: 1. 2. it still works good. If you can explain the usage of this, I would greatly appreciate it. EDIT: If I do this: but if I change 3. I guess I don't understand this syntax. I saw something like this, and it works: Please explain this for me please? Sometimes, I see 4. Then I got the error EDIT: If I try this then I got 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<>,List< T>,List< E>和List< Object>之间的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<3> 列表< T> $ c
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>
。
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>
List
, List<?>
, List<T>
, List<E>
, and 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);
}
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.List<?>
: is an unbounded wildcard. But not sure what this is for? So if I change the test
method topublic static void test(List<?> set){
System.out.println(set);
}
public static void test(List<?> set){
set.add(new Long(2)); //--> Error
set.add("2"); //--> Error
System.out.println(set);
}
test
to this:public static void test(List<String> set){
set.add(new Long(2)); //--> Error
set.add("2"); //--> Work
System.out.println(set);
}
List<T>
: public static void test(List<T> set){ //T cannot be resolved
System.out.println(set);
}
public <T> T[] toArray(T[] a){
return a;
}
<T>
, or <E>
, or <U>
, <T,E>
. Are they all the same or do they represent something different?List<Object>
public static void test(List<Object> set){
System.out.println(set);
}
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);
}
test((List<Object>)names);
Cannot cast from List<String> to List<Object>