简体   繁体   English

第 24 行:错误:类型不兼容:列表<string>不能转换成字符串</string>

[英]Line 24: error: incompatible types: List<String> cannot be converted to String

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        
        if(strs.length==0)
           return new ArrayList<>();
           
        Map<String,List<String>> anagrams=new HashMap<>();
        String anagram_key;
        
        for(String s:strs){
            char[] arr=s.toCharArray();
            Arrays.sort(arr);
            anagram_key=String.valueOf(arr);

            if(!anagrams.containsKey(anagram_key))
                anagrams.put(anagram_key,new ArrayList<>());

            anagrams.get(anagram_key).add(s);
        }
        
        List<String> resList=new ArrayList<>();

        for(Map.Entry<String,List<String>> _itr:anagrams.entrySet()){
            resList.add(_itr.get());
        }

        return resList;
    }
}

Was Expecting List Of String Array.期待字符串数组列表。 Running fine on my Intellij Idea IDE but not on leet code在我的 Intellij Idea IDE 上运行良好但在 leet 代码上运行不正常

The Error shown on leetcode is as follows: Please Review + Any Effective Source To Learn More About Errors. leetcode 上显示的错误如下: Please Review + Any Effective Source To Learn More about Errors。

Line 24: error: incompatible types: List<String> cannot be converted to String
    resList.add(_itr.getValue());
                             ^
Line 27: error: incompatible types: List<String> cannot be converted to List<List<String>>
return resList;
       ^
2 errors

You last code section should look something like this:你最后的代码部分应该是这个样子:

List<List<String>> resList=new ArrayList<>();

for(Map.Entry<String,List<String>> _itr:anagrams.entrySet()){
    resList.add(_itr.getValue());
}

return resList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:不兼容的类型:String[] 无法转换为 String - error: incompatible types: String[] cannot be converted to String 错误:类型不兼容:布尔值无法转换为列表<String> - error: incompatible types: boolean cannot be converted to List<String> 错误:类型不兼容:字符串无法转换为URI - error: incompatible types: String cannot be converted to URI 错误:不兼容的类型:int 无法转换为 String - error: incompatible types: int cannot be converted to String 错误:类型不兼容:char无法转换为String - error: incompatible types: char cannot be converted to String 错误:不兼容的类型:无法将 Opiskelija 转换为字符串 - error: incompatible types: Opiskelija cannot be converted to String 错误:不兼容的类型:字符串无法转换为 int - error: incompatible types: String cannot be converted to int JAVA - 错误不兼容类型:字符串无法转换为字符串 [] - JAVA - Error incompatible types: String cannot be converted to String[ ] 错误:不兼容的类型:字符串无法转换为 JSONObject url,(字符串)null,^ - error: incompatible types : String cannot be converted to JSONObject url, (String) null,^ 错误:不兼容的类型:java.lang.String 无法转换为 String - error: incompatible types: java.lang.String cannot be converted to String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM