java 将 List<String> 传递给需要 List<Object> 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17143239/
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
passing List<String> to method expecting List<Object>
提问by damon
In Page 112 of CHAPTER 5 GENERICS in the book - Effective Java
, these sentences appears
在Page 112 of CHAPTER 5 GENERICS in the book - Effective Java
,这些句子出现
Just what is the difference between the raw type
List
and the parameterized typeList<Object>
...While you can pass aList<String>
to a parameter of type List, you can't pass it to a parameter of typeList<Object>
原始类型
List
和参数化类型之间有什么区别List<Object>
...虽然您可以将 a 传递List<String>
给 List 类型的参数,但不能将其传递给类型的参数List<Object>
I tried this
我试过这个
public static void getMeListOfObjs(List<Object> al){
System.out.println(al.get(0));
}
public static void main(String[] args) {
List<Object> al = new ArrayList<Object>();
String mys1 = "jon";
al.add(mys1);
getMeListOfObjs(al);
}
It runs without any error... Was that an error in book content? I am quoting from the second edition
它运行没有任何错误......这是书内容中的错误吗?我引用的是第二版
回答by Quirin
Try this:
试试这个:
public static void getMeListOfObjs(List<? extends Object> al) {
System.out.println(al.get(0));
}
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
String mys1 = "jon";
al.add(mys1);
getMeListOfObjs(al);
}
This wont compile, because List<String>
isn't match List<Object>
这不会编译,因为List<String>
不匹配List<Object>
As @darijan pointed out the wildcard ? extends the type chack with all class that is descendant of it.
正如@darijan 指出的通配符 ? 使用它的所有后代类扩展类型 chack。
I recommend to read more about generics and wildcards
我建议阅读有关泛型和通配符的更多信息
回答by darijan
The code you provided works. I guess you want your al
list to be List<String>
. Then, you would have to make getMeListOfObjs
like this:
您提供的代码有效。我猜你希望你的al
清单是List<String>
. 然后,你必须getMeListOfObjs
像这样:
public static void getMeListOfObjs(List<? extends Object> al) {
System.out.println(al.get(0));
}
public static void main(String[] args) {
List<String> al = new ArrayList<String>();
String mys1 = "jon";
al.add(mys1);
getMeListOfObjs(al);
}
Notice the differnce?
注意到区别了吗?
public static void getMeListOfObjs(List<? extends Object> al) {
The wildcard ?
changes any type that extends Object
, i.e. any object.
通配符?
更改扩展的任何类型Object
,即任何对象。
回答by Peter Jaloveczki
The example you have created does not create a List<String>
. You also use a List<Object>
you just add Strings to the Object list. So you essentially pass Object list, that's why it works. You can add any object the the object list including string. It works since the String extends Object.
您创建的示例不会创建List<String>
. 您还可以使用List<Object>
您刚刚将字符串添加到对象列表中的方法。所以你基本上传递了对象列表,这就是它起作用的原因。您可以在对象列表中添加任何对象,包括字符串。它可以工作,因为字符串扩展了对象。
回答by Prasad Kharkar
You are passing a List<Object>
only. If your concern is that you have passed a String
in the List<Object>
that is perfectly fine because String
IS-A Object
.
你只是通过一个List<Object>
。如果您担心您已经通过了 a String
,List<Object>
那完全没问题,因为String
IS-A Object
。
Because even if you are putting a String
as an element in the List<Object>
, you are passing a List<Object>
to the method.
因为即使您将 aString
作为 元素放入 中List<Object>
,您也会将 a 传递List<Object>
给该方法。
回答by Paolof76
The book is saying that you can do this:
这本书说你可以这样做:
List al = new ArrayList<String>();
(but Eclipse suggest you not to use raw types... "List is a raw type. References to generic type List should be parameterized")
(但 Eclipse 建议你不要使用原始类型......“List 是一种原始类型。对泛型类型 List 的引用应该被参数化”)
but you can't do this:
但你不能这样做:
List<Object> al = new ArrayList<String>();
because the compiler recognizes a type mismatch "cannot convert from ArrayList<String> to List<Object>"
因为编译器识别出类型不匹配“无法从 ArrayList<String> 转换为 List<Object>”