Creating an Array

To create an array, you must specify the elements and index values. In Table 6-2, we show a sample associative array that uses household objects and relates them to strings that describe their shapes.

Table 6-2. An associative array that relates objects to their shapes

Key Value

Soda can Cylinder

Notepad Rectangle

Apple Sphere

Orange Sphere

Phone book Rectangle

The elements of an array can be anything, including strings, numbers, and even other arrays. The key field must be a scalar. Scalar values are simple values such as a number, text, or Boolean value, not data that can have more than one value such as an array or an object. The key field of an array must also be unique for each element; otherwise you may overwrite the same value. Should you attempt to assign a value using a key you specified already, the new value simply replaces the old value.

Short yet meaningful values for your index keys make your programs run faster, which will make them easier to maintain.

Was this article helpful?

0 0

Post a comment