简体   繁体   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)

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.

相关问题 错误:ArrayList 类型中的方法 add(Integer)<integer> 不适用于 arguments(字符串)</integer> - ERROR: The method add(Integer) in the type ArrayList<Integer> is not applicable for the arguments (String) Integer 类型中的方法 parseInt(String) 不适用于参数(布尔值) - The method parseInt(String) in the type Integer is not applicable for the arguments (boolean) 如何修复错误:Map 类型中的 put(Integer, Integer) 方法<integer,integer>不适用于 arguments (int, String)</integer,integer> - How to fix error: The method put(Integer, Integer) in the type Map<Integer,Integer> is not applicable for the arguments (int, String) ArrayList类型中的方法add(String) <String> 不适用于参数(对象) - The method add(String) in the type ArrayList<String> is not applicable for the arguments (Object) ArrayList类型中的方法add(String) <String> 不适用于arguments() - The method add(String) in the type ArrayList<String> is not applicable for the arguments() 方法 add(ArrayList<integer> ) 类型为 ArrayList <arraylist<integer> &gt; 不适用于 arguments(无效) </arraylist<integer></integer> - The method add(ArrayList<Integer>) in the type ArrayList<ArrayList<Integer>> is not applicable for the arguments (void) 方法添加到ArrayList中 <ClassName> 不适用于参数(字符串) - method add in ArrayList<ClassName> is not applicable for the arguments (String) Integer类型的方法valueOf(String)不适用于参数(int) - The method valueOf(String) in the type Integer is not applicable for the arguments (int) 转换 ArrayList <ArrayList<String> &gt; 到 ArrayList <ArrayList<Integer> &gt; - Converting ArrayList<ArrayList<String>> to ArrayList<ArrayList<Integer>> 在Java中编辑字符串布尔值和整数的ArrayList - Editing an ArrayList of String Boolean and Integer in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM