Java -类型不匹配:不能从元素类型对象转换为字符串。

时间:2023-01-12 13:20:26

I'm having this error:

我有这个错误:

Type mismatch: cannot convert from element type Object to 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;
}

2 个解决方案

#1


7  

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

您在列表声明中丢失了“ ”。

#2


1  

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

希望帮助. .如果不回答我的问题,那就更容易帮助了。

#1


7  

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

您在列表声明中丢失了“ ”。

#2


1  

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

希望帮助. .如果不回答我的问题,那就更容易帮助了。