473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Array Question

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
5 5255
-----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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
8
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.