Monday, January 11, 2016

A note on Autoboxing for arrays in Java

There is no autoboxing for primitive type arrays!


    int[] arr={1, 5, 7, 0, 4, 5, 10, 11, 40};
cannot become *automatically*

Integer[] arr2 = arr;

You need to loop!



    int[] arr={1, 5, 7, 0, 4, 5, 10, 11, 40};
   
    Integer[] arr2 = new Integer[arr.length];
   
    for (int i=0; i < arr.length; i++)
    {
        arr2[i]=arr[i];
    }

No comments:

Post a Comment

Your comment will be visible after approval.