Up to this point, you have worked with one- and two-dimensional arrays, but arrays can be larger than two dimensions.
Most high-level programming languages provide a convenient way to create and manage multi-dimensional arrays while others require a more hands-on approach. C++ and Java are two examples of languages that provide multi-dimensional arrays as part of the language. Python, of course, does not directly support arrays of any dimension. But that did not prevent us from defining and implementing abstract data types for one- and two-dimensional arrays. Likewise, we can define an abstract data type for creating and using arrays of any dimension.
To accommodate multi-dimensional arrays of two or more dimensions, we define the MultiArray ADT and as with the earlier array abstract data types, we limit the operations to those commonly provided by arrays in most programming languages that provide the array structure.
The MultiArray ADT
A multi-dimensional array consists of a collection of elements organized into multiple dimensions. Individual elements are referenced by specifying an n-tuple or a subscript of multiple component indices, (), one for each dimension of the array. All indices start at 0.
MultiArray(dim1, dim2, ... dimN)
Creates a multi-dimensional array of elements organized into $n$-dimensions with each element initially set to None . The number of dimensions, which is specified by the number of arguments, must be greater than 1. The individual arguments, all of which must be greater than zero, indicate the lengths of the corresponding array dimensions. The dimensions are specified from highest to lowest, where dim1 is the highest possible dimension and dimN is the lowest.
dims()
Returns the number of dimensions in the multi-dimensional array.
length(dim)
Returns the length of the given array dimension. The individual dimensions are numbered starting from 1, where 1 represents the first, or highest, dimension possible in the array. Thus, in an array with three dimensions, 1 indicates the number of tables in the box, 2 is the number of rows, and 3 is the number of columns.
clear(value)
Clears the array by setting each element to the given value .
getitem(index1, index2, ..., indexN)
Returns the value stored in the array at the element position indicated by the n-tuple (index1, index2, ... indexN ). All of the specified indices must be
given and they must be within the valid range of the corresponding array dimensions. Accessed using the element operator:
y = x[1, 2, 8] .
setitem(index1, index2, ..., indexN, value)
Modifies the contents of the specified array element to contain the given value . The element is specified by the n-tuple (index1, index2, ... indexN ). All of the subscript components must be given and they must be within the valid range of the corresponding array dimensions. Accessed using the element operator: x[1, 2, 8] = y .
|