By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Array Question

P: n/a
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.

Thanks

Best Regards
Andrew

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Dixon - Depictions.net wrote:
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.

Thanks

Best Regards
Andrew


Hi,
I think you'd like to try using java.util.ArrayList. It's really
simple to use, you can keep adding objects over and over without
knowing the initial size, and you can convert it to a real array when
you're finished very easily. Take a look!

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/5eaQwxczzJRavJYRAqqbAJ46RB8qDqoy5FjXtZ2cFa2rNGan6Q Cfa67F
hKzHPH2mx0Ufg8RKWbttnrc=
=G2b8
-----END PGP SIGNATURE-----
Jul 17 '05 #2

P: n/a
Excellent. Thanks. All working now.

Best Regards
Andrew Dixon

"Chris" <ch*******@hotmail.com> wrote in message
news:0hoFb.15037$ss5.8580@clgrps13... -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Andrew Dixon - Depictions.net wrote:
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.

Thanks

Best Regards
> Andrew


Hi,
I think you'd like to try using java.util.ArrayList. It's really
simple to use, you can keep adding objects over and over without
knowing the initial size, and you can convert it to a real array when
you're finished very easily. Take a look!

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/5eaQwxczzJRavJYRAqqbAJ46RB8qDqoy5FjXtZ2cFa2rNGan6Q Cfa67F
hKzHPH2mx0Ufg8RKWbttnrc=
=G2b8
-----END PGP SIGNATURE-----

Jul 17 '05 #3

P: n/a
"Andrew Dixon - Depictions.net" <an**********@NOREPLY.depictions.net> wrote
in message news:%V*********************@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

Jul 17 '05 #4

P: n/a
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" <aj*****@bigpond.com> wrote in message
news:wD******************@news-server.bigpond.net.au...
"Andrew Dixon - Depictions.net" <an**********@NOREPLY.depictions.net> wrote in message news:%V*********************@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

Jul 17 '05 #5

P: n/a
Why is this a big deal? If I have a method on an object that takes in a
java.util.List as a parameter, either one would be acceptable.

"Tony Morris" <di******@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...
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" <aj*****@bigpond.com> wrote in message
news:wD******************@news-server.bigpond.net.au...
"Andrew Dixon - Depictions.net" <an**********@NOREPLY.depictions.net>

wrote
in message news:%V*********************@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


Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.