Object-Oriented Programming(c) From Problem Solving to Java
Authors: Garrido J. M.
Published year: 2005
Pages: 80-81/184
Buy this book on amazon.com >>

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.



9.3 Referring Individual Elements of an Array

To refer to an individual element in an array, an integer value is used that represents the relative position of the element in the array. This index value is known as the index for the array. The index value starts at 0, and the maximum value is the capacity of the array minus 1.

9.3.1 Arrays of Simple Types

A particular element in the array is denoted by the name of the array followed by the index value enclosed in rectangular brackets. For example, to assign a value of 342.65 to element 7 of array temp , the KJP code is:

set temp[6] = 342.65

The index value can be an integer constant, a constant identifier, or an integer variable. For example, to refer to element 7 of array temp using variable j as the index:

constants integer MAX_TEMP = 10

variables

float temp array [MAX_TEMP] integer j . . . set j = 6 set temp[j] = 342.65

9.3.2 Arrays of Object References

An array of object references is not an array of objects, because the objects are stored elsewhere in memory. To create an object of class Employee and assign its reference to the element with index j of array employees , the KJP code is:

create employees[j] of class Employee

To invoke a public function of an object referenced in an element of an array, the call statement is used as explained before and the name of the array is followed by the index value enclosed in brackets. For example, to invoke function get_salary of the object referenced by the element with index j in array employees , the KJP code is:

variables float obj_salary integer j . . . set obj_salary = call get_salary of employees[j]


Object-Oriented Programming(c) From Problem Solving to Java
Authors: Garrido J. M.
Published year: 2005
Pages: 80-81/184
Buy this book on amazon.com >>