|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Ruby |
The initialize method for the MultiDimensionalArray class
is defined in Program
.
The dimensions argument is an array
which represents the dimensions of the array.
For example,
to create a
three-dimensional array,
we create a MultiDimensionalArray like this:
a = MultiDimensionalArray.new(3, 5, 7)

Program: MultiDimensionalArray class initialize method.
The initialize method copies the dimensions of the array into the @dimensions array, and then it computes the @factors array. These operations take O(n), where n is the number of dimensions. The initialize method then allocates a one-dimensional array of length m given by
![]()
The worst-case running time of the initialize method is O(m+n).