Java - Type mismatch: cannot convert from element type Object to String

后端 未结 2 326
别那么骄傲
别那么骄傲 2020-12-21 08:24

I\'m having this error:

Type mismatch: cannot convert from element type Object to String

This is the code in error:



        
相关标签:
2条回答
  • 2020-12-21 08:48

    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

    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

    0 讨论(0)
  • 2020-12-21 09:07

    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

    0 讨论(0)
提交回复
热议问题