【Java数据结构】实现顺序表

简介: 【Java数据结构】实现顺序表

 

SqList:

1. import java.util.Arrays;
2. 
3. public class SqList {
4. 
5. private static final int DEFAULTSIZE=10;//默认数组长度
6. 
7. private int[] elem=new int[DEFAULTSIZE];//数组
8. 
9. private int usedSize;
10. 
11. /**
12.      * getSize()获取有效的长度
13.      * @return
14.      */
15. public int getSize(){
16. 
17. return usedSize;
18. 
19.     }
20. 
21. /**
22.      * isFull()判断顺序表是否为满
23.      * @return
24.      */
25. public boolean isFull(){
26. 
27. return usedSize==DEFAULTSIZE;
28.     }
29. 
30. /**
31.      * isEmpty()判断顺序表是否为空
32.      * @return
33.      */
34. public boolean isEmpty(){
35. return usedSize==0;
36.     }
37. /**
38.      * disPlay()打印顺序表
39.      */
40. public void disPlay(){
41. 
42. for (int i = 0; i <getSize() ; i++) {
43. 
44.             System.out.print(this.elem[i]+" ");
45. 
46.         }
47.         System.out.println();
48.     }
49. 
50. /**
51.      * add()在顺序表末尾增加元素
52.      * @param data
53.      */
54. public void add(int data){
55. //1、判满
56. if(isFull()){
57. //2、扩容
58. this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
59.         }
60. //3、加入
61. this.elem[usedSize]=data;
62. //4、usedSize++
63.         usedSize++;
64.     }
65. 
66. /**
67.      * add()在顺序表的指定位置添加元素
68.      * @param pos  位置 如果pos位置不合法就会抛出一个PosWrongFulException
69.      * @param data 元素
70.      */
71. public void add(int pos,int data)throws PosWrongFulException{
72. //1、判满
73. if(isFull()){
74.             System.out.println("顺序表已满!");
75. this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
76.         }
77. //2、判断位置是否合理(在合理范围内且不能间隔元素)
78. if(pos<0||pos>usedSize){
79. 
80.             System.out.println("pos位置不合法");
81. throw new  PosWrongFulException("pos位置不合法!");
82. 
83.         }
84. //3、移动位置
85. for (int i =usedSize-1; i>=pos; i--) {
86. this.elem[i+1]=this.elem[i];
87.         }
88. 
89. //4、添加元素
90. this.elem[pos]=data;
91. //5、usedSize++
92.         usedSize++;
93.     }
94. 
95. /**
96.      * indexOf(获取第一次出现指定元素的下标)
97.      * @param data 指定元素
98.      * @return
99.      */
100. 
101. public int indexOf(int data)throws EmptyException{
102. //1、判空
103. if(isEmpty()){
104.             System.out.println("顺序表为空");
105. throw new  EmptyException("顺序表为空!");
106.         }
107. //2、查找
108. for (int i = 0; i < getSize(); i++) {
109. 
110. if(this.elem[i]==data){
111. return i;
112.             }
113.         }
114. return -1;
115. 
116. 
117.     }
118. 
119. /**
120.      * valueOf (int pos)获取指定下标的元素
121.      * @param pos
122.      * @return
123.      */
124. public int valueOf (int pos)throws PosWrongFulException,EmptyException{
125. //1、判空
126. if(isEmpty()){
127.             System.out.println("顺序表为空");
128. throw new  EmptyException("顺序表为空!");
129.         }
130. //2、判断位置是否合理
131. if(pos<0||pos>usedSize){
132. 
133.             System.out.println("pos位置不合法");
134. throw new  PosWrongFulException("pos位置不合法!");
135. 
136.         }
137. //3、获取该元素
138. return this.elem[pos];
139. 
140.     }
141. 
142. /**
143.      * setValue(int pos,int value)设置指定位置的元素
144.      * @param pos
145.      * @param value
146.      */
147. public void setValue(int pos,int value)throws PosWrongFulException{
148. //1、判断位置是否合法
149. if(pos<0||pos>usedSize){
150. 
151.             System.out.println("pos位置不合法");
152. throw new  PosWrongFulException("pos位置不合法!");
153. 
154.         }
155. //2、设置位置的值
156. this.elem[pos]=value;
157.     }
158. 
159. /**
160.      * deletePos(int pos)删除指定位置的元素
161.      * @param pos
162.      */
163. public void deletePos(int pos)throws EmptyException,PosWrongFulException{
164. //1、判空
165. if(isEmpty()){
166.             System.out.println("顺序表为空");
167. throw new  EmptyException("顺序表为空!");
168.         }
169. //2、判断位置是否合法
170. if(pos<0||pos>usedSize){
171. 
172.             System.out.println("pos位置不合法");
173. throw new  PosWrongFulException("pos位置不合法!");
174. 
175.         }
176. 
177. //3、移动
178. for (int i =pos; i<usedSize-1; i++) {
179. 
180. this.elem[i]=this.elem[i+1];
181. 
182.         }
183. //4、usedSize--
184. 
185.         usedSize--;
186.     }
187. 
188. 
189. /**
190.      *  delete(int value)删除第一次出现的value
191.      * @param value
192.      */
193. public void delete(int value) throws EmptyException{
194. //1、判空
195. if(isEmpty()){
196.             System.out.println("顺序表为空");
197. throw new  EmptyException("顺序表为空!");
198.         }
199. //2、找位置
200. 
201. int index=this.indexOf(value);
202. //3、移动
203. for (int i =index; i<usedSize-1; i++) {
204. 
205. this.elem[i]=this.elem[i+1];
206. 
207.         }
208. //4、usedSize--
209. 
210.         usedSize--;
211.     }
212. 
213. 
214. /**
215.      * clear()清空顺序表
216.      */
217. public void clear(){
218.         usedSize=0;
219.     }
220. 
221. 
222. 
223. }

