插入Hashtable <character,list <boolean =“”>&gt; </character,>

时间:2014-02-05 20:07:37

标签: java

我不知道为什么我在put

下收到错误
The method put(Character, List<Boolean>) in the type 
Hashtable<Character,List<Boolean>> is not applicable for the arguments (char, boolean)

我认为我有所有匹配的类型

这是我的代码

    Hashtable<Character, List<Boolean>> charCheck =
         new Hashtable<Character, List<Boolean>>();
    List<Boolean> used = new ArrayList<Boolean>();
    //Set<String> nonDuplicate = new Set<String>();
    // make a hashtable of characters used
    char[] charArray = str.toCharArray(); 
    for (char c : charArray) {
        charCheck.put(c, used.add(false));

2 个答案:

答案 0 :(得分:2)

Java中的List#add方法返回boolean指示值是否已成功添加到List。 您应该将新List添加到Map,然后向其中添加新元素:

Hashtable<Character, List<Boolean>> charCheck = new Hashtable<Character, List<Boolean>>();
char[] charArray = str.toCharArray(); 

for (char c : charArray) {
    List<Boolean> used = charCheck.get(c);

    // If the char isn't in the map yet, add a new list
    if (used == null) {
        used = new ArrayList<Boolean>();
        charCheck.put (c, used);
    }

    used.add(false);
}

答案 1 :(得分:1)

您需要单独处理每个ArrayList,即每个ArrayList密钥只有一个Character

List<Boolean> used;

char[] charArray = str.toCharArray(); 

for (char c : charArray) {

    used = charCheck.get(c); // Get the list of values for this character

    if (used == null) { // No values stored so far for this character
        used = new ArrayList<Boolean>(); // Create a new (empty) list of values
        charCheck.put(c, used); // Add the empty list of values to the map
    }

    used.add(false); // Add the value for this character to the value list
}

另外,我建议使用HashMap代替Hashtable

HashMap<Character, List<Boolean>> charCheck = new HashMap<Character, List<Boolean>>();