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
Matt Humphrey - 04 Nov 2006 16:45 GMT
>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
[quoted text clipped - 11 lines]
> whose
> size may change throughout the running of a program
java.util.ArrayList
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
Robert Klemme - 04 Nov 2006 17:22 GMT
>> 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
[quoted text clipped - 13 lines]
>
> 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
Simon Brooke - 04 Nov 2006 17:45 GMT
> 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
[quoted text clipped - 8 lines]
> 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.

Signature
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; Conservatives are not necessarily stupid,
;; but most stupid people are conservatives -- J S Mill
Daniel Pitts - 05 Nov 2006 00:07 GMT
> > 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
[quoted text clipped - 10 lines]
>
> You're looking for a Vector.
I doubt he's looking for a Vector, ArrayList is much more appropriate
in most circumstances.
> --
> simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
>
> ;; Conservatives are not necessarily stupid,
> ;; but most stupid people are conservatives -- J S Mill
LaieTechie - 08 Nov 2006 08:53 GMT
>> 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
trippy - 04 Nov 2006 22:10 GMT
> 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
[quoted text clipped - 9 lines]
>
> thanks
Use ArrayList instead.

Signature
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"
Patricia Shanahan - 04 Nov 2006 23:43 GMT
> 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
[quoted text clipped - 7 lines]
> 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