> This happens all the time.
<opalpa@gmail.com> wrote in message
news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
>> This happens all the time.
>
> Roll your own (function or class)? Also, in google groups, there is an
> advert on the right side for IntegerList.
Hmmmm . . . .
Would you (or someone) mind showing me how to make a function which takes a
collection, using generics and perform a "virtual" dynamic array type cast?
Assuming this is possible
Mystery function/class converts Collection <Type X> to Type Y[] where type Y
is assumed to be a primitive. Of course I assume Type X contains a function
to convert instance of class X to primitive type Y (eg: int
Integer.intValue())
Choice B is hard code it for double's and int's - this I could do easily.
Knowing that java allows you generate an inline class and pass it as a
parameter, leads me to believe that there is some way to do the above.
--
LTP
:)
opalpa@gmail.com opalinski from opalpaweb - 24 Jun 2006 17:31 GMT
Does this code do what you request?
package experiment;
import java.util.*;
import java.lang.reflect.Array;
public class ToPrimitiveArray {
private ToPrimitiveArray() {}
/** take a list, make an array of specified type, including
primitive type */
static Object to(List l, Class destType) {
int len = l.size();
Object dest = Array.newInstance(destType,len);
for (int i=0; i<len; i++)
Array.set(dest, i, l.get(i));
return dest;
}
static public void main(String args[]) {
ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(4);
al.add(7);
al.add(17);
int a[] = (int[]) to(al, Integer.TYPE);
for (int i=0; i<a.length; i++)
System.out.println("a["+i+"]="+a[i]);
double b[] = (double[]) to(al, Double.TYPE);
for (int i=0; i<b.length; i++)
System.out.println("b["+i+"]="+b[i]);
}
}
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
> <opalpa@gmail.com> wrote in message
> news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
[quoted text clipped - 23 lines]
>
> :)
Luc The Perverse - 24 Jun 2006 20:07 GMT
<opalpa@gmail.com> wrote in message
news:1151166670.641019.243270@y41g2000cwy.googlegroups.com...
> Does this code do what you request?
>
[quoted text clipped - 12 lines]
> return dest;
> }
Well . . . probably. I will have to try it
--
LTP
:)
P.Hill - 24 Jun 2006 23:17 GMT
What about use of ArrayList.toArray()?
public Object[] toArray(Object[] a)
use ArrayList to build the set of values then use this
method to create an actual array.
Why can't you use an ArrayList directly, Collection interface are very
nice and have kinds of useful conversions an alternatives available.
-Paul
Jeffrey Schwab - 24 Jun 2006 23:35 GMT
> What about use of ArrayList.toArray()?
> public Object[] toArray(Object[] a)
That produces an array of references. Luc wants an array of primitives.
> use ArrayList to build the set of values then use this
> method to create an actual array.
>
> Why can't you use an ArrayList directly, Collection interface are very
> nice and have kinds of useful conversions an alternatives available.
Dimitri Maziuk - 27 Jun 2006 08:40 GMT
Luc The Perverse sez:
><opalpa@gmail.com> wrote in message
> news:1151088549.355893.137140@i40g2000cwc.googlegroups.com...
[quoted text clipped - 8 lines]
> collection, using generics and perform a "virtual" dynamic array type cast?
> Assuming this is possible
Not really, the best you can do is a function for each type
(you can overload the name):
void toArray( int[] dst, Collection src )
foreach s in src,
if s is convertable to int,
dst[i] = convert( s )
void toArray( long[] dst, Collection src )
...
(Note that generics won't help you there, either.)
Dima

Signature
The speed at which a mistyped command executes is directly proportional
to the amount of damage done. -- Joe Zeff