简体   繁体   English

错误发生在Arraylist的list.add()中

[英]Error is coming in Arraylist 's list.add()

I am using Eclipse JUno ,I am having trouble with the .add() of the arraylist guys please help.here is my code 我正在使用Eclipse JUno,我遇到了arraylist的.add()问题请帮助。我的代码是

     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);
  }
}

the error which is coming is : 即将发生的错误是:

 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)

but here is the weird thing ,that the line 但这是奇怪的事情,那条线

  list.add("abc");

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.. 没有引起任何错误..列表的ADD方法采取一个参数,这是一个对象类型然后为什么我面临这个问题,请帮助那些..我搜索了很多我没有得到任何解决方案。我必须做这个练习由于这个错误,我不能继续我的练习..

I suppose that you're using java prior version 1.5. 我想你使用的是java 1.5之前的版本。 Autoboxing was introduced in java 1.5. Autoboxing在java 1.5中引入。 And your code compiles fine on java 1.5+. 并且您的代码在java 1.5+上编译得很好。

Compile as source 1.4: 编译为源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

With 1.5 (or later): 1.5(或更高):

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

I suggest you to update your java or box all primitives to objects manually, as @SoulDZIN suggested. 建议你手动更新你的java或框所有基元到对象,如@SoulDZIN建议的那样。

Notice that the 'add' method is failing for the data types: 请注意,'add'方法对于数据类型失败:

int, double, and boolean. int,double和boolean。

These are all primitive data types and not 'Objects', which the method is expecting. 这些都是原始数据类型,而不是该方法所期望的“对象”。 I believe that autoboxing is not occurring here because you are using literal values, I'm not sure about this though. 我相信在这里没有发生自动装箱,因为你使用的是字面值,但我对此并不确定。 Nevertheless, to fix this, use the associated Object type of each primitive: 不过,要解决此问题,请使用每个基元的关联对象类型:

ArrayList list=new ArrayList();
list.add(new Integer(90));
list.add(new Double(9.9));
list.add("abc");
list.add(new Boolean(true));
System.out.println(list);

SOURCE: Experience 消息来源:经验

EDIT: 编辑:

I always try to specify the type of my Collection, even if it is an Object. 我总是尝试指定我的Collection的类型,即使它是一个Object。

ArrayList<Object> list = new ArrayList<Object>();

However, apparently this isn't a good practice if you are running Java 1.4 or less. 但是,如果您运行Java 1.4或更低版本,显然这不是一个好习惯。

Works great with JDK 6 适用于JDK 6

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);
    }

Printed result :[90, 9.9, abc, true]. 印刷结果 :[90,9.9,abc,true]。

If still you are using lesser version than jdk 6 .Please specify version. 如果你仍然使用比jdk 6更小的版本。请指定版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM