p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_java thread: Re: Chap4 question 3 - a different way - dynamic array...


Message #1 by "digi" <digitalsy@h...> on Tue, 16 Apr 2002 12:08:52
> Hey,
I> 've been going back to past examples trying to figure out if it's 
p> ossible to do them an easier way.

> I want to do question 3 from Chap4(from a string variable of text, 
create 
a> n array and sort the words alphabetically...) but i want to simplify 
the 
p> rocedure.
i> n the first while loop is is possible to add to an array dynamically, 
t> hat is define an array and then gradually add to it during each 
iteration 
o> f the loop?
-> -------------------------------------
p> ublic class Sort {
 >    
 >    public static void main(String[] args) {
 >        
 >       String myText = "In the beginning was the Word, and the Word 
was " +
 >                    "with God and the Word was God. He was with God in " 
+
 >                    "the beginning. Through him all things were made; " +
 >                    "without him nothing was made that has been made. " +
 >                   "In him was life, and that life was the light 
ofmen. " +
 >                    "The light shines in the darkness, but the 
darkness " +
 >                    "has not understood it.";
i> nt startpos = 0;
i> nt endpos = 0;
i> nt counter = 0;
S> tring seperator = " ";

> while(endpos != -1) {
 >  endpos = myText.indexOf(seperator,startpos);
 >  startpos = endpos + 1;
 >  counter++; 

> In that while loop i want to add a line such as textArray[counter] = 
m> yText.substring(startpos,endpos);

> but that requires an array to be defined with a specific size, which i 
d> on't know, i want it to be able to resize itself dynamically as each 
word 
i> s added.

> Any help would be appreciated...

> -Julian
Message #2 by "digi" <digitalsy@h...> on Tue, 16 Apr 2002 12:04:58
Hey,
I've been going back to past examples trying to figure out if it's 
possible to do them an easier way.

I want to do question 3 from Chap4(from a string variable of text, create 
an array and sort the words alphabetically...) but i want to simplify the 
procedure.
in the first while loop is is possible to add to an array dynamically, 
that is define an array and then gradually add to it during each iteration 
of the loop?
--------------------------------------
public class Sort {
    
    public static void main(String[] args) {
        
       String myText = "In the beginning was the Word, and the Word was " +
                    "with God and the Word was God. He was with God in " +
                    "the beginning. Through him all things were made; " +
                    "without him nothing was made that has been made. " +
                   "In him was life, and that life was the light ofmen. " +
                    "The light shines in the darkness, but the darkness " +
                    "has not understood it.";
int startpos = 0;
int endpos = 0;
int counter = 0;
String seperator = " ";

while(endpos != -1) {
  while(endpos != -1) {
  endpos = myText.indexOf(seperator,startpos);
  startpos = endpos + 1;
  counter++; 

In that while loop i want to add a line such as textArray[counter] = 
myText.substring(startpos,endpos);

but that requires an array to be defined with a specific size, which i 
don't know, i want it to be able to resize itself dynamically as each word 
is added.

Any help would be appreciated...

-Julian

  Return to Index