Home Segments Index Top Previous Next

376: Mainline

To use an array, once it is created, you need to know how to write into and read from the various locations in the array, each of which is identified by its numerical indexes.

Consider distances, the one-dimensional array of integers. To write data into distances, you use assignment statements in which the array name and a bracketed integer index appear on the left side of an assignment operator, the place where you are accustomed to seeing variable names. The following statement, for example, inserts an integer into the place indexed by the value of counter.

 *-- Array's name 
 | 
 |         *-- An integer 
 |         |  
 |         |          *-- Value to be stored 
 v         v          v 
distances[counter] = 57; 

To read data from the distances array, once the data have been written, you write an expression containing the array name and a bracketed integer index. The following expression, for example, yields the integer stored in the place indexed by the value of counter:

distances[counter]