Avoiding Silliness

Discussion in 'Java' started by Luc The Perverse, Jun 23, 2006.

  1. 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[] )

    --
    LTP

    :)
     
    Luc The Perverse, Jun 23, 2006
    #1
    1. Advertising

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

    Opalinski

    http://www.geocities.com/opalpaweb/
     
    opalinski from opalpaweb, Jun 23, 2006
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    >> 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

    :)
     
    Luc The Perverse, Jun 24, 2006
    #3
  4. 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

    http://www.geocities.com/opalpaweb/




    Luc The Perverse wrote:
    > <> wrote in message
    > news:...
    > >> 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
    >
    > :)
     
    opalinski from opalpaweb, Jun 24, 2006
    #4
  5. <> wrote in message
    news:...
    > 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

    --
    LTP

    :)
     
    Luc The Perverse, Jun 24, 2006
    #5
  6. Luc The Perverse

    P.Hill Guest

    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
     
    P.Hill, Jun 24, 2006
    #6
  7. P.Hill wrote:
    >
    > 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.
     
    Jeffrey Schwab, Jun 24, 2006
    #7
  8. Luc The Perverse sez:
    ><> wrote in message
    > news:...
    >>> 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


    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
    --
    The speed at which a mistyped command executes is directly proportional
    to the amount of damage done. -- Joe Zeff
     
    Dimitri Maziuk, Jun 27, 2006
    #8
  9. Luc The Perverse

    Dale King Guest

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

    --
    Dale King
     
    Dale King, Jun 28, 2006
    #9
  10. "Dale King" <> wrote in message
    news:...
    > Luc The Perverse wrote:
    >>
    >> 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.

    --
    LTP

    :)
     
    Luc The Perverse, Jun 28, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith R. Williams

    Re: Avoiding latches

    Keith R. Williams, Jul 14, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,197
    Keith R. Williams
    Jul 14, 2003
  2. Ken Smith

    Re: Avoiding latches

    Ken Smith, Jul 15, 2003, in forum: VHDL
    Replies:
    3
    Views:
    2,597
    Tim Hubberstey
    Jul 17, 2003
  3. Jan De Ceuster

    Re: Avoiding latches

    Jan De Ceuster, Jul 15, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,711
    Jan De Ceuster
    Jul 15, 2003
  4. Mastupristi

    avoiding GCLK

    Mastupristi, Nov 20, 2003, in forum: VHDL
    Replies:
    7
    Views:
    4,240
    sharat babu
    Dec 10, 2003
  5. Larry Gates

    Re: Just a bit of silliness

    Larry Gates, Feb 23, 2009, in forum: C Programming
    Replies:
    45
    Views:
    1,110
    David Thompson
    Mar 9, 2009
Loading...

Share This Page