page176.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 54 行

HTML
54
字号
<HTML><HEAD><TITLE>Finding the Position of an Item and Accessing by Position</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="tex2html3235" HREF="page177.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3233" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3227" HREF="page175.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3237" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007116000000000000000">Finding the Position of an Item and Accessing by Position</A></H3><P>Program&nbsp;<A HREF="page176.html#progorderedListAsArraye"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines two more operationsof the <tt>OrderedListAsArray</tt> class,<tt>findPosition</tt> and <tt>__getitem__</tt>.In addition to <tt>self</tt>,the <tt>findPosition</tt> method takes as its argument an object, <tt>obj</tt>.The purpose of this method is to search the ordered listfor an item which matches the object,and to return the position in the form of a <tt>Cursor</tt>.In this case,the result is an instance of the <tt>OrderedListAsArray.Cursor</tt> class.<P><P><A NAME="8865">&#160;</A><A NAME="progorderedListAsArraye">&#160;</A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program8806" SRC="img745.gif"  ><BR><STRONG>Program:</STRONG> <tt>OrderedListAsArray</tt> class <tt>findPosition</tt> and 	and <tt>__getitem__</tt> methods.<BR><P><P>The search algorithm used in <tt>findPosition</tt> is identical to thatused in the <tt>find</tt> method (Program&nbsp;<A HREF="page173.html#progorderedListAsArrayc"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).The <tt>findPosition</tt> uses the <tt>==</tt> operator to locatea contained object which is equal to the search target.Note that if no match is found,the <tt>_offset</tt> is set to the value <tt>_count</tt>,which is one position to the right of the last item in the ordered list.The running time of <tt>findPosition</tt> is identical to that of<tt>find</tt>:  <IMG WIDTH=136 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline61009" SRC="img738.gif"  >, where  <IMG WIDTH=72 HEIGHT=9 ALIGN=BOTTOM ALT="tex2html_wrap_inline60691" SRC="img663.gif"  >.<P>In addition to <tt>self</tt>,the <tt>__getitem__</tt> method takes an <tt>int</tt> argument and returns the objectin the ordered list at the specified position.In this case, the position is specified usingan integer-valued subscript expression.The implementation of this method is trivial--it simply indexes into the array.Assuming the specified offset is valid,the running time of this method is <I>O</I>(1).<P><HR><A NAME="tex2html3235" HREF="page177.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3233" HREF="page170.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3227" HREF="page175.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html3237" 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 &#169; 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 + -
显示快捷键?