Arrays of records
Records are treated as data types, so they can be held within a single array.
This allows for storage of more than one record within the same structure. This structure is essentially an array of records.
Instead of using four arrays with four data types (name, population, area, capital):
- Use of a record could allow the program to store all of the desired information for each city within an array called scottish_cities
- The data type for the array of records would be the name of the record type, in this case cityData
In summary - all of the data for each city could be stored within an array called scottish_cities holding values of the data type cityData:
DECLARE scottish_cities AS ARRAY OF cityData INITIALLY [Glasgow, Dundee, Perth, Aberdeen, Stirling, Edinburgh, Inverness ]
The table below simplifies the way that this data would be held in memory.
Empty array
In the example above, we had already created variables for each city. These were then added to the array of records.
However, sometimes the variables may not yet exist and we may want to create an empty array.
To declare an empty array of records capable of storing data on seven cities and using the cityData record type, the following line of reference language would be necessary:
DECLARE scottish_cities AS ARRAY OF cityData INITIALLY [[]]* 7
This would create an array capable of storing seven variables using the cityData record type. Even though the number 7 is used when declaring the array, indexing for seven variables would be from 0 鈥 6.
Each programming language will use a different method of defining a record type:
- In some languages the term record is not used but an equivalent construct exists.
- In other languages it is not possible to create a record in the way that we would understand the theory for this course.
Object-oriented languages
In an object-oriented context a class is a record with associated methods. A class called city could hold all of the instance variables and methods that would be required for each city.
If you are using object-oriented programming languages you may want to research the use of records in procedural environments to get a better understanding of how they are applied in other languages.