user2461414
user2461414

Reputation: 31

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

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

Upvotes: 3

Views: 8530

Answers (3)

souldzin
souldzin

Reputation: 1476

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.

Upvotes: 1

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

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.

Upvotes: 4

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 0

Related Questions