Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
9.1 Introduction
There is often the need to declare and use a large number of variables of the same type and carry out the same calculations on each of these variables. Most programming languages provide a mechanism to handle large number of values in a single collection and to refer to each value with an index.
An array is a data structure that can store multiple values of the same type. These values are stored using contiguous memory locations under the same name. The values in the array are known as elements. To access an element in a particular location or slot of the array, an integer value known as index is used. This index represents the relative position of the element in the array; the values of the index start from zero. Figure 9.1 illustrates the structure of an array with 10 elements.
An array is a static data structure; after the array is declared (and created), its capacity cannot change. For example, if an array is declared to hold 15 elements, it cannot be changed to hold a larger or smaller number of elements. Using arrays involves three steps:
-
Array declaration
-
Array initialization, or assigning initial values to the array elements
-
Element referencing to access and update the value of the elements
Категории