代码之家  ›  专栏  ›  技术社区  ›  user3344382

错误:未找到适合put(String,int)的方法

  •  0
  • user3344382  · 技术社区  · 12 年前

    编译此时出错:

    TreeMap <String, Long> myMap = new TreeMap <String, Long>();
    //populate the map
    myMap.put("preload_buffer_size", 1024);
    myMap.put("net_buffer_length", 1024);
    //etc...
    
    
    error: no suitable method found for put(String,int)
        myMap.put("preload_buffer_size", 1024);
             ^
    method TreeMap.put(String,Long) is not applicable
      (actual argument int cannot be converted to Long by method invocation conversion)
    method AbstractMap.put(String,Long) is not applicable
      (actual argument int cannot be converted to Long by method invocation conversion)
    

    我需要使用Long,而不是int 我真的不知道该怎么解决,如果你能帮我解决这个问题,我将不胜感激。

    3 回复  |  直到 12 年前
        1
  •  1
  •   Lee    12 年前
    myMap.put("preload_buffer_size", 1024L);
    
        2
  •  1
  •   bknopper dmnd    12 年前

    您正在尝试将一个具有Integer值的String键(以其原始形式int)放入一个指定为String到Long映射的映射中。所以它只接受

    myMap.put(String, Long);
    

    通过在数字中添加“L”,编译器会将其识别为Long,而不是默认的Integer。

    因此,解决方案如下:

    myMap.put("preload_buffer_size", 1024L);
    
        3
  •  1
  •   Olimpiu POP    12 年前

    问题是,您正在尝试将数字文本放置在具有 String 作为密钥, Long 作为值。默认情况下,在java中,数字文本为 int 因此,请写下:

     TreeMap <String, Long> myMap = new TreeMap <String, Long>();
     //populate the map
     myMap.put("preload_buffer_size", 1024L);
     myMap.put("net_buffer_length", 1024L);
    

    或

    TreeMap <String, Long> myMap = new TreeMap <String, Long>();
    //populate the map
    myMap.put("preload_buffer_size", new Long(1024));
    myMap.put("net_buffer_length", new Long(1024));