9.2 Declaring Arrays


9.2 Declaring Arrays

In an array declaration, an identifier is used for the name of the array. The type of array can be of a simple (primitive) type or a class. The capacity of the array is the number of elements it can hold. The general KJP statement to declare an array of a simple type is:

        array_type   array_name  array [  capacity  ] 

Arrays of simple types must be declared in the variables section for data definitions. For example, the KJP declaration of array temp of type float and with capacity of 10 elements is:

         variables             float temp array [10]             . . . 

The declaration of an array of object references is similar to the declaration of object references. The general KJP statement for declaring arrays of object references is:

        object  array_name  array [  capacity  ]                  of class  class_name  

Arrays of object references must be declared in the objects section for data definitions. For example, the KJP declaration of array employees of class Employee and with 50 elements of capacity is:

        objects            object employees array [50] of class Employee            . . . 

A more convenient and recommended manner to declare an array is to use an identifier constant with the value of the capacity of the array. For example, assume the constant MAX_TEMP has a value 10 and NUM_OBJECTS a value of 25; the declaration of the array temp and array employees is:

        constants            integer MAX_TEMP = 10            integer NUM_OBJECTS = 25        variables            float temp array [MAX_TEMP]        objects            object employees array [NUM_OBJETS] of                                     class Employee            . . . 

The declaration of an array indicates the type of value that the elements of the array can hold. It also indicates the name and total number of elements of the array. In most practical problems, the number of elements manipulated in the array is less than the total number of elements. For example, an array is declared with a capacity of 50 elements, but only the first 20 elements of the array are used. Because the array is a static data structure, elements cannot be inserted or deleted from the array, only the values of the elements can be updated.




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