[英]The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean)
I'm getting an error on this line 我在这条线上出现错误
tm.put(temp[j],tm.get(temp[j]).add(i));
when i was compiling my program in eclipse: 当我在Eclipse中编译程序时:
The method put(String, ArrayList<Integer>) in the type TreeMap<String,ArrayList<Integer>> is not applicable for the arguments (String, boolean)
The followings are my codes: 以下是我的代码:
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));
}
}
the folders is something like this 文件夹是这样的
folders="0 Jim,Cook,Edward";
I'm wondering why there's no error on the former put method, but only on the second one. 我想知道为什么前一个put方法没有错误,而只有第二个错误。
ArrayList.add(E)
returns a boolean
, you simply cannot chain them up. ArrayList.add(E)
返回一个boolean
,您根本无法将它们链接起来。
tm.get(temp[j]).add(j);
is enough, you don't need to put
again. 足够,您无需再次
put
。
new ArrayList<Integer>(j)
won't give you an arraylist of one element, the argument is the initialCapacity. new ArrayList<Integer>(j)
不会为您提供一个元素的arraylist,参数是initialCapacity。
Then, you should declare tm
as Map<String, List<Integer>>
. 然后,应将
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)
returns a boolean
value, and thus, you can't incorporate the call within a single statement. ArrayList.add(E)
返回一个boolean
值,因此,您不能将调用合并到单个语句中。
You need to pass an ArrayList<Integer>
object as the second argument to the put
method. 您需要将
ArrayList<Integer>
对象作为put
方法的第二个参数传递。
ArrayList::add
returns true in this scenario ; 在这种情况下
ArrayList::add
返回true ; that is, it doesn't return the new ArrayList. 也就是说,它不会返回新的ArrayList。 Try cloning the list, adding to it, and then passing it as an argument.
尝试克隆列表,将其添加到列表中,然后将其作为参数传递。
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E ) http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#add(E )
public boolean add(E e) Appends the specified element to the end of this list and returns a boolean. public boolean add(E e)将指定的元素追加到此列表的末尾并返回一个布尔值。 Hence, the error.
因此,错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.