本文介绍了Arraylist 的 list.add() 中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Eclipse JUno,我在使用 arraylist 的 .add() 时遇到问题,请帮忙.这是我的代码

I am using Eclipse JUno ,I am having trouble with the .add() of the arraylist guys please help.here is my code

     import java.util.ArrayList;
public class A
{
public static void main(String[] args)
  {
    ArrayList list=new ArrayList();
    list.add(90);
    list.add(9.9);
    list.add("abc");
    list.add(true);
    System.out.println(list);
  }
}

即将出现的错误是:

 Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method add(int, Object) in the type ArrayList is not applicable for the arguments (int)
    The method add(Object) in the type ArrayList is not applicable for the arguments (double)
    The method add(Object) in the type ArrayList is not applicable for the arguments (boolean)

    at A.main(A.java:7)

但奇怪的是,这条线

  list.add("abc");

没有引起任何错误.. 列表的 ADD 方法采用一个对象类型的参数,那么为什么我面临这个问题请帮助伙计们..我已经搜索了很多我没有得到任何解决方案.我必须做对此进行练习,由于此错误,我无法继续练习..

is not causing any error.. ADD method of list take one argument which is an object type then why i am facing this problem please help guys..i had searched a lot i did not get any solution.I have to do practice on this and due to this error i cant continue my practice..

推荐答案

我想您使用的是 Java 1.5 之前的版本.自动装箱 是在 java 1.5 中引入的.你的代码在 java 1.5+ 上编译得很好.

I suppose that you're using java prior version 1.5. Autoboxing was introduced in java 1.5. And your code compiles fine on java 1.5+.

编译为源 1.4:

javac -source 1.4 A.java


A.java:7: error: no suitable method found for add(int)
    list.add(90);
        ^
    method ArrayList.add(int,Object) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(Object) is not applicable
      (actual argument int cannot be converted to Object by method invocation conversion)
A.java:8: error: no suitable method found for add(double)
    list.add(9.9);
        ^
    method ArrayList.add(int,Object) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(Object) is not applicable
      (actual argument double cannot be converted to Object by method invocation conversion)
A.java:10: error: no suitable method found for add(boolean)
    list.add(true);
        ^
    method ArrayList.add(int,Object) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.add(Object) is not applicable
      (actual argument boolean cannot be converted to Object by method invocation conversion)
3 errors

使用 1.5(或更高版本):

With 1.5 (or later):

javac -source 1.5 A.java

warning: [options] bootstrap class path not set in conjunction with -source 1.5
Note: A.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 warning

建议您按照@SoulDZIN 的建议更新您的 java 或手动将所有原语装箱到对象中.

I suggest you to update your java or box all primitives to objects manually, as @SoulDZIN suggested.

这篇关于Arraylist 的 list.add() 中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:06