'dynamic arrays' in java

J

Jeremy Watts

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
 
M

Matt Humphrey

Jeremy Watts said:
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 (e-mail address removed) http://www.iviz.com/
 
R

Robert Klemme

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
 
S

Simon Brooke

Jeremy Watts said:
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.
 
T

trippy

Jeremy Watts said:
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"
 
P

Patricia Shanahan

Jeremy said:
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
 
L

LaieTechie

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top