dynamic arrays

Discussion in 'Java' started by segex, Aug 25, 2005.

  1. segex

    segex Guest

    Hi all

    I was wondering if it is possible to make an array dynamicly. I don't
    the easy newbie stuff like
    -----
    int[] meth(int size) {
    return new int[size];
    }
    -----
    I actually more though of making an array of whatever (primitiv-type or
    class and all sorts of dimensions)
    so more somthing like:
    -----
    /*
    * Signature is something similar to
    * [I for int[] or
    * [[Ljava.lang.String for String[][]
    * etc
    */
    Object makeArr(String signature) {
    return <made array>
    }
    -----

    G. Segex
    segex, Aug 25, 2005
    #1
    1. Advertising

  2. Hi everyone,

    You can use the Java ArrayList class

    Richard West
    freesoft_2000, Aug 25, 2005
    #2
    1. Advertising

  3. segex wrote:
    >
    > /*
    > * Signature is something similar to
    > * [I for int[] or
    > * [[Ljava.lang.String for String[][]
    > * etc
    > */
    > Object makeArr(String signature) {
    > return <made array>
    > }


    java.lang.reflect.Array.newInstance(Class<?>,int)

    Typical implementations of Collection.toArray(Object[]) use it.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Aug 25, 2005
    #3
  4. freesoft_2000 coughed up:
    > Hi everyone,
    >
    > You can use the Java ArrayList class
    >
    > Richard West



    Have you lost your @#$%ing mind, or did you simply not read his post?
    Thomas G. Marshall, Aug 26, 2005
    #4
  5. segex

    segex Guest

    Oh, didn't think it was that simple

    well thanks a lot :)
    segex, Aug 26, 2005
    #5
  6. Thomas G. Marshall wrote:
    > freesoft_2000 coughed up:
    >
    >>Hi everyone,
    >>
    >> You can use the Java ArrayList class
    >>
    >>Richard West

    >
    >
    >
    > Have you lost your @#$%ing mind, or did you simply not read his post?
    >
    >


    I thought he was just giving people permission to use the ArrayList
    class. After all, he didn't quote anybody, so who knows? And he did
    address the post to everybody, not just the OP.

    So now we can rest easy knowing that Richard West is allowing us to use
    the ArrayList class. The one I'm sweating out is HashMap....

    Ray

    --
    XML is the programmer's duct tape.
    Raymond DeCampo, Aug 26, 2005
    #6
  7. segex

    Roedy Green Guest

    On Thu, 25 Aug 2005 15:57:51 +0100, Thomas Hawtin
    <> wrote or quoted :

    >
    >java.lang.reflect.Array.newInstance(Class<?>,int)


    the other way to do it without reflection would be a switch on
    dimension to create 1 to 10 dimensions

    e.g.

    case 3:
    return new Qbject[a0][a1][a2];
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Aug 28, 2005
    #7
  8. Roedy Green wrote:
    > On Thu, 25 Aug 2005 15:57:51 +0100, Thomas Hawtin
    > <> wrote or quoted :
    >
    >
    >>java.lang.reflect.Array.newInstance(Class<?>,int)

    ^length
    >
    >
    > the other way to do it without reflection would be a switch on
    > dimension to create 1 to 10 dimensions
    >
    > e.g.
    >
    > case 3:
    > return new Qbject[a0][a1][a2];


    A different issue. I believe G. Segex was primarily looking at creating
    arrays of different types rather than dimensions. You can't switch on
    all possible types ever.

    To create an array (of arrays)^n use:

    java.lang.reflect.Array.newInstance(Class<?>,int[] dimensions)

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Aug 28, 2005
    #8
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,529
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,225
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    550
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    371
  5. Philipp
    Replies:
    21
    Views:
    1,100
    Philipp
    Jan 20, 2009
Loading...

Share This Page