int[] and Integer[], and generics

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[]>)
 
M

Michael Rauscher

kelvSYC said:
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.

Bye
Michael
 
P

Piotr Kobzda

kelvSYC said:
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_arrays


Of 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top