'dynamic arrays' in java

Discussion in 'Java' started by Jeremy Watts, Nov 4, 2006.

  1. Jeremy Watts

    Jeremy Watts Guest

    i am writing a java method which needs to take note of a set of numbers
    generated by the routine as it runs. the problem is that i dont know
    beforhand how many of these numbers will be generated, so the simple use of
    an array cant be used as java seems to insist of the size of the array to be
    declared upfront, where as i say i dont have this information prior to
    running the method.

    there are other java data constructs but i am a bit bemused as to what would
    be appropriate here. i am no java expert so what i am looking for is what
    used (or still is i dont know) to be called a 'dynamic array' ie. one whose
    size may change throughout the running of a program

    thanks
    Jeremy Watts, Nov 4, 2006
    #1
    1. Advertising

  2. "Jeremy Watts" <> wrote in message
    news:Zh23h.5818$...
    >i am writing a java method which needs to take note of a set of numbers
    > generated by the routine as it runs. the problem is that i dont know
    > beforhand how many of these numbers will be generated, so the simple use
    > of
    > an array cant be used as java seems to insist of the size of the array to
    > be
    > declared upfront, where as i say i dont have this information prior to
    > running the method.
    >
    > there are other java data constructs but i am a bit bemused as to what
    > would
    > be appropriate here. i am no java expert so what i am looking for is what
    > used (or still is i dont know) to be called a 'dynamic array' ie. one
    > whose
    > size may change throughout the running of a program


    java.util.ArrayList

    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Nov 4, 2006
    #2
    1. Advertising

  3. On 04.11.2006 16:45, Matt Humphrey wrote:
    > "Jeremy Watts" <> wrote in message
    > news:Zh23h.5818$...
    >> i am writing a java method which needs to take note of a set of numbers
    >> generated by the routine as it runs. the problem is that i dont know
    >> beforhand how many of these numbers will be generated, so the simple use
    >> of
    >> an array cant be used as java seems to insist of the size of the array to
    >> be
    >> declared upfront, where as i say i dont have this information prior to
    >> running the method.
    >>
    >> there are other java data constructs but i am a bit bemused as to what
    >> would
    >> be appropriate here. i am no java expert so what i am looking for is what
    >> used (or still is i dont know) to be called a 'dynamic array' ie. one
    >> whose
    >> size may change throughout the running of a program

    >
    > java.util.ArrayList


    If numbers are ints and they are later used for lookups (rather than
    iterating) a BitSet may also serve well here.

    Kind regards

    robert
    Robert Klemme, Nov 4, 2006
    #3
  4. Jeremy Watts

    Simon Brooke Guest

    in message <Zh23h.5818$>, Jeremy Watts
    ('') wrote:

    > i am writing a java method which needs to take note of a set of numbers
    > generated by the routine as it runs. the problem is that i dont know
    > beforhand how many of these numbers will be generated, so the simple use
    > of an array cant be used as java seems to insist of the size of the array
    > to be declared upfront, where as i say i dont have this information prior
    > to running the method.
    >
    > there are other java data constructs but i am a bit bemused as to what
    > would be appropriate here. i am no java expert so what i am looking for
    > is what
    > used (or still is i dont know) to be called a 'dynamic array' ie. one
    > whose size may change throughout the running of a program


    You're looking for a Vector.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/

    ;; Conservatives are not necessarily stupid,
    ;; but most stupid people are conservatives -- J S Mill
    Simon Brooke, Nov 4, 2006
    #4
  5. Jeremy Watts

    trippy Guest

    In article <Zh23h.5818$>, Jeremy Watts
    took the hamburger meat, threw it on the grill, and I said "Oh Wow"...

    > i am writing a java method which needs to take note of a set of numbers
    > generated by the routine as it runs. the problem is that i dont know
    > beforhand how many of these numbers will be generated, so the simple use of
    > an array cant be used as java seems to insist of the size of the array to be
    > declared upfront, where as i say i dont have this information prior to
    > running the method.
    >
    > there are other java data constructs but i am a bit bemused as to what would
    > be appropriate here. i am no java expert so what i am looking for is what
    > used (or still is i dont know) to be called a 'dynamic array' ie. one whose
    > size may change throughout the running of a program
    >
    > thanks
    >


    Use ArrayList instead.

    --
    trippy
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

    NP: "All I Really Want" -- Alanis Morissette

    "Now, technology's getting better all the time and that's fine,
    but most of the time all you need is a stick of gum, a pocketknife,
    and a smile."

    -- Robert Redford "Spy Game"
    trippy, Nov 4, 2006
    #5
  6. Jeremy Watts wrote:
    > i am writing a java method which needs to take note of a set of numbers
    > generated by the routine as it runs. the problem is that i dont know
    > beforhand how many of these numbers will be generated, so the simple use of
    > an array cant be used as java seems to insist of the size of the array to be
    > declared upfront, where as i say i dont have this information prior to
    > running the method.
    >
    > there are other java data constructs but i am a bit bemused as to what would
    > be appropriate here. i am no java expert so what i am looking for is what
    > used (or still is i dont know) to be called a 'dynamic array' ie. one whose
    > size may change throughout the running of a program


    The closest to dynamic array is ArrayList, an array based implementation
    of the java.util.List interface.

    However, in the first sentence you mention a "set of numbers". If the
    only reason for associating an order with them is because you were
    thinking of putting them in an array, then consider a java.util.Set
    implementation instead.

    In particular, if you frequently ask "Is n in the result set?" then a
    HashSet may be more efficient and appropriate than any List implementation.

    If the numbers are reasonably small, BitSet may also be a good choice.

    If you have not already done so, take a look at the API documents for
    java.util. It has a lot of classes for solving "how do I store this
    data?" questions.

    Patricia
    Patricia Shanahan, Nov 4, 2006
    #6
  7. Jeremy Watts

    Daniel Pitts Guest

    Simon Brooke wrote:
    > in message <Zh23h.5818$>, Jeremy Watts
    > ('') wrote:
    >
    > > i am writing a java method which needs to take note of a set of numbers
    > > generated by the routine as it runs. the problem is that i dont know
    > > beforhand how many of these numbers will be generated, so the simple use
    > > of an array cant be used as java seems to insist of the size of the array
    > > to be declared upfront, where as i say i dont have this information prior
    > > to running the method.
    > >
    > > there are other java data constructs but i am a bit bemused as to what
    > > would be appropriate here. i am no java expert so what i am looking for
    > > is what
    > > used (or still is i dont know) to be called a 'dynamic array' ie. one
    > > whose size may change throughout the running of a program

    >
    > You're looking for a Vector.

    I doubt he's looking for a Vector, ArrayList is much more appropriate
    in most circumstances.
    >
    > --
    > (Simon Brooke) http://www.jasmine.org.uk/~simon/
    >
    > ;; Conservatives are not necessarily stupid,
    > ;; but most stupid people are conservatives -- J S Mill
    Daniel Pitts, Nov 4, 2006
    #7
  8. Jeremy Watts

    LaieTechie Guest

    On Sat, 04 Nov 2006 15:07:55 -0800, Daniel Pitts wrote:

    > Simon Brooke wrote:
    >> You're looking for a Vector.

    > I doubt he's looking for a Vector, ArrayList is much more appropriate in
    > most circumstances.


    Always declare using the List interface, that way you can easily change
    the actual implementation to see which gives you the best performance.

    HTH,
    La`ie Techie
    LaieTechie, Nov 8, 2006
    #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,589
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,249
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

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

    Arrays and Pointers to Arrays

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

Share This Page