Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Resizing an Array

 

Program gif defines the length property of the Array class. The fget accessor simply invokes the __len__ method which returns the length of the array. The fset accessor of the length property calls the setLength method which provides the means to change the size of an array at run time. This method can be used both to increase and to decrease the size of an array.

   program2833
Program: Array class length property.

The running time of this algorithm depends only on the new array length. Let n be the original size of the array and let m be the new size of the array. Consider the case where tex2html_wrap_inline60439. The method first allocates and initializes a new array of size m. Next, it copies at most tex2html_wrap_inline60443 elements from the old array to the new array. Therefore, tex2html_wrap_inline60445.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.