0

Auto-unboxing seems to not work when using ListIterator.
The code below throws a compile-time error stating "incompatible types: Object cannot be converted to int"

import java.util.*;
class Input_Array
{
    public static void main(String[] args) 
    {
        String x = "36 42 74 98 41 85";
        String y[]  = x.split(" ");
        int p[] = new int [6];
        ArrayList<Integer> lst  = new ArrayList<>();
        int i=0;
        for(String v : y)
        {
            lst.add(Integer.valueOf(v));
        }
        ListIterator X = lst.listIterator(lst.size());
        while(X.hasPrevious())
        {
            int n=X.previous();
            System.out.print(n+" ");
        }
        System.out.println();
    }
}


.
Replacing line 18 with

int n=(int)X.previous();


seems to work pretty good. Isn't auto unboxing(automatic conversion of Object type to their primitive) supported by Listiterators?

  • Boxing conversions have nothing to do with `Object`. They have to do with the defined wrapper classes, such as `Integer` and `Boolean` and `Long`, etc. – Makoto Jul 17 '18 at 15:40
  • It is perhaps unclear why the given dupe target is relevant. The problem is that `ListIterator` is a generic type, and you are using it raw (without a type parameter). – John Bollinger Jul 17 '18 at 15:46
  • @JohnBollinger thank You. That explains the solution to the question. – Akhil Kathi Jul 17 '18 at 16:02

0 Answers0