ArrayList -> int[]

Discussion in 'Java' started by Marc Dzaebel, Feb 15, 2004.

  1. Marc Dzaebel

    Marc Dzaebel Guest

    is there a *single* method for the above standard transformation?
    arrayList.toArray() creates an Object[] array rather than an int[] array. Of
    course a simple "for" loop solves the problem.

    Thanks, Marc
     
    Marc Dzaebel, Feb 15, 2004
    #1
    1. Advertising

  2. Marc Dzaebel

    Chris Smith Guest

    Marc Dzaebel wrote:
    > is there a *single* method for the above standard transformation?
    > arrayList.toArray() creates an Object[] array rather than an int[] array. Of
    > course a simple "for" loop solves the problem.


    No.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Feb 15, 2004
    #2
    1. Advertising

  3. Marc Dzaebel

    Andrew Hobbs Guest

    "Marc Dzaebel" <> wrote in message
    news:c0mg97$edd$00$-online.com...
    > is there a *single* method for the above standard transformation?
    > arrayList.toArray() creates an Object[] array rather than an int[] array.

    Of
    > course a simple "for" loop solves the problem.
    >


    No.

    After all, there is no way for a method to know that an ArrayList only
    contains objects which can be converted in some systematic way to an int.

    Andrew


    --
    ********************************************************
    Andrew Hobbs PhD

    MetaSense Pty Ltd - www.metasense.com.au
    12 Ashover Grove
    Carine W.A.
    Australia 6020

    61 8 9246 2026
    metasens AntiSpam @iinet dot net dot au


    *********************************************************


    > Thanks, Marc
    >
    >
     
    Andrew Hobbs, Feb 15, 2004
    #3
  4. Marc Dzaebel

    Jim Moonves Guest

    "Andrew Hobbs" <> wrote in
    news:402ecb18$0$1734$:

    > No.
    >
    > After all, there is no way for a method to know that an ArrayList
    > only contains objects which can be converted in some systematic
    > way to an int.
    >
    > Andrew


    I wonder if 1.5 will change that as primitive to associated Object
    conversions are transparent?
     
    Jim Moonves, Feb 15, 2004
    #4
  5. Marc Dzaebel

    Bjorn Abelli Guest

    "Jim Moonves" <> skrev i meddelandet
    news:Xns948FD4272BE23nonenonecom@216.168.3.44...
    > "Andrew Hobbs" <> wrote in
    > news:402ecb18$0$1734$:
    >
    > > No.
    > >
    > > After all, there is no way for a method to know that
    > > an ArrayList only contains objects which can be
    > > converted in some systematic way to an int.
    > >
    > > Andrew

    >
    > I wonder if 1.5 will change that as primitive to
    > associated Object conversions are transparent?


    Not as your subject indicates, but it will help.

    Lets assume you want to use an ArrayList to store numbers of type "int",
    well that's not possible, but if you use the wrapper Integer to instantiate
    the ArrayList, you can use them in a similar manner, e.g.:

    ArrayList<Integer> array = new ArrayList<Integer>();

    array.add(1);
    array.add(3);

    If you now want to turn them back to an array of "int:s", you still have to
    use the Integer-type:

    Integer[] vektor = (Integer[]) array.toArray(new Integer[0]);

    Since the *object* returned by toArray is an object of *array-type*, there
    can't be any "unboxing" to e.g. an int[]. However, this is not really
    necessary in most cases, since you now can use an Integer-object "almost" as
    it would have been an int:

    int sum = 0;
    for (Integer temp : vektor)
    {
    sum = sum + temp;
    }

    System.out.println(sum);

    // Bjorn A
     
    Bjorn Abelli, Feb 15, 2004
    #5
  6. Marc Dzaebel

    Marc Dzaebel Guest

    "Bjorn Abelli" <> schrieb im Newsbeitrag
    news:c0mmg2$18gsda$-berlin.de...
    > int sum = 0;
    > for (Integer temp : vektor)
    > {
    > sum = sum + temp;
    > }


    Bjorn, these new 1.5 features are in fact valuable and timesaving. Hope
    they're stable soon.

    For completeness, currently the shortest method (<1.5) then is:

    int[] ints=new int[arryList.size()];
    for(int=0; i<ints.length; i++)
    ints=((Integer)arrayList.get(i)).intValue();

    However, a method like "toSimpleArray(Class)" for basic homogeneous
    Collections is possible and could ease the work E.g.:

    class HomogeneousArrayList extends ArrayList {
    public Object toSimpleArray(Class elementType) {
    if(elementType.equals(Integer.class)) {
    int[]ints=new int[size()];
    for(int i=0; i<ints.length; i++)
    ints=((Integer)get(i)).intValue();
    return ints;
    }
    // ... other basic types
    return null;
    }
    }

    The above method could be placed into AbstractCollection but might violate
    safety considerations.

    Thanks for your quality comments!

    Marc
     
    Marc Dzaebel, Feb 15, 2004
    #6
    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. Homa
    Replies:
    4
    Views:
    9,918
  2. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,647
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    457
    Dave Thompson
    Jul 31, 2006
  5. aling
    Replies:
    8
    Views:
    958
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page