int[] and Integer[], and generics

  • Follow


I'm having trouble dealing with some array-to-collection work: itseems that even though int and Integer can be autoboxed, it appearsthe same cannot be said for int[] and Integer[].Consider an aggregator class, which aggregates based on an underlyingcollection:public static <Ret, T> Ret aggregate(Collection<T> collection, RetinitialResult, 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 nowint[] array = {1, 2, 3};aggregate(array, 0, sumDelegate); // should return 6fails to compile for some reason (according to the compiler, it'smatching the int array to Collection<T> rather than T[]).So I need something to wrap the array of primitive type to acollection of its boxed equivalent (eg. a List<Integer> wrapper forint[]).  Is there something in the Java library for this, or do I haveto write this myself? (asList doesn't work as it returns a List<int[]>)
0
Reply kelvSYC822 (9) 4/4/2007 6:53:38 AM

kelvSYC schrieb:> 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[].True.> 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[]>)AFAIK: the latter.ByeMichael
0
Reply Michael 4/4/2007 8:53:04 AM


kelvSYC wrote:> 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[]>)There is no direct support for primitive array boxing in Java library.However, the library might appear handful for that purposes, like in the following example:     public static List<Integer> asList(final int[] array) {         return new AbstractList<Integer>() {             @Override             public int size() {                 return array.length;             }             @Override             public Integer get(int index) {                 return array[index];             }             @Override             public Integer set(int index, Integer element) {                 Integer e = array[index];                 array[index] = element;                 return e;             }         };     }Note:  The above could be slightly improved for your usage scenarios (see private ArrayList class from java.util.Arrays on which methods are good candidates for overriding, when better performing List implementation is desired).  See also Tom Hawtin's blog for other insights:http://jroller.com/page/tackline?entry=generic_handling_of_primitive_arraysOf course, that's not only approach.  Another one is to copy (element by element) your int[] array into Integer[] array, and then use asList() on it, or (even better) directly copy your array into e.g. new ArrayList<Integer>.Or simply don't use primitive arrays at all in your code, use only Lists instead.piotr
0
Reply Piotr 4/4/2007 9:43:35 AM

2 Replies
99 Views

(page loaded in 0.038 seconds)

Similiar Articles:













7/28/2012 4:34:17 AM


Reply: