Java 类型中的方法 func(List<Object>) 不适用于参数 (List<String>)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20355809/
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
The method func(List<Object>) in the type is not applicable for the arguments (List<String>)
提问by ron reish
I have tried both these pieces of code but I am getting errors for both. Attached below are both pieces and both errors that I am getting. I would appreciate any insight as to why this is happening.
我已经尝试了这两段代码,但两者都出现错误。下面附上我得到的两个部分和两个错误。我将不胜感激任何有关为什么会发生这种情况的见解。
Example 1
示例 1
static List<String> list = new ArrayList<String>();
public static void main(String[] args) {
func(list);
}
private static void func(List<Object> lst) {
}
Error:
错误:
The method func(List<Object>) in the type is not applicable for the arguments (List<String>)
The method func(List<Object>) in the type is not applicable for the arguments (List<String>)
Example 2
示例 2
static List<Object> list = new ArrayList<Object>();
public static void main(String[] args) {
func(list);
}
private static void func(List<String> lst) {
}
Error:
错误:
The method func(List<String>) in the type is not applicable for the arguments (List<Object>)
The method func(List<String>) in the type is not applicable for the arguments (List<Object>)
回答by Adam Arold
static List<Object> list = new ArrayList<Object>();
public static void main(String[] args) {
func(list);
}
private static void func(List<Object> lst) {
}
This should work with the right imports(java util).
这应该适用于正确的导入(java util)。
You can't supply List<Object>
to a method with the parameter List<String>
. It will only work with the same type parameters.
您不能提供List<Object>
带有参数的方法List<String>
。它仅适用于相同类型的参数。
回答by davioooh
The method is not applicable because String
is an Object
but List<String>
is not a List<Object>
.
该方法不适用,因为String
is anObject
但List<String>
不是 a List<Object>
。
回答by Santosh Joshi
You should basically understand the purpose of Generics and how it works:
您应该基本上了解泛型的目的及其工作原理:
List<String> list = new ArrayList<String>();
means that this list can only contain list of strings, and it can only be passed to methods that accepts the argument of type List<String>
. And you can not substitute it for List <Object>
.
意味着此列表只能包含字符串列表,并且只能传递给接受 type 参数的方法List<String>
。而且你不能用它代替List <Object>
。
In order to make the function more generic you can use:
为了使函数更通用,您可以使用:
function (List<?> list) {
//body
}
回答by Ingo
Why you can't pass a List<String>
to a List<Object>
:
为什么不能将 a 传递List<String>
给 a List<Object>
:
void bong(List<Object> objs) {
objs.add ( new Integer(42) );
}
List<String> strings = Arrays.toList("foo", bar");
bong(strings); // not allowed because ... see next line
for (String s : strings) print(x.charAt(0));
This would be safe only if the method couldn't modify the passed list. Unfortunatly, most Java classes are mutable, and so are most Lists implementations.
仅当该方法无法修改传递的列表时,这才是安全的。不幸的是,大多数 Java 类都是可变的,大多数 Lists 实现也是如此。
Why you can't pass a List<Object>
to a List<String>
:
为什么不能将 a 传递List<Object>
给 a List<String>
:
void bing(List<String> strings) {
for (String s : strings) print(x.charAt(0));
}
bing(Arrays.toList((Object)1,(Object)2,(Object)3));