scores-insert.html

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

HTML
29
字号
<html><head><title>Code Fragment</title></head><body text=#000000><center></center><br><br><dl><dd><pre>  <font color = #ff0080>/** Attempt to add a new score to the collection (if it is high enough) */</font>  <font color=#8000a0><font color=#8000a0>public</font> </font><font color=#8000a0>void</font> <font color=#0000ff>add</font>(GameEntry e) {    <font color=#8000a0><font color=#8000a0>int</font> </font>newScore = e.<font color=#0000ff>getScore</font>();    <font color=#ff0080>// is the new entry e really a high score?</font>    <font color=#ff8000>if</font><font color=#0000ff> </font>(numEntries == maxEntries) { <font color=#ff0080>// the array is full</font>      <font color=#ff8000>if</font><font color=#0000ff> </font>(newScore &lt;= entries[numEntries-1].<font color=#0000ff>getScore</font>())	<font color=#ff8000>return</font>;  <font color=#ff0080>// the new entry, e, is not a high score in this case</font>    }    <font color=#ff8000>else</font> <font color=#ff0080>// the array is not full</font>      numEntries++;    <font color=#ff0080>// Locate the place that the new (high score) entry e belongs</font>    <font color=#8000a0><font color=#8000a0>int</font> </font>i = numEntries-1;     <font color=#ff8000>for</font><font color=#0000ff> </font>( ;<font color=#0000ff> </font>(i &gt;= 1) &&<font color=#0000ff> </font>(newScore &gt; entries[i-1].<font color=#0000ff>getScore</font>()); i--)      entries[i] = entries[i - 1]; 	  <font color=#ff0080>// move entry i one to the right</font>    entries[i] = e; 			  <font color=#ff0080>// add the new score to entries</font>  }</dl></body></html>

⌨️ 快捷键说明

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