![](/img/trans.png)
[英]ERROR: The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (String)
[英]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.