我是靠谱客的博主 悦耳心情,这篇文章主要介绍实现一个简单的顺序表(Java代码),现在分享给大家,希望可以做个参考。

在本篇文章,主要写一些关于顺序表的简单操作。在介绍顺序表的简单操作前,先来了解什么是顺序表?顺序表的简单操作又有那些呢?增删改除算不算呢?下面让我们一起来了解一下吧!

 将表中元素一个接一个的存入一组连续的存储单元中,这种存储结构是顺序结构。采用顺序存储结构的线性表简称为“ 顺序表”。

现在来定义一个表。当elem没有初始化的时候,默认为null;而usedSize没初始化时被默认为0。

复制代码
1
2
3
4
5
6
7
8
public class MyArrayList { //实现顺序表代码 public int[] elem; public int usedSize; public MyArrayList() { this.elem = new int[5]; //数组里有五个元素 } }

接下来让我们看看第一步:打印顺序表

复制代码
1
2
3
4
5
public void toString() { for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } }

第二步:增加新元素,新增元素的时候,默认在数组的最后面新增。但是增加元素的时候,需要判断数组容量是否是满的,如果数组所开辟的空间已经放满了元素,则需要扩容。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void add(int data) { //1.判断是不是满的 if (isFull()) { //扩容 this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length); } this.elem[this.usedSize] = data; this.usedSize++; } private boolean isFull() { if (this.usedSize == this.elem.length) { return true; //满的 } return false; }

第三步:在pos位置新增元素。当然,在指定位置新增元素,也需要对这个指定位置进行判断,看它合法吗,不能在开辟的空间外新增元素。同样,也需要对所开辟空间进行判断,如果该空间满了,放不下新元素了,就需要扩容了。在指定位置增加元素后、这个位置本来的元素将会后移一位。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//在pos位置新增元素 public void add(int pos, int data) { //0、对pos位置进行合法判断 if (pos < 0 || pos > this.usedSize) { System.out.println("pos位置不合法!"); return; } //不能是满的 if (isFull()) { //扩容 this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length); } //2、正常插入 for (int i = this.usedSize; i > pos; i--) { this.elem[i] = this.elem[i-1]; } this.elem[pos] = data; this.usedSize++; }

第四步:判断顺序表是否包含某个元素。这个操作需要注意的问题就很少了,只用关注查找范围就好。

复制代码
1
2
3
4
5
6
7
8
public boolean contains(int toFind) { for (int i = 0; i < this.usedSize; i++) { if (this.elem[i] == toFind) { return true; } } return false; }

第五步:查找某个元素的对应位置

复制代码
1
2
3
4
5
6
7
8
public int indexOf(int toFind) { for (int i = 0; i < this.usedSize; i++) { if (this.elem[i] == toFind) { return i; } } return -1; //数组没有-1下标 }

第六步:获取pos位置的元素。这个指定位置需要判断它是否合法,不能去获取一个不存在位置的数据、元素。

复制代码
1
2
3
4
5
6
7
public int get(int pos) { if (pos < 0 || pos >= this.usedSize) { System.out.println("pos位置不合法!"); throw new RuntimeException("pos位置不合法!"); } return this.elem[pos]; }

第七步:给pos位置的元素设为value。将指定位置的元素设置为value,可以理解为用value覆盖住原来的值。

复制代码
1
2
3
4
5
6
7
public void set(int pos, int value) { if (pos < 0 || pos >= this.usedSize) { System.out.println("pos位置不合法!"); throw new RuntimeException("pos位置不合法!"); } this.elem[pos] = value; //覆盖原来的值 }

第八步:删除第一次出现的关键字key。这次,终于不用判断位置合法不合法了,出现了一个新的需要判断条件了——数组是否为空?当然了,如果数组为空,那么删除什么呢?,所以咱们为了实现删除,前提是数组不为空。

复制代码
1
2
3
4
5
6
7
8
9
10
11
public void remove(int key) { if (isEmpty()) { System.out.println("空的!不能删除!!"); return; } int index = indexOf(key); for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i + 1]; } this.usedSize--; }

第九步:获取顺序表的长度。

复制代码
1
2
3
public int size() { return this.usedSize; }

第十步:清空顺序表。

复制代码
1
2
3
public void clear() { this.usedSize = 0; }

以上,都是顺序表的基本操作。

下面附上完整代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.Arrays; public class MyArrayList { //实现顺序表代码 public int[] elem; //null public int usedSize; //0 public MyArrayList() { this.elem = new int[10]; } //打印顺序表 public void myToString() { for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); } //新增元素,默认在数组最后新增 public void add(int data) { //1.判断是不是满的 if (isFull()) { //扩容 this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length); } this.elem[this.usedSize] = data; this.usedSize++; } private boolean isFull() { if (this.usedSize == this.elem.length) { return true; //满的 } return false; } //在pos位置新增元素 public void add(int pos, int data) { //0、对pos位置进行合法判断 if (pos < 0 || pos > this.usedSize) { System.out.println("pos位置不合法!"); return; } //不能是满的 if (isFull()) { //扩容 this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length); } //2、正常插入 for (int i = this.usedSize; i > pos; i--) { this.elem[i] = this.elem[i-1]; } this.elem[pos] = data; this.usedSize++; } //判断是否包含某个元素 public boolean contains(int toFind) { for (int i = 0; i < this.usedSize; i++) { if (this.elem[i] == toFind) { return true; } } return false; } //查找某个元素对应的位置 public int indexOf(int toFind) { for (int i = 0; i < this.usedSize; i++) { if (this.elem[i] == toFind) { return i; } } return -1; //数组没有-1下标 } //获取pos位置的元素 public int get(int pos) { if (pos < 0 || pos >= this.usedSize) { System.out.println("pos位置不合法!"); throw new RuntimeException("pos位置不合法!"); } return this.elem[pos]; } //给pos位置元素设为value public void set(int pos, int value) { if (pos < 0 || pos >= this.usedSize) { System.out.println("pos位置不合法!"); throw new RuntimeException("pos位置不合法!"); } this.elem[pos] = value; //覆盖原来的值 } //删除第一次出现的关键字key public void remove(int key) { if (isEmpty()) { System.out.println("空的!不能删除!!"); return; } int index = indexOf(key); for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i + 1]; } this.usedSize--; } private boolean isEmpty() { if (this.usedSize == 0) { return true; } return false; } //获取顺序表长度 public int size() { return this.usedSize; } //清空顺序表 public void clear() { this.usedSize = 0; } }

调用、测试操作代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class TsetDemo { public static void main(String[] args) { MyArrayList myArrayList=new MyArrayList(); myArrayList.add(7); myArrayList.add(9); myArrayList.add(5); myArrayList.add(3); myArrayList.add(6); //实现新增元素 myArrayList.myToString(); //打印数组 myArrayList.add(4,8); //实现在pos位置新增元素 myArrayList.myToString(); //打印数组 System.out.println(myArrayList.contains(9)); System.out.println(myArrayList.contains(1)); //判断是否包含某个元素 System.out.println(myArrayList.indexOf(9)); //查找某个元素对应的位置 System.out.println(myArrayList.get(5)); //获取pos位置的元素 myArrayList.set(4,12); //给pos位置元素设为value myArrayList.myToString(); //打印数组 myArrayList.remove(3); //删除第一次出现的关键字key myArrayList.myToString(); //打印数组 System.out.println(myArrayList.size()); //获取顺序表长度 myArrayList.clear(); //清空顺序表 } }

最后

以上就是悦耳心情最近收集整理的关于实现一个简单的顺序表(Java代码)的全部内容,更多相关实现一个简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部