You will learn about the following in this chapter:
Why the idea of putting many values of the same type into the same data structure (the array) is so compelling
How to declare, define, initialize, and apply one-dimensional arrays
How arrays are effectively accessed with loop statements
The iteration statement foreach and how this statement, compared to the for-loop, can simplify access to an array
The implications of the fact that an array is a reference type.
How arrays can be provided as arguments to methods and as return values from methods
The implications of letting the values of an array be references to objects
The ability to let arrays be instance variables of classes and the associated advantages
In previous chapters we have worked with only a few variables in the sample programs presented. The elevator simulation program in Chapter 5, "Your First Object-Oriented Program," dealt with just one or two elevators interacting with merely one person, and a later example contained only one accountBalance. However, our programs often need to represent much larger numbers of similar items. Some skyscrapers can contain 30 or more elevators, transporting thousands of people every day, and banks have to manage tens of thousands of account balances.
How can we effectively and conveniently represent large amounts of similar data items in our source code? We could, as a first hunch, attempt to declare the items one by one in a similar fashion to what we have done before. For a bank simulation program attempting to represent an unchanging amount of different account balances, say 3000, it could look like the following:
decimal accountBalance1; decimal accountBalance2; ... decimal accountBalance1000; decimal accountBalance1001; ... decimal accountBalance3000;
which declares 3000 different variables of type decimal using 3000 lines of source code.
However, this approach poses a couple of serious problems:
Not only is it a nuisance, but it is also time and memory consuming to write the thousands of source code lines required.
It is impossible to traverse the list of variables by using a loop statement, because we are dealing with 3000 different names. A loop construct needs a single name combined with an index that can be incremented to access different values; just like the string type allows a loop to traverse its characters through a single name and an index enclosed in square brackets (example: myText[i] allows the counter i to determine which character we access).
Thus, if we needed to, say, perform a routine addition of interest to all the accounts, we would have to write each visit explicitly in the code, such as
accountBalance1 = accountBalance1 + accountBalance1 * interestRate; accountBalance2 = accountBalance2 + accountBalance2 * interestRate; ... accountBalance3000 = accountBalance3000 + accountBalance3000 * interestRate
requiring another 3000 lines of code.
To solve these problems, we need a construct that can hold and represent an entire collection of similar data items and that possesses the following characteristics:
The construct can be declared simply by using one or two lines of source code, disregarding the number of items it is meant to represent.
Each item in the collection can be accessed via just one name plus a unique index (such as accountBalances), allowing a simple loop construct to traverse the entire list.
The array class presented and discussed in this chapter holds those and many other qualities, making it suitable to represent small and large collections of data items of the same type.