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:
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.
ArrayList.add(E)returns aboolean, you simply cannot chain them up.tm.get(temp[j]).add(j);is enough, you don’t need toputagain.new ArrayList<Integer>(j)won’t give you an arraylist of one element, the argument is the initialCapacity.Then, you should declare
tmasMap<String, List<Integer>>.