繁体   English   中英

方法put(String,ArrayList <Integer> ),类型为TreeMap <String,ArrayList<Integer> &gt;不适用于参数(字符串,布尔值)

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

我在这条线上出现错误

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

我想知道为什么前一个put方法没有错误,而只有第二个错误。

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

tm.get(temp[j]).add(j); 足够,您无需再次put

new ArrayList<Integer>(j)不会为您提供一个元素的arraylist,参数是initialCapacity。

然后,应将tm声明为Map<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.

}

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

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

在这种情况下ArrayList::add返回true 也就是说,它不会返回新的ArrayList。 尝试克隆列表,将其添加到列表中,然后将其作为参数传递。

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E

public boolean add(E e)将指定的元素追加到此列表的末尾并返回一个布尔值。 因此,错误。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM