【问题标题】:no suitable method found for add(long)没有找到适合 add(long) 的方法
【发布时间】:2019-09-26 21:21:48
【问题描述】:

没有找到适合 add(long) 的方法!

我知道这个问题是因为使用了 Long 和 long 但是 - 当我想在一些编码问题中使用我的 x 和 y 数组时如何解决这个问题!

Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int it=0;it<T;it++){
    int N=sc.nextInt();
    long x[]=new long[N];//////
    long y[]=new long[N];//////

    HashMap<Long,List<Long>> hh=new HashMap<>();////////////
    for(int i=0;i<N;i++){
        x[i]=sc.nextLong();///
        y[i]=sc.nextLong();///
        List<Long>ll=new ArrayList<>();////
        if(hh.get(x[i])==null){
            ll.add(y[i]);//*error
            hh.put(x[i], ll);//*error
        }else{
            ll=hh.get(x[i]);
            ll.add(y[i]);//*error
            hh.put(x[i], ll);//*error
        }
    }
}

【问题讨论】:

  • 对我来说很好。您可以将long 放入List&lt;Long&gt;
  • 什么是java版本?
  • 我使用的是 Java 版本 8
  • List 给出了意外的类型错误
  • List 给出了意外的类型错误 - 使用 List&lt;Long&gt; 而不是 List&lt;long&gt;

标签: java list types hashmap


【解决方案1】:

终于得到了解决方案:- java.lang.Long 解决了这个问题,但仍然不知道原因。

HashMap<java.lang.Long,List<java.lang.Long>> hh=new HashMap<>();////////////
for(int i=0;i<N;i++){
    x[i]=sc.nextLong();///
    y[i]=sc.nextLong();///
    List<java.lang.Long>ll=new ArrayList<>();////
    if(hh.get(x[i])==null){
        ll.add(y[i]);//*error
        hh.put(x[i], ll);//*error
    }else{
        ll=hh.get(x[i]);
        ll.add(y[i]);//*error
        hh.put(x[i], ll);//*error
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多