Chapter 9: Arrays


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.

click to expand
Figure 9.1: An array named temp 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:

  1. Array declaration

  2. Array initialization, or assigning initial values to the array elements

  3. Element referencing to access and update the value of the elements




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net