arrayindexlist-vector.html

来自「经典的数据结构源代码(java 实现)」· HTML 代码 · 共 50 行

HTML
50
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre><font color=#ff0080>/** Realization of an indexed list by means of an array, which is doubled   * when the size of the indexed list exceeds the capacity of the array.    */</font><font color=#8000a0>public</font> <font color=#8000a0><font color=#ff8000>class</font> </font>ArrayIndexList&lt;E&gt; <font color=#8000a0><font color=#ff8000>implements</font> </font>IndexList&lt;E&gt; {  <font color=#8000a0><font color=#8000a0>private</font> </font>E[] A;	      <font color=#ff0080>// array storing the elements of the indexed list</font>  <font color=#8000a0><font color=#8000a0>private</font> </font><font color=#8000a0>int</font> capacity = 16;  <font color=#ff0080>// initial length of array A</font>  <font color=#8000a0><font color=#8000a0>private</font> </font><font color=#8000a0>int</font> size = 0;	      <font color=#ff0080>// number of elements stored in the indexed list</font>  <font color = #ff0080>/** Creates the indexed list with initial capacity 16. */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#0000ff>ArrayIndexList</font>() {     A =<font color=#0000ff> </font>(E[]) <font color=#8000a0><font color=#ff8000>new</font> </font>Object[capacity]; <font color=#ff0080>// the compiler may warn, but this is ok</font>  }  <font color = #ff0080>/** Inserts an element at the given index. */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>add</font>(<font color=#8000a0>int</font> r, <font color=#8000a0>E </font>e)     <font color=#8000a0><font color=#ff8000>throws</font> </font>IndexOutOfBoundsException {    <font color=#0000ff>checkIndex</font>(r, <font color=#0000ff>size</font>() + 1);    <font color=#ff8000>if</font><font color=#0000ff> </font>(size == capacity) {		<font color=#ff0080>// an overflow</font>      capacity *= 2;      E[] B =<font color=#0000ff></font>(E[]) <font color=#8000a0><font color=#ff8000>new</font> </font>Object[capacity];      <font color=#ff8000>for</font><font color=#0000ff> </font>(<font color=#8000a0>int</font> i=0; i&lt;size; i++) 	B[i] = A[i];      A = B;    }    <font color=#ff8000>for</font><font color=#0000ff> </font>(<font color=#8000a0>int</font> i=size-1; i&gt;=r; i--)	<font color=#ff0080>// shift elements up</font>      A[i+1] = A[i];    A[r] = e;    size++;  }  <font color = #ff0080>/** Removes the element stored at the given index. */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font>E <font color=#0000ff>remove</font>(<font color=#8000a0>int</font> r)     <font color=#8000a0><font color=#ff8000>throws</font> </font>IndexOutOfBoundsException {    <font color=#0000ff>checkIndex</font>(r, <font color=#0000ff>size</font>());    <font color=#8000a0>E </font>temp = A[r];    <font color=#ff8000>for</font><font color=#0000ff> </font>(<font color=#8000a0>int</font> i=r; i&lt;size-1; i++)	<font color=#ff0080>// shift elements down</font>      A[i] = A[i+1];    size--;    <font color=#8000a0><font color=#ff8000>return</font> </font>temp;  }</dl></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?