简体   繁体   中英

Conversion issue Integer to int

Unable to add the values in collections, when i`m trying to add the element getting below error message.

Error message that i`m getting "The method add(int, Integer) in the type List is not applicable for the arguments (int)".

public static void additem(String type, List<Integer> list)
{
    long st=System.currentTimeMillis();

    for(int i=0;i<1E5;i++)
    {
    list.add(i);
    }
    long st1=System.currentTimeMillis();
    System.out.println("Added Item is : " +list.get(0) + type+ "Time" +(st-st1));
}

java.util.List does have a suitable add() method and your code does compile in Ideone .

I therefore strongly suspect the List class in your example isn't java.util.List but is some other class.

Look at the import statements in your code to figure out what exactly is going on.

Variable 'list' is a list of Integer 'List '. The argument of 'list.add' should not be 'int' but 'Integer'.

Your problem may solved by converting int to Integer in your code as follows.

public static void additem(String type, List<Integer> list)
{
    long st=System.currentTimeMillis();

    for(int i=0;i<1E5;i++)
    {
        list.add(new Integer(i)); // create Integer for using as argument.
    }
    long st1=System.currentTimeMillis();
    System.out.println("Added Item is : " +list.get(0).toString + type+ "Time" +(st-st1)); // convert Integer to string for print.
}

I think this will fix your problem.

        public static void additem(String type, java.util.List<Integer> list){
        ...
        }

I suspect you just use java.awt.List

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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