EmptyException:

1. public class EmptyException extends RuntimeException{
2. public EmptyException() {
3.     }
4. 
5. public EmptyException(String message) {
6. super(message);
7.     }
8. }

PosWrongFulException:

1. public class PosWrongFulException extends RuntimeException{
2. public PosWrongFulException() {
3.     }
4. 
5. public PosWrongFulException(String message) {
6. super(message);
7.     }
8. }

Test

1. public class Test {
2. public static void main(String[] args) {
3. 
4.         SqList sqList=new SqList();
5.         sqList.add(1);
6.         sqList.add(2);
7.         sqList.add(3);
8.         sqList.disPlay();
9. try{
10.             sqList.add(1,10);
11.         }catch (PosWrongFulException e){
12. 
13.             e.printStackTrace();
14. 
15.         }
16.         sqList.disPlay();
17. 
18. try{
19.             System.out.println(sqList.indexOf(1));
20.         }catch (EmptyException e){
21.             e.printStackTrace();
22.         }
23. 
24. try{
25.             System.out.println(sqList.valueOf(1));
26.         }catch (PosWrongFulException e){
27.             e.printStackTrace();
28.         }
29. 
30. try {
31.             sqList.setValue(0,99);
32.         }catch (PosWrongFulException e){
33.             e.printStackTrace();
34.         }
35. 
36.         sqList.disPlay();
37. try{
38.             sqList.deletePos(3);
39.         }catch (PosWrongFulException e){
40.             e.printStackTrace();
41.         }
42. 
43.         sqList.disPlay();
44. try {
45.             sqList.delete(2);
46.         }catch (EmptyException e){
47.             e.printStackTrace();
48.         }
49.         sqList.disPlay();
50. 
51.         sqList.clear();
52. 
53.     }
54. }


