Associative Arrays
This lesson explains how associative arrays can be used in D and the different methods that can be used with associative arrays.
Associative arrays #
Associative array is a feature that is found in most modern high-level languages. They are very fast data structures that work like mini databases and are used in many programs.
We have seen in the arrays lesson that plain arrays are containers that store their elements side-by-side and provide access to them by index. An array that stores the names of the days of the week can be defined like this:
string[] dayNames =
[ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ];
Note:
string
datatype will be covered later in this chapter.
The name of a specific day can be accessed by its index in that array:
writeln(dayNames[1]); // prints "Tuesday"
How are associative arrays different from plain arrays? #
-
The fact that plain arrays provide access to their values through index numbers can be described as an association of indexes with values. In other words, arrays map indexes to values. Plain arrays can use only integers as indexes.
Associative arrays allow indexing not only using integers but also using any other type. They map the values of one type to the ...