Avoiding Silliness

L

Luc The Perverse

Hi - I was wondering if the later java versions (1.5 and higher) allow me to
do something to avoid this silliness.

This happens all the time. I am trying to make a list of integers - and I
don't start out knowing how many there are going to be (although typically I
would know a max). At the end, I know how many there are, and I need to
remake my result to generate an array of the correct size. Here is an
example from a top coder problem in which I use a linked List

import java.lang.*;
import java.util.*;

public class InterestingDigits{
public int[] digits(int base){
boolean bad = false;
LinkedList<Integer> li = new LinkedList<Integer>();
for(int i=2;i<base;i++){
bad = false;
for(int j=i + i;j<base*base*base&&!bad;j+=i){
int s = 0;
for(int c=j;c>0;c/=base)
s+=c%base;
if(s%i!=0)
bad=true;
}
if(!bad)
li.add(new Integer(i));
}
int c = 0;
int[] ret = new int[li.size()];
for(Integer i : li)
ret[c++] = i.intValue();
return ret;
}
}

Those last four lines before the return statement are what I have an
objection to.

Is there a function for returning an array subset that I could directly on
an array of type int[] ? Is there a way to cast directly to an int[] from
a linked list (or any collection) ? (Note: I realize that I can generate
an array of type Integer[] easily enough from a collection using
li.toArray(new Integer[0]) but I would need an int[] )
 
L

Luc The Perverse

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.
 
O

opalpa

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);
double b[] = (double[]) to(al, Double.TYPE);
for (int i=0; i<b.length; i++)
System.out.println("b["+i+"]="+b);
}

}

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/



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.
 
L

Luc The Perverse

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;
}


Well . . . probably. I will have to try it
 
P

P.Hill

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
 
J

Jeffrey Schwab

P.Hill said:
What about use of ArrayList.toArray()?
public Object[] toArray(Object[] a)

That produces an array of references. Luc wants an array of primitives.
 
D

Dimitri Maziuk

Luc The Perverse sez:
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

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 = convert( s )

void toArray( long[] dst, Collection src )
...

(Note that generics won't help you there, either.)

Dima
 
D

Dale King

Luc said:
Is there a function for returning an array subset that I could directly on
an array of type int[] ? Is there a way to cast directly to an int[] from
a linked list (or any collection) ? (Note: I realize that I can generate
an array of type Integer[] easily enough from a collection using
li.toArray(new Integer[0]) but I would need an int[] )

I have my own utility class that does this. I'd be happy to share it
with you using a creative commons attribution license.
 
L

Luc The Perverse

Dale King said:
Luc said:
Is there a function for returning an array subset that I could directly
on an array of type int[] ? Is there a way to cast directly to an
int[] from a linked list (or any collection) ? (Note: I realize that I
can generate an array of type Integer[] easily enough from a collection
using li.toArray(new Integer[0]) but I would need an int[] )

I have my own utility class that does this. I'd be happy to share it with
you using a creative commons attribution license.

I appreciate it - but I think I am ok now.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top