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: How to convert bytearray into integer? - comp.lang.python ...... to convert 4 bytes on an bytearray into an 32-bit integer. ... index=0 for this generated list > to get the int out. ... buf[, num_bytes[, flags]]) but forget the more > generic ... To combine string and integer in F90 - comp.lang.fortran ...Hello all, I would like to combine string and an integer number to write the result ... none > =A0 character*3 int_as_String > =A0 character*7 all > > =A0 write(int_as ... 64 bit integer - comp.lang.c++.moderated"BackslashZero" <Costellj@gmail.com> wrote: > I need to to store 19 9's in an integer, so i need an unsigned 64 bit > int. In windows vsc++ compiler the code would be ... Convert int to double then back to int? - comp.lang.c++.moderated ...The integer value doesn't change. I'm wondering whether this is true everywhere or it ... $ cat main.cpp #include <iostream> #include <limits> int main() { //long int x ... GL_RG_INTEGER - comp.graphics.api.openglHi, I tried using 32 bits integer RG textures and use them in a shader, but I don't get meaningful values in my shader (if I just replace GL_RG32I b... nitializing a static vector <> of integers (this static vector... similar behavior and > methods - its a template! it is a generic way of ... integers (this static vector nitializing a static vector <> of integers (this static vector<int ... Difference between passing a number and a variable to a subroutine ...... C++ when calling "bar" with a literal (it needs a "const" qualifier) int ... has, but I'd venture a guess that it is not the kind value for default integer. Generic ... 128 bit integer - comp.lang.cHi all, How to program 128 bit integer in C language ? Thank you. ... ... > > Repeat the above replacing "int" with "uint" for unsigned types. > > But most ... display integer in MessageBox - comp.lang.c++.moderatedThanks. -mod } Hello, I need to display int value in MessageBox in my dll ... Display number in text box - comp.soft-sys.matlab display integer in MessageBox - comp ... unsigned int to ABGR float values - comp.graphics.api.opengl ...hello NG! i wrote an tool to store color variables in a file, in unsigned int. so ... 8) | r; > > a = ((ABGR && 0xFF000000) >> 24) / 255 this one does an integer division ... int vs Integer : Java Glossary - Canadian Mind Products ...pass it as a generic object to a TableCellRenderer. You may pass a variety of ... // to int i from Integer ii int i = ii. intValue (); // to Integer ii from int i Integer ii = new ... java - How do I cast from int to generic type Integer? - Stack ...I'm relatively new to Java and am used to generics in C# so have struggled a bit with this code. Basically I want a generic method for getting a stored Android ... 7/28/2012 4:34:17 AM
|