9.3 Referring Individual Elements of an Array


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
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