Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > 'dynamic arrays' in java

Reply
Thread Tools

'dynamic arrays' in java

 
 
Jeremy Watts
Guest
Posts: n/a
 
      11-04-2006
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


 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      11-04-2006

"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/


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-04-2006
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
 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      11-04-2006
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
 
Reply With Quote
 
trippy
Guest
Posts: n/a
 
      11-04-2006
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"



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-04-2006
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-04-2006

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


 
Reply With Quote
 
LaieTechie
Guest
Posts: n/a
 
      11-08-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hot Requirements: 1.Sr Java Developer,2.Java Developer (Java with EJB) Isaac Java 0 01-20-2011 08:41 PM
hey i am just started java,, can anyone tell me the use ,application, why java , importance of java.. manish sahu Java 3 02-14-2008 12:00 AM
[JAVA] [EVALUATION] - The Java Failure (Sorry: The Java(tm) Failure) Ilias Lazaridis Java 0 02-01-2005 10:32 AM
JAVA VIRTUAL MUCHINE OR SUN JAVA Fernando Kohan Firefox 1 11-14-2004 02:04 AM
Job to convert Java App 1.3.1 to Java Newest of Java Michael Kintner Java 0 11-30-2003 04:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57