Java - 类型不匹配:无法从元素类型对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23853404/
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
Java - Type mismatch: cannot convert from element type Object to String
提问by PlayinCOD3142
I'm having this error:
我有这个错误:
Type mismatch: cannot convert from element type Object to String
类型不匹配:无法从元素类型 Object 转换为 String
This is the code in error:
这是错误的代码:
public List<String> customPrefixes(PermissionUser u)
{
List returnlist = new ArrayList();
for (String k : u.getAllPermissions().keySet()) {
List perms = (List)u.getAllPermissions().get(k);
for (String s : perms) {
String[] split = s.split(".");
if ((split.length >= 3) &&
(split[0].equalsIgnoreCase("plugin")) &&
(split[1].equalsIgnoreCase("prefix"))) {
returnlist.add(split[2]);
}
}
}
return returnlist;
}
采纳答案by Lena Bru
try this :
尝试这个 :
public List<String> customPrefixes(PermissionUser u)
{
List<String> returnlist = new ArrayList<String>();
for (String k : u.getAllPermissions().keySet()) {
List<String> perms = (List<String>)(u.getAllPermissions()).get(k);
for (String s : perms) {
String[] split = s.split(".");
if ((split.length >= 3) &&
(split[0].equalsIgnoreCase("plugin")) &&
(split[1].equalsIgnoreCase("prefix"))) {
returnlist.add(split[2]);
}
}
}
return returnlist;
}
You were missing "<String>"
in the List declaration
您"<String>"
在 List 声明中丢失了
回答by Aviad
I think you're casting is wrong..
我觉得你打错了..
What is u.getAllPermissions().get(k); should return? List of something? if it does so you need to add type of the generic list
什么是 u.getAllPermissions().get(k); 应该返回?东西清单?如果是这样,您需要添加通用列表的类型
List<String> perms = (List<String>)u.getAllPermissions().get(k);
If that doesn't work you can also try to do
如果这不起作用,你也可以尝试做
for (Object o : perms) {
String s = o.toString();
.....
}
Hope that helps.. If not answer my question and it will be easier to help
希望有帮助..如果不回答我的问题,它会更容易帮助