How do I fix this without using Integer[] arr = new Integer[]{..}?
You don't. This simply won't work.
I was under the impression that the compiler would box my int to Integer automatically?
It will, for individual int
to Integer
values. But there's no conversion from int[]
to Integer[]
... so combined with the lack of primitive support in Java generics, a method taking a T[]
simply cannot take an int[]
.
There are third-party libraries which have conversion methods, but they will always create a new array, rather than creating a view onto the existing array. (It would be possible to create a List<Integer>
which was a view onto the existing array, although obviously add
operations would have to fail, as would set
operations with a null
value.)