2012-06-07 63 views
0

我在这条线得到一个错误的方法把(字符串,ArrayList的<Integer>)在类型TreeMap的<字符串,ArrayList的<Integer>>不适用的参数(字符串,布尔值)

tm.put(temp[j],tm.get(temp[j]).add(i)); 

时我是编译我的程序在eclipse:

The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean) 

以下是我的代码:

TreeMap<String, ArrayList<Integer>> tm=new TreeMap<String, ArrayList<Integer>>(); 
String[] temp=folders.split(" |,"); 
for (int j=1;j<temp.length;j++){ 

      if (!tm.containsKey(temp[j])){ 
       tm.put(temp[j], new ArrayList<Integer>(j)); 
      } else { 
       tm.put(temp[j],tm.get(temp[j]).add(j)); 
      } 
     } 

文件夹是这样

folders="0 Jim,Cook,Edward"; 

我不知道为什么没有错误对前者方法,但仅在第二个。

+0

抛出什么错误? –

回答

2

ArrayList.add(E)返回一个boolean,你根本无法链接起来。

tm.get(temp[j]).add(j);已经足够了,您再也不需要put了。

new ArrayList<Integer>(j)不会给你一个元素的数组列表,参数是initialCapacity。

然后,您应该声明tmMap<String, List<Integer>>

Map<String, List<Integer>> tm=new TreeMap<String, List<Integer>>(); 
String[] temp=folders.split(" |,"); 
for (int j=1;j<temp.length;j++){ 

    if (!tm.containsKey(temp[j])){ 
     tm.put(temp[j], new ArrayList<Integer>()); 
    } 
    tm.get(temp[j]).add(j); // This will change the arraylist in the map. 

} 
+0

老兄谢谢.... – Cybershoe

0

ArrayList.add(E)返回boolean值,因此,您不能将调用并入单个语句中。

您需要传递一个ArrayList<Integer>对象作为put方法的第二个参数。

相关问题