相关文章
|
2月前
|
Java
【Java集合类面试二十六】、介绍一下ArrayList的数据结构?
ArrayList是基于可动态扩展的数组实现的,支持快速随机访问,但在插入和删除操作时可能需要数组复制而性能较差。
|
2月前
|
存储 设计模式 算法
JAVA中的常见数据结构
JAVA中的常见数据结构
|
3月前
|
存储 C语言
【数据结构】顺序表
数据结构中的动态顺序表
39 3
【数据结构】顺序表
|
6天前
|
Java
java数据结构,双向链表的实现
文章介绍了双向链表的实现,包括数据结构定义、插入和删除操作的代码实现,以及双向链表的其他操作方法,并提供了完整的Java代码实现。
java数据结构,双向链表的实现
|
6天前
|
存储 Java
java数据结构,线性表链式存储(单链表)的实现
文章讲解了单链表的基本概念和Java实现,包括头指针、尾节点和节点结构。提供了实现代码,包括数据结构、接口定义和具体实现类。通过测试代码演示了单链表的基本操作,如添加、删除、更新和查找元素,并总结了操作的时间复杂度。
java数据结构,线性表链式存储(单链表)的实现
|
1天前
|
存储 安全 Java
Java 数据结构类型总结
在 Java 中,常用的数据结构包括基础数据结构(如数组和字符串)、集合框架(如 Set、List 和 Map 接口的多种实现)、特殊数据结构(如栈、队列和双端队列)、链表(单链表、双链表和循环链表)以及图和树等。这些数据结构各有特点和适用场景,选择时需考虑性能、内存和操作需求。集合框架提供了丰富的接口和类,便于处理对象集合。
|
16天前
|
存储 Java 程序员
【数据结构】初识集合&深入剖析顺序表(Arraylist)
Java集合框架主要由接口、实现类及迭代器组成,包括Collection和Map两大类。Collection涵盖List(有序、可重复)、Set(无序、不可重复),Map则由键值对构成。集合通过接口定义基本操作,具体实现由各类如ArrayList、HashSet等提供。迭代器允许遍历集合而不暴露其实现细节。List系列集合元素有序且可重复,Set系列元素无序且不可重复。集合遍历可通过迭代器、增强for循环、普通for循环及Lambda表达式实现,各有适用场景。其中ArrayList实现了动态数组功能,可根据需求自动调整大小。
28 11
|
24天前
|
存储 C语言 C++
数据结构基础详解(C语言) 顺序表:顺序表静态分配和动态分配增删改查基本操作的基本介绍及c语言代码实现
本文介绍了顺序表的定义及其在C/C++中的实现方法。顺序表通过连续存储空间实现线性表,使逻辑上相邻的元素在物理位置上也相邻。文章详细描述了静态分配与动态分配两种方式下的顺序表定义、初始化、插入、删除、查找等基本操作,并提供了具体代码示例。静态分配方式下顺序表的长度固定,而动态分配则可根据需求调整大小。此外,还总结了顺序表的优点,如随机访问效率高、存储密度大,以及缺点,如扩展不便和插入删除操作成本高等特点。
|
24天前
|
存储 算法 C语言
C语言手撕数据结构代码_顺序表_静态存储_动态存储
本文介绍了基于静态和动态存储的顺序表操作实现,涵盖创建、删除、插入、合并、求交集与差集、逆置及循环移动等常见操作。通过详细的C语言代码示例,展示了如何高效地处理顺序表数据结构的各种问题。
|
6天前
|
存储 Java
java数据结构,线性表顺序存储(数组)的实现
文章介绍了Java中线性表顺序存储(数组)的实现。线性表是数据结构的一种,它使用数组来实现。文章详细描述了线性表的基本操作,如增加、查找、删除、修改元素,以及其他操作如遍历、清空、求长度等。同时,提供了完整的Java代码实现,包括MyList接口和MyLinearList实现类。通过main函数的测试代码,展示了如何使用这些方法操作线性表。
下一篇
无影云桌面