page88.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 44 行
HTML
44 行
<HTML><HEAD><TITLE>Resizing an Array</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2220" HREF="page89.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2218" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2214" HREF="page87.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2222" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004160000000000000000">Resizing an Array</A></H2><P><A NAME="secfdsresize"> </A><P>Program <A HREF="page88.html#progarraye"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>length</tt> property of the <tt>Array</tt> class.The <tt>fget</tt> accessor simply invokes the <tt>__len__</tt> methodwhich returns the length of the array.The <tt>fset</tt> accessor of the <tt>length</tt> propertycalls the <tt>setLength</tt> method which provides the meansto change the size of an array at run time.This method can be used both to increase and to decreasethe size of an array.<P><P><A NAME="2910"> </A><A NAME="progarraye"> </A> <IMG WIDTH=575 HEIGHT=352 ALIGN=BOTTOM ALT="program2833" SRC="img589.gif" ><BR><STRONG>Program:</STRONG> <tt>Array</tt> class <tt>length</tt> property.<BR><P><P>The running time of this algorithm depends only on the new array length.Let <I>n</I> be the original size of the arrayand let <I>m</I> be the new size of the array.Consider the case where <IMG WIDTH=44 HEIGHT=25 ALIGN=MIDDLE ALT="tex2html_wrap_inline60439" SRC="img590.gif" >.The method first allocatesand initializes a new array of size <I>m</I>.Next, it copies at most <IMG WIDTH=69 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60443" SRC="img591.gif" > elements from the old arrayto the new array.Therefore, <IMG WIDTH=287 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline60445" SRC="img592.gif" >.<P><HR><A NAME="tex2html2220" HREF="page89.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2218" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2214" HREF="page87.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2222" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?