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

后端 未结 3 1902
星月不相逢
星月不相逢 2021-01-14 03:42

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          


        
相关标签:
3条回答
  • 2021-01-14 03:46

    Works great with 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].

    If still you are using lesser version than jdk 6.Please specify version.

    0 讨论(0)
  • 2021-01-14 03:53

    Notice that the 'add' method is failing for the data types:

    int, double, and 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.

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

    However, apparently this isn't a good practice if you are running Java 1.4 or less.

    0 讨论(0)
  • 2021-01-14 04:03

    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+.

    Compile as source 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):

    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.

    0 讨论(0)
提交回复
热议问题