Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

int[] and Integer[], and generics

Thread view: 
kelvSYC - 04 Apr 2007 07:53 GMT
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[]>)
Michael Rauscher - 04 Apr 2007 09:53 GMT
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.

Bye
Michael
Piotr Kobzda - 04 Apr 2007 10:43 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.