Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 5. Array Data Structures > 5.9 DYNAMICALLY DECLARING ARRAYS

5.9 DYNAMICALLY DECLARING ARRAYS

The arrays considered so far were declared 'statically'. Memory locations are set aside when the compiler translates the source code, and this storage remains allocated until the program ends.

Examine the code fragment of Listing 5.6; it prompts the user to enter a maximum of 10 names. The user may store fewer than 10, by entering 'Z' instead of a name. Note how the boolean variable finish determines whether the while loop runs for 10 times or fewer. Loop control uses a compound condition by testing both finish and the value of counter i. If finish becomes true before i reaches 10, the loop stops. If not, it runs 10 times, storing all the 10 names. One of the methods of the String class, equals, tests whether 'Z' has been entered.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint