Java 方法将(String,ArrayList<;Integer>;)放入类型TreeMap<;字符串,ArrayList<;整数>&燃气轮机;不适用于参数(字符串、布尔值)

Java 方法将(String,ArrayList<;Integer>;)放入类型TreeMap<;字符串,ArrayList<;整数>&燃气轮机;不适用于参数(字符串、布尔值),java,put,treemap,Java,Put,Treemap,我在这条线上遇到了一个错误 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) 我想知道为什么前一个put方

我在这条线上遇到了一个错误

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)
我想知道为什么前一个put方法没有错误,而只有第二个方法有错误。

ArrayList.add(E)
返回一个
布尔值,因此,不能将调用合并到一个语句中

您需要将
ArrayList
对象作为第二个参数传递给
put
方法。

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

您需要将一个
ArrayList
对象作为第二个参数传递给
put
方法;也就是说,它不会返回新的ArrayList。尝试克隆列表,添加到列表中,然后将其作为参数传递。

;也就是说,它不会返回新的ArrayList。尝试克隆列表,添加到列表中,然后将其作为参数传递。

ArrayList.add(E)
返回一个
布尔值,您无法将它们链接起来

tm.get(temp[j]).add(j)
就足够了,您不需要再次
put

newarraylist(j)
不会给出一个元素的ArrayList,参数是initialCapacity

然后,您应该将
tm
声明为
Map

Map tm=newtreemap();
字符串[]temp=folders.split(“|,”);
对于(int j=1;j
ArrayList.add(E)
返回一个
boolean
,您无法将它们链接起来

tm.get(temp[j]).add(j);
就足够了,你不需要再
put

newarraylist(j)
不会给出一个元素的ArrayList,参数是initialCapacity

然后,您应该将
tm
声明为
Map

Map tm=newtreemap();
字符串[]temp=folders.split(“|,”);
对于(int j=1;j)

公共布尔加法(E) 将指定的元素追加到此列表的末尾,并返回布尔值。因此会出现错误。

公共布尔加法(E) 将指定的元素追加到此列表的末尾,并返回布尔值。因此,出现了错误

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";
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.

}