Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Resizing an Array-setBase and setLength

 

The setBase and setLength methods of the Array class are given in Program gif. The setBase method simply modifies the base field as required. The setLength method provides a 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.

   program2785
Program: Array class setBase and setLength methods.

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_inline59934. The Resize method first allocates and initializes a new array of size m. Next, it copies at most tex2html_wrap_inline59938 elements from the old array to the new array. Therefore, tex2html_wrap_inline59940.


next up previous contents index

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