顺序表是什么

简介: 顺序表是什么

一、顺序表是什么?
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

数组不就是一个现场的顺序表吗?但是数组并没有直接向我们提供增删查改的工具,所以我们必须重新实现一下顺序表。

二、自定义异常
空引用异常
如果我们的顺序表为空时,手动抛出空引用异常

public class NullException extends RuntimeException{

public NullException(String message) {
    super(message);
}

}
1
2
3
4
5
下标越界异常
当我们进行增删查改时,下标越界时,我们手动抛出一个下标越界异常

public class IndexException extends RuntimeException{

public IndexException(String message) {
    super(message);
}

}
1
2
3
4
5
三、顺序表的方法
顺序表的实现
这里我们定义一个顺序表,默认容量为DEFAULTSIZE,实际大小为usedsize.

public class ArrList {

public int[] arr;
public int usedSize;
public static final int DEFAULTSIZE = 10;

public ArrList() {
    this.arr = new int[DEFAULTSIZE];
}

}
1
2
3
4
5
6
7
8
9
获取顺序表长度
usedSize存储的就是当前顺序表的长度,直接返回即可。

public int size() {

    return this.usedSize;
}

1
2
3
顺序表是否为空
此方法我们只想在顺序表内部使用,所以我们定义为private.

private boolean isEmpty() {

    return this.arr == null;
}

1
2
3
顺序表是否为满
此方法我们只想在顺序表内部使用,所以我们定义为private.

private boolean isFull() {

    //如果数组所放元素大于等于数组长度,那么数组满了
    return this.size() >= this.arr.length;
}

1
2
3
4
打印顺序表
public void display() {

    for (int i = 0; i < this.usedSize; i++) {
        System.out.print(arr[i]+" ");
    }
    System.out.println();
}

1
2
3
4
5
6
末尾新增元素
public void add(int data) throws NullException{

    //1.数组为空,报空异常
    if(isEmpty()) {
        throw new NullException("数组为空");
    }
    //2.数组满了,先增容
    if(isFull()) {
        this.arr = new int[2 * this.arr.length];
    }
    //3.进行新增
    this.arr[this.usedSize] = data;
    //4.元素+1
    this.usedSize++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
指定位置新增元素
public void add(int pos, int data) throws RuntimeException,IndexException{

    //1.判断数组是否为空
    if(isEmpty()) {
        throw new NullException("数组为空");
    }
    //2.判断新增位置是否合法,抛数组越界异常
    if(pos < 0 || pos > this.arr.length) {
        throw new IndexException("数组越界");
    }
    //3.判断数组是否已满,进行扩容
    if(isFull()) {
        this.arr = new int[2 * this.arr.length];
    }
    //4.进行新增
    for (int i = this.usedSize - 1; i >= pos; i--) {
        this.arr[i+1] = this.arr[i];
    }
    this.arr[pos] = data;
    this.usedSize++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
判断是否包含某元素
public boolean contains(int toFind) {

    for (int i = 0; i < this.usedSize; i++) {
        if(toFind == this.arr[i]) {
            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(toFind == this.arr[i]) {
            return i;
        }
    }
    return -1; 
}

1
2
3
4
5
6
7
8
获取 pos 位置的元素
public int get(int pos) throws IndexException{

    //判断pos位置是否合法
    if(pos < 0 || pos >= this.usedSize) {
        throw new IndexException("输入pos位置数组越界");
    }else {
        return this.arr[pos];
    }
}

1
2
3
4
5
6
7
8
给 pos 位置的元素赋值
public void set(int pos, int value) throws NullException,IndexException{

    if(isEmpty()) {
        throw new NullException("数组为空");
    }
    //2.判断新增位置是否合法,抛数组越界异常
    if(pos < 0 || pos >= this.arr.length) {
        throw new IndexException("数组越界");
    }
    this.arr[pos] = value;
}

1
2
3
4
5
6
7
8
9
10
删除第一次出现的关键字key
public void remove(int toRemove) throws NullException{

    if(isEmpty()) {
        throw new NullException("数组为空");
    }
    int ret = indexOf(toRemove);
    if(ret == -1) {
        System.out.println("不存在此数");
        return;
    }
    if(ret != -1) {
        for (int i = ret; i < this.usedSize - 10; i++) {
            this.arr[i] = this.arr[i+1];
        }
    }
    this.usedSize++;
}
相关文章
|
1月前
|
存储 测试技术 C语言
顺序表详解(SeqList)
顺序表详解(SeqList)
49 0
|
8月前
|
存储
【顺序表】
【顺序表】
37 0
|
8天前
|
算法
顺序表的应用
顺序表的应用
18 5
|
8天前
|
存储 算法
顺序表专题
顺序表专题
18 4
|
17天前
|
存储 索引
顺序表和链表
通过以上示例,我们可以看到顺序表和链表在实际应用中如何操作。顺序表适合于需要频繁读取数据的场景,而链表则更适用于频繁修改数据的情况。在选择使用哪种数据结构时,应考虑到实际应用的需求和上下文环境。
12 2
|
25天前
|
存储
2.顺序表_链表(附练习)
2.顺序表_链表(附练习)
|
25天前
|
存储
25.顺序表专题
25.顺序表专题
|
1月前
|
存储
顺序表讲解
顺序表讲解
27 0
|
1月前
顺序表的实现
顺序表的实现
|
7月前
|
测试技术
顺序表(2)
顺序表(2)
532 0