顺序表
<code class="hljs cs has-numbering">class SeqList{ <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> maxSize; <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> size; <span class="hljs-keyword">private</span> Object[] listArray; <span class="hljs-comment">//构造函数</span> <span class="hljs-keyword">public</span> <span class="hljs-title">SeqList</span>(<span class="hljs-keyword">int</span> size){ maxSize = size; listArray = <span class="hljs-keyword">new</span> Object[size]; size = <span class="hljs-number">0</span>; } <span class="hljs-comment">//插入</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">insert</span>(<span class="hljs-keyword">int</span> index,Object obj) throws Exception{ <span class="hljs-keyword">if</span>(size == maxSize){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"顺序表已满无法插入!"</span>); } <span class="hljs-keyword">if</span>(index<<span class="hljs-number">0</span> || index>size){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"参数错误!"</span>); } <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j=size;j>index;j--){ listArray[j] = listArray[j-<span class="hljs-number">1</span>]; } listArray[index] = obj; size++; } <span class="hljs-comment">//删除</span> <span class="hljs-keyword">public</span> Object <span class="hljs-title">delete</span>(<span class="hljs-keyword">int</span> index) throws Exception { <span class="hljs-keyword">if</span>(size == <span class="hljs-number">0</span>){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"链表为空无法删除!"</span>); } <span class="hljs-keyword">if</span>(index<<span class="hljs-number">0</span> || index>size-<span class="hljs-number">1</span>){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"参数有误!"</span>); } Object it = listArray[index]; <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> j=index;j<size-<span class="hljs-number">1</span>;j++){ listArray[j] = listArray[j+<span class="hljs-number">1</span>]; } size--; <span class="hljs-keyword">return</span> it; } <span class="hljs-comment">//获取元素</span> <span class="hljs-keyword">public</span> Object <span class="hljs-title">getData</span>(<span class="hljs-keyword">int</span> index) throws Exception { <span class="hljs-keyword">if</span>(index<<span class="hljs-number">0</span> || index>size-<span class="hljs-number">1</span>){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Exception(<span class="hljs-string">"参数有误!"</span>); } <span class="hljs-keyword">return</span> listArray[index]; } <span class="hljs-comment">//获取元素个数</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">size</span>() { <span class="hljs-keyword">return</span> size; } <span class="hljs-comment">//是否为空</span> <span class="hljs-keyword">public</span> boolean <span class="hljs-title">isEmpty</span>() { <span class="hljs-keyword">return</span> size == <span class="hljs-number">0</span>; } } <span class="hljs-comment">//测试类</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Demo{ <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) { <span class="hljs-keyword">try</span> { SeqList seqList = <span class="hljs-keyword">new</span> SeqList(<span class="hljs-number">3</span>); seqList.insert(<span class="hljs-number">0</span>, <span class="hljs-keyword">new</span> Integer(<span class="hljs-number">1</span>)); seqList.insert(<span class="hljs-number">1</span>, <span class="hljs-keyword">new</span> Integer(<span class="hljs-number">2</span>)); seqList.insert(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>); System.<span class="hljs-keyword">out</span>.println(seqList.isEmpty()); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"listArray[1] = "</span>+seqList.getData(<span class="hljs-number">1</span>)); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"listArray[2] = "</span>+seqList.getData(<span class="hljs-number">2</span>)); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"size = "</span>+seqList.size()); System.<span class="hljs-keyword">out</span>.println(<span class="hljs-string">"delete listArray[0] = "</span>+seqList.delete(<span class="hljs-number">0</span>)); } <span class="hljs-keyword">catch</span> (Exception e) { <span class="hljs-comment">// TODO 自动生成的 catch 块</span> e.printStackTrace(); } } } </code><ul style="FILTER: ; ZOOM: 1" class="pre-numbering"><li>1</li></ul>
运行结果:
false
listArray[1] = 2
listArray[2] = 3
size = 3
delete listArray[0] = 1