Re: Array Question
From: Tony Morris (dibblego_at_optusnet.com.au)
Date: 12/28/03
- Previous message: Tony Morris: "Re: TextField error checking"
- In reply to: Anthony Borla: "Re: Array Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Dec 2003 00:27:40 +1000
Quick criticism (for constructive purposes and also because it irks me!)
ArrayList al = new ArrayList(); // yuk !
List al = new ArrayList(); // much better
Use the interface type wherever appropriate (and it usually is with the
Collections API).
For more info., "Effective Java Programming Guide", Joshua Bloch.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
"Anthony Borla" <ajborla@bigpond.com> wrote in message
news:wDqFb.61002$aT.22966@news-server.bigpond.net.au...
> "Andrew Dixon - Depictions.net" <andrew.dixon@NOREPLY.depictions.net>
wrote
> in message news:%VjFb.2724$8x3.24779027@news-text.cableinet.net...
> > Hi Everyone.
> >
> > Bit new to Java and I have question about arrays. I have wrote
> > some code to look for certain substrings within a larger string
> > which work fine. I would like to store each substring in an array,
> > however I don't know how many there will be. All the example I
> > have found for arrays only show how to predefine the size of the
> > array, however I would like to expand it by one each time I need to.
> > Can anyone let me know how to do this.
> >
>
> Sorry, no can do: arrays are fixed-size objects. In order to have a
> 'growable' or 'resizable' array you:
>
> * Allocate a new, larger array, copy all elements from old array
> to the new array; this, effectively, disposes of the old array
>
> Pretty cumbersome approach - not recommended
>
> * Use a Collection-based object for storage, and only create
> an array from this when needed [assuming you *must*
> have an array with which to work]. You might use an
> 'ArrayList' or a 'Vector'. Quick example:
>
> String[] getArrayOfSubstrings(String bigString)
> {
> ArrayList list = new ArrayList();
> ...
> for (...)
> {
> ...
> list.add(bigString.substring(...));
> ...
> }
> ...
> return ((String[]) list.toArray(new String[list.size()]));
> }
>
> I hope this helps.
>
> Anthony Borla
>
>
>
- Previous message: Tony Morris: "Re: TextField error checking"
- In reply to: Anthony Borla: "Re: Array Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|