目录
1.初始化数组
2.顺序表的打印
3.获取顺序表的长度
4.默认在数组最后位置新增的add方法
5.在指定位置新增的add方法
6.判断是否包含某个元素
7.查找某个元素对应的位置
8.获取pos位置的元素
9.将pos位置的元素更新成value
10.删除第一次出现的关键字key
11.清空顺序表
顺序表本质上就是一个数组,而顺序表就是实现对这个数组进行增删查改等操作方法的一个类,而Java中也有类似与顺序表的集合类:ArrayList<E> 下面我会对这个类里面比较重要且常用的方法进行实现。
1.初始化数组
我们首先创建一个我们自己的类MyArrayList,在里面定义一个int类型的数组,usedSize记录数组里面的元素个数,DEFAULT_SIZE 是建立数组的默认容量,通过构造方法对数组进行初始化容量
public class MyArraylist {
public int[] elem;
public int usedSize;
private static final int DEFAULT_SIZE = 10;//默认容量
public MyArraylist() {
this.elem = new int[DEFAULT_SIZE];
}
}
2.顺序表的打印
遍历整个顺序表,用前面定义的 usedSize 判断数组的长度(elem.length也可以),对里面的元素进行打印,最后换行
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
3.获取顺序表的长度
直接return usedSize即可。
// 获取顺序表长度
public int size() {
return this.usedSize;
}
4.默认在数组最后位置新增的add方法
只要是增加元素,就要考虑数组的容量是否充足,所以我将判断 数组是否当前空间是否满了 的判断另写了一个方法 isFull() 如果数组满了就进行2倍扩容(当前数组的长度*2),然后再数组的尾部新增元素,usedSize++。
public void add(int data) {
if (this.isFull()) {
this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
}
this.elem[this.usedSize] = data;
this.usedSize++;
}
public boolean isFull() {
if (this.elem.length >= this.usedSize) {
return true;
}
return false;
}
5.在指定位置新增的add方法
仍然是先要判断数组的容量,进行扩容操作,在之后就要对传入的pos位置进行判断,pos的位置一定是不能为负,也不能大于当前数组的元素个数的,如果pos不符合这两个条件,那么我们就要抛出一个异常告诉使用者。
再之后就要进行增添元素,将从pos位置一直到数组最后一个元素向后移动一个位置,然后将新元素放到pos位置,再进行usedSize++,就可以了。
public void add(int pos, int data) {
if (this.isFull()) {
this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
}
if (pos < 0 || pos > this.usedSize) {
throw new PosWrongfulException("pos位置不合法");
}
for (int i = this.usedSize - 1; i >= pos; i--) {
this.elem[i + 1] = this.elem[i];
}
this.elem[pos] = data;
this.usedSize++;
}
public boolean isFull() {
if (this.elem.length >= this.usedSize) {
return true;
}
return false;
}
//这个异常在另一个文件中,为了方便就放一起了
public class PosWrongfulException extends RuntimeException{
public PosWrongfulException() {
}
public PosWrongfulException(String message) {
super(message);
}
}
6.判断是否包含某个元素
返回boolean类型,遍历整个顺序表即可。
// 判定是否包含某个元素
public boolean contains(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (this.elem[i] == toFind) {
return true;
}
}
return false;
}
7.查找某个元素对应的位置
遍历整个顺序表,找到返回下标,没找到返回-1。
// 查找某个元素对应的位置
public int indexOf(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if (this.elem[i] == toFind) {
return i;
}
}
return -1;
}
8.获取pos位置的元素
首先判断顺序表是否为空,报异常,然后判断pos位置是否合法,当pos<0 || pos>=usedSize的时候是不合法的,报异常,经过上面的筛查,顺序表中的pos位置一定是有元素的,那么直接返回这个元素即可。
// 获取 pos 位置的元素
public int get(int pos) {
if (this.isEmpty()) {
throw new PosWrongfulException("顺序表里没有元素");
}
if (pos < 0 || pos >= this.usedSize) {
throw new PosWrongfulException("pos位置不合法");
}
return this.elem[this.indexOf(pos)];
}
private boolean isEmpty() {
if (this.usedSize == 0) {
return true;
}
return false;
}
9.将pos位置的元素更新成value
和获取元素大致相同,两道筛选,判断顺序表是否为空,判断pos位置是否合法,然后修改pos位置的数值。
// 给 pos 位置的元素设为【更新为】 value
public void set(int pos, int value) {
if (this.isEmpty()) {
throw new PosWrongfulException("顺序表里没有元素");
}
if (pos < 0 || pos >= this.usedSize) {
throw new PosWrongfulException("pos位置不合法");
}
this.elem[pos] = value;
}
private boolean isEmpty() {
if (this.usedSize == 0) {
return true;
}
return false;
}
10.删除第一次出现的关键字key
首先判断顺序表是否为空,然后运用前面写过的方法indexOf,将key传进去,得到它的下标,如果返回不是-1,那么进行删除操作:将index + 1位置到尾部的所有元素前向平移一个位置,再将usedSize--。
public void remove(int key) {
if (this.isEmpty()) {
throw new PosWrongfulException("顺序表里没有元素");
}
int index = this.indexOf(key);
if(index != -1) {
for (int i = index + 1; i < this.usedSize; i++) {
this.elem[i - 1] = this.elem[i];
}
this.usedSize--;
}
}
11.清空顺序表
两种方法:第一种比较简单粗暴,因为整个顺序表都是根据usedSize的大小进行操作的,所以第一种方法就是将usedSize = 0(如果数组中传的是引用需要全部置空)
// 清空顺序表
public void clear() {
/*for (int i = 0; i < this.usedSize; i++) {
this.elem[i] = null;
}*/
this.usedSize = 0;
}
第二种方法是重新new一个数组,大小还是取最开始设置的默认值,然后再将usedSize = 0
// 清空顺序表
public void clear() {
this.elem = new int[DEFAULT_SIZE];
this.usedSize = 0;
}