for-each not applicable to expression type


  • 0
    L
    
    public class Solution {
        public List<String> letterCombinations(String digits) {
            List<String> res = new LinkedList<>();
            if(digits == null || digits.length() == 0){ 
                return res;
            }
            HashMap map = new HashMap();
            
            map.put('0', new char[] {'0'});
            map.put('1', new char[] {'1'});
            map.put('2', new char[] {'a','b','c'});
            map.put('3', new char[] {'d','e','f'});
            map.put('4', new char[] {'g','h','i'});
            map.put('5', new char[] {'j','k','l'});
            map.put('6', new char[] {'m','n','o'});
            map.put('7', new char[] {'p','q','r','s'});
            map.put('8', new char[] {'t','u','v'});
            map.put('9', new char[] {'w','x','y','z'});
            
            backtrack(res, map, digits, new StringBuilder());
            return res;
            
            
        }
        
        public void backtrack (List<String> res, HashMap map, String ca, StringBuilder sb){
            if (sb.length() == ca.length()){
                res.add(sb.toString());
                return;
            }else{
                    for (char c : map.get(ca.charAt(0))){
                        sb.append(c);
                        backtrack(res, map, ca.substring(1,ca.length(), sb));
                        sb.delete(c);
                    }
            }
            
        }
    }
    
    

    so I have this "for-each not applicable to expression type" error when trying to run my code on line 31, which is

    for (char c : map.get(ca.charAt(0))){
    

    map should get the corresponding char array and I am pretty sure for each should work with array. Then why is this wrong?

    TIA


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.