目录
写在前面
线性表
在谈正式内容之前,我们先来看看什么是线性表
顺序表
顺序表是用一段**物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。它的本质就是一个数组.我们会疑惑我们既然有数组,为何还会出现顺序表呢?实际上在C99之前,我们的编译器不支持变常数组,在对数据的存储方面有一定的不好.这就出现了顺序表
今天顺序表的博客是很简单的,就是关于数组元素的增删查改.没有很么可以值德思考的地方,细心一点就可以了
顺序表又分为:
- 静态顺序表:使用定长数组存储
- 动态顺序表:使用动态开辟的数组存储。
顺序表的创建
我们先定义两个类(存放在两个文件中)
public class MyArraylist {
public int[] elem; //定义一个数组
public int usedSize; //数组使用的长度
public static final int intCapacity = 10; //初始容量
//构造方法
public MyArraylist() {
this.elem = new int[intCapacity];
this.usedSize = 0;
}
// 打印顺序表
public void display() { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int search(int toFind) { return -1; }
// 获取 pos 位置的元素
public int getPos(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }
}
public class TestDemo {
public static void main(String[] args) {
}
}
打印顺序表
public void display() {
for (int i = 0; i < this.usedSize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
在 pos 位置新增元素
-
判断pos的值是否合法
pos合法性的判断主要有两点
- 不能小于 0
- 不能和数组最后一个元素产生间隔
if (pos < 0 || pos > this.usedSize) {
System.out.println("不合法");
return;
}
- 原本的数组满了,增容后添加
if(ifFull()) {
//二倍扩容
this.elem = Arrays.copyOf(this.elem,2 * this.elem.length);
}
这里ifFull方法我们单独写出来
private boolean ifFull() {
if(this.usedSize == this.elem.length) {
//满了
return true;
}
return false;
}
int i = this.usedSize - 1;
while(i >= pos) {
this.elem[i + 1] = this.elem[i];
i--;
}
this.elem[pos] = data;
this.usedSize++;
public void add(int pos, int data) {
if(ifFull()) {
//二倍扩容
this.elem = Arrays.copyOf(this.elem,2 * this.elem.length);
}
if (pos < 0 || pos > this.usedSize) {
System.out.println("不合法");
return;
}
int i = this.usedSize - 1;
while(i >= pos) {
this.elem[i + 1] = this.elem[i];
i--;
}
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 search(int toFind) {
for (int i = 0; i < this.usedSize; i++) {
if(this.elem[i] == toFind)
return i;
}
return -1; //找不到该元素
}
获取 pos 位置的元素
- 空顺序表
private boolean ifEmpty() {
return this.usedSize==0;
}
- pos的值不合法
private boolean ifLeg(int pos) {
if(pos<0||pos>this.usedSize) {
//
return true;
}
return false;
}
public int getPos(int pos) {
if(ifEmpty()) {
throw new RuntimeException("顺序表为空"); //抛出一个异常
// return -1; //如果那个元素就是 -1
}
if(ifLeg(pos)) {
throw new RuntimeException("不合法");
}
return this.elem[pos];
}
给 pos 位置的元素设为 value
public void setPos(int pos, int value) {
if(ifEmpty()) {
throw new RuntimeException("顺序表为空");
}
if(ifLeg(pos)) {
throw new RuntimeException("不合法");
}
this.elem[pos] = value;
}
删除第一次出现的关键字key
public void remove(int toRemove) {
int i = search(toRemove); //查一查key对应的位置
if(-1==i) {
System.out.println("不存在这个数");
} else {
for(;i < this.usedSize-1;i++) {
this.elem[i] = this.elem[i + 1];
}
this.usedSize--; //一定要写
}
}
获取顺序表长度
public int size() {
return this.usedSize;
//return 0;
}
清空顺序表
public void clear() {
//如何清空
this.usedSize = 0;
System.out.println("clear");
}