小编use*_*330的帖子

为什么我不能有两个带ArrayList参数的方法?

为什么我不能创建两个重载方法,其参数既是数组列表,又有不同的数据类型?

public class test {
  public static void main(String[] args){

    ArrayList<Integer> ints = new ArrayList<Integer>();
    ints.add(1);
    ints.add(2);
    ints.add(3);
    ints.add(4);
    ints.add(5);

    showFirst(ints);

    ArrayList<Double> dubs = new ArrayList<Double>();
    dubs.add(1.1);
    dubs.add(2.2);
    dubs.add(3.3);
    dubs.add(4.4);
    dubs.add(5.5);

    showFirst(dubs);
  } 

  public static void showFirst(ArrayList<Integer> a)
  {
    System.out.println(a.remove(0));
  }

  public static void showFirst(ArrayList<Double> a)
  {
    System.out.println(a.remove(0));
  }
}
Run Code Online (Sandbox Code Playgroud)

我在eclipse中,它强调了导致代码为红色的问题并给出了这样的信息: Method showFirst(ArrayList<Integer>) has the same erasure showFirst(ArrayList<E>) as another method in type test

我能让它工作的唯一方法是添加其他参数,例如, int bafter showFirst(ArrayList<Integer> a, int bafter showFirst(ArrayList<Double> a.

有没有办法让这段代码按照我的意图运作?如果没有,我想知道为什么会这样. …

java generics methods exception arraylist

3
推荐指数
1
解决办法
397
查看次数

标签 统计

arraylist ×1

exception ×1

generics ×1

java ×1

methods ×1