K
kelvSYC
I'm having trouble dealing with some array-to-collection work: it
seems that even though int and Integer can be autoboxed, it appears
the same cannot be said for int[] and Integer[].
Consider an aggregator class, which aggregates based on an underlying
collection:
public static <Ret, T> Ret aggregate(Collection<T> collection, Ret
initialResult, Delegate<Ret, T> delegate);
public static <Ret, T> Ret aggregate(T[] array, Ret initialResult,
Delegate<Ret, T> delegate) { return aggregate(Arrays.asList(array),
initialResult, delegate); }
Simple enough, right? But now
int[] array = {1, 2, 3};
aggregate(array, 0, sumDelegate); // should return 6
fails to compile for some reason (according to the compiler, it's
matching the int array to Collection<T> rather than T[]).
So I need something to wrap the array of primitive type to a
collection of its boxed equivalent (eg. a List<Integer> wrapper for
int[]). Is there something in the Java library for this, or do I have
to write this myself? (asList doesn't work as it returns a List<int[]>)
seems that even though int and Integer can be autoboxed, it appears
the same cannot be said for int[] and Integer[].
Consider an aggregator class, which aggregates based on an underlying
collection:
public static <Ret, T> Ret aggregate(Collection<T> collection, Ret
initialResult, Delegate<Ret, T> delegate);
public static <Ret, T> Ret aggregate(T[] array, Ret initialResult,
Delegate<Ret, T> delegate) { return aggregate(Arrays.asList(array),
initialResult, delegate); }
Simple enough, right? But now
int[] array = {1, 2, 3};
aggregate(array, 0, sumDelegate); // should return 6
fails to compile for some reason (according to the compiler, it's
matching the int array to Collection<T> rather than T[]).
So I need something to wrap the array of primitive type to a
collection of its boxed equivalent (eg. a List<Integer> wrapper for
int[]). Is there something in the Java library for this, or do I have
to write this myself? (asList doesn't work as it returns a List<int[]>)