简体   繁体   English

将Integer转换为int

[英]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)". 我收到“类型列表中的方法add(int,Integer)不适用于参数(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 . java.util.List确实具有合适的add()方法,并且您的代码确实在Ideone中进行编译

I therefore strongly suspect the List class in your example isn't java.util.List but is some other class. 因此,我强烈怀疑示例中的List类不是java.util.List而是其他一些类。

Look at the import statements in your code to figure out what exactly is going on. 查看代码中的import语句以弄清楚到底发生了什么。

Variable 'list' is a list of Integer 'List '. 变量“列表”是整数“列表”的列表。 The argument of 'list.add' should not be 'int' but 'Integer'. “ list.add”的参数不应为“ int”,而应为“ Integer”。

Your problem may solved by converting int to Integer in your code as follows. 您的问题可以通过在代码中将int转换为Integer来解决,如下所示。

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 我怀疑你只是使用java.awt.List

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

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