Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
Program gives the code for three DynamicArray class constructors. The main constructor (lines 6-10) takes two arguments, n and m, which represent the desired array length and the lower bound for array indices, respectively. This constructor allocates an array of objects of length n and sets the baseIndex field to m. The remaining two constructors (lines 12-16) simply call the main constructor by invoking the this initializer. These constructors simply provide default values for m and n.
Program: DynamicArray constructors.
In C#, when an array is allocated, two things happen. First, memory is allocated for the array object and its elements. Second, each element of the array is initialized with the appropriate default value (in this case null).
For now, we shall assume that the first step takes a constant amount of time. Since there are n elements to be initialized, the second step takes O(n) time. Therefore, the running time of the main constructor is O(n).