Prev: Your Helping Heart
Next: create folder
From: markspace on 22 Jul 2010 18:34 Simone wrote: > I also noticed that the solution with Arrays.asList(vector) works only > if vector is an array of a wrapper class and not if it's an array of a > primitive type, That's because List itself doesn't work with primitive types. You can't have a List<int>, you'd have to have use List<Integer>. > in which case the output is, again, the hash code of > the array. Well, it's a list of one element, being the list you passed in. Type of List<int[]>. > I'm using the solution Arrays.asList(vector) for now, maybe I'll write > a specific method Not sure the best way to do this. A minimalist approach, one which reuses as much code as possible, might be to convert the primitive array to an array of objects, and then pass that to Arrays.asList(). I.e., convert int[] to Integer[] then use that on Arrays.asList(). Hmm.... I might want to do that...
From: markspace on 22 Jul 2010 18:51 markspace wrote: > Hmm.... I might want to do that... > This seems to work: package test; import java.lang.reflect.Array; public class Util { static public <T extends Number> T[] autoBox( Class<T> type, Object array ) { int length = Array.getLength( array ); Object retVal = Array.newInstance( type, length ); for( int i = 0; i < length; i++ ) { Array.set( retVal, i, Array.get( array, i) ); } return (T[])retVal; } } Useage: public static void main( String[] args ) { int[] test = {1,2,3}; System.out.println( Arrays.asList( autoBox( Integer.class, test ) ) ); } prints: [1, 2, 3]
From: Alan Gutierrez on 22 Jul 2010 19:40 markspace wrote: > markspace wrote: > >> Hmm.... I might want to do that... >> > > This seems to work: > > > package test; > import java.lang.reflect.Array; > > public class Util { > > static public <T extends Number> T[] autoBox( Class<T> type, > Object array ) > { > int length = Array.getLength( array ); > Object retVal = Array.newInstance( type, length ); > for( int i = 0; i < length; i++ ) { > Array.set( retVal, i, Array.get( array, i) ); > } > return (T[])retVal; > } > } > > > Useage: > > public static void main( String[] args ) > { > int[] test = {1,2,3}; > System.out.println( Arrays.asList( autoBox( Integer.class, test ) ) ); > } > > prints: > [1, 2, 3] Oh, I see the problem now... import java.util.Arrays; public class AsList { public static void main(String[] args) { Integer[] boxes = { 1, 2, 3 }; int[] primitives = { 1, 2, 3 }; System.out.println(Arrays.asList(boxes)); System.out.println(Arrays.asList(primitives)); } } [alan(a)postojna ~]$ javac AsList.java [alan(a)postojna ~]$ java AsList [1, 2, 3] [[I(a)10d448] Nice solution, Mark Space. I looked into removing the unsafe array cast, but it appears as though it can't be helped. -- Alan Gutierrez - alan(a)blogometer.com - http://twitter.com/bigeasy
From: Lew on 22 Jul 2010 21:24 markspace wrote: >> [some stuff] Alan Gutierrez wrote: > Nice solution, Mark Space. Case counts. Spaces count. -- Lew
From: Alan Gutierrez on 22 Jul 2010 22:22
Lew wrote: > markspace wrote: > >> [some stuff] > > Alan Gutierrez wrote: >> Nice solution, Mark Space. > > Case counts. Spaces count. Nice solution, markspace. -- Alan Gutierrez - alan(a)blogometer.com - http://twitter.com/bigeasy |