Previous topic | Next topic | Ada Home Page | Index

Arrays of Arrays

With 1D arrays, we have said that each element may be of any type

2D array vs. Array of arrays

Where a 2D array has two indices, and you specify them both at once to pick out the element you want, an array of arrays has one index into each array.

Two dimensional array:

    type MAT45 is array (1 .. 4, 1 .. 5) of INTEGER;

    R : MAT45;

Array of arrays:

    type ROW5 is array (1 .. 5) of INTEGER;

    type MATRIX35 is array (1 .. 3) of ROW5;

    X : MATRIX35;

An array of STRINGs is a special case of arrays of arrays:

    subtype NAMES is STRING (1 .. 20);

    type REGISTERS is array (POSITIVE range <>) of NAMES;


Using Arrays of Arrays

To refer to an element of an array of arrays, you must specify indexes separately

	X(2)		entire second row of array X
	X(2)(3)		a single element of row 2 of X

Note the difference from 2D matrix reference.


Advantages and disadvantages

Advantages of arrays of arrays

Disadvantages of arrays of arrays

    type ROW5 is array (1 .. 5) of INTEGER;

    type MATRIXN5 is array (POSITIVE range <>) of ROW5;


Previous topic | Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96