顺序表 - Java

目录

一、IList

二、MyArrayList

三、PoslslegalException

四、Test


一、IList

java 复制代码
package myArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-05
 * Time: 8:45
 */
public interface IList<T> {
    // 判断顺序表是否已满
    boolean isFull();

    // 新增元素,默认在数组最后新增
    void add(T data);
    // 在pos位置新增元素
    void add(int pos, T data) throws PosIslegalException;

    // 判定是否包含某个元素
    boolean contains(T toFind);

    // 查找某个元素对应的位置
    int indexOf(T toFind);
    // 获取 pos 位置的元素
    T get(int pos) throws PosIslegalException;

    // 给 pos 位置的元素设为 value
    void set(int pos, T value) throws PosIslegalException;

    //删除第一次出现的关键字key
    void remove(T toRemove);

    void removeTow(T toRemove);

    // 获取顺序表长度
    int size();

    // 清空顺序表
    void clear();

    // 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
    void display();
}

二、MyArrayList

java 复制代码
package myArrayList;

import java.awt.image.AreaAveragingScaleFilter;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-05
 * Time: 8:45
 */

//如果不把MyArrayList定义成泛型的,就传不了泛型标记了🙀

public class MyArrayList<T> implements IList<T> {
    private Object[] elem;
    private int usedSize;//顺序表中有效数据个数

    public MyArrayList() {
        this.elem = new Object[6];
    }

    @Override
    public boolean isFull() {//判断顺序表是否已满
        if (this.elem.length == this.usedSize) {
            return true;
        } else {
            return false;
        }
    }

    //扩容,没必要把返回值定义成T[],我们使用泛型限定了放入数组中的元素类型,并不是限定数组类型,Object[]也不能强转为其他类型的数组
    private Object[] newCapacity() {
        Object[] cur = Arrays.copyOf(this.elem, this.elem.length * 2);
        return cur;
    }

    private boolean isLegal(int pos) {//add时是否合法
        if (pos >= 0 && pos <= this.elem.length) {
            return true;
        } else {
            return false;
        }
    }

    private boolean isLegalTow(int pos) {//重置时是否合法
        if (pos >= 0 && pos < this.elem.length) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void add(T data) {//新增元素,默认在数组最后新增
        if (this.isFull()) {
            this.elem = this.newCapacity();
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, T data) throws PosIslegalException {//在pos位置新增元素
        if (this.isFull()) {
            this.elem = this.newCapacity();
        }

        if (!this.isLegal(pos)) {
            throw new PosIslegalException("pos位置不合法!!!");
        }

        for (int i = this.usedSize; i > pos ; i--) {
            this.elem[i] = this.elem[i - 1];
        }

        this.elem[pos] = data;
        this.usedSize++;
    }

    @Override
    public boolean contains(T toFind) { //判定是否包含某个元素,contains是包含的意思
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i].equals(toFind)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(T toFind) {//查找某个元素对应的位置
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i].equals(toFind)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public T get(int pos) throws PosIslegalException {//获取 pos 位置的元素
        if (!this.isLegal(pos)) {
            throw new PosIslegalException("pos位置不合法!!!");
        }

        return (T)this.elem[pos];
    }

    @Override
    public void set(int pos, T value) throws PosIslegalException {//给pos位置的元素设为 value
        if (!this.isLegalTow(pos)) {
            throw new PosIslegalException("pos位置不合法!!!");
        }

        this.elem[pos] = value;
    }

    @Override
    public void remove(T toRemove) {//删除第一次出现的关键字key
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i].equals(toRemove)) {
                int j = i;
                for (; j < this.usedSize - 1; j++) {
                    this.elem[j] = this.elem[j + 1];
                }
                this.elem[j] = null;
                this.usedSize--;
                break;
            }
        }
    }

    @Override
    public void removeTow(T toRemove) {//删除顺序表中所有的key,等待优化🧐!
        while (true) {
            int flag = 1;
            for (int i = 0; i < this.usedSize; i++) {
                if (this.elem[i].equals(toRemove)) {
                    flag = 0;
                    int j = i;
                    for (; j < this.usedSize - 1; j++) {
                        this.elem[j] = this.elem[j + 1];
                    }
                    this.elem[j] = null;
                    this.usedSize--;
                    break;
                }
            }
            if (flag == 1) {
                break;
            }
        }
    }

    @Override
    public int size() {//获取顺序表长度
        return this.usedSize;
    }

    @Override
    public void clear() {//清空顺序表
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = null;
        }
        this.usedSize = 0;//别忘了🙀
        this.elem = null;
    }

    // 打印顺序表
    // 注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
    @Override
    public void display() {
        System.out.println(Arrays.toString(this.elem));
    }
}

三、PoslslegalException

java 复制代码
package myArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-05
 * Time: 9:31
 */
public class PosIslegalException extends Exception {
    public PosIslegalException() {
        super();
    }

    public PosIslegalException(String str) {
        super(str);
    }
}

四、Test

java 复制代码
package myArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-05
 * Time: 8:45
 */
public class Test {
    public static void main(String[] args) throws PosIslegalException {
        IList<String> iList = new MyArrayList<>();//这种写法父类只能访问父类存在的方法
        //MyArrayList<String> myArrayList = new MyArrayList<>();

        iList.add("abc");
        iList.add("def");
        iList.add("def");
        iList.add("ghi");
        iList.add("def");
        iList.add("ghi");

//        iList.add("ghi");
//        iList.add("ghi");
//        iList.add("ghi");
//        iList.add("ghi");
//        iList.display();

//        iList.add(0,"kkk");
//        iList.add(2,"eee");
//        iList.add(10,"eee");
//        iList.display();

//        System.out.println(iList.contains("kkk"));
//        System.out.println(iList.contains("def"));

//        System.out.println(iList.indexOf("kkk"));
//        System.out.println(iList.indexOf("def"));//1

//        System.out.println(iList.get(1));
//        System.out.println(iList.get(100));

//        iList.set(1,"hhhh");
//        iList.set(100,"hhhh");
//        iList.display();

//        iList.display();
//        iList.remove("def");
//        iList.display();\

//        iList.display();
//        iList.removeTow("def");
//        iList.display();

//        System.out.println(iList.size());

        iList.clear();
    }
}
相关推荐
阿华的代码王国几秒前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
黑蛋同志1 分钟前
array和linked list的区别
java
繁依Fanyi7 分钟前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
andrew_12197 分钟前
腾讯 IEG 游戏前沿技术 一面复盘
java·redis·sql·面试
寻求出路的程序媛15 分钟前
JVM —— 类加载器的分类,双亲委派机制
java·jvm·面试
这孩子叫逆17 分钟前
35. MyBatis中的缓存失效机制是如何工作的?
java·spring·mybatis
骆晨学长17 分钟前
基于SpringBoot的校园失物招领系统
java·spring boot
汇匠源17 分钟前
零工市场小程序:保障灵活就业
java·小程序·零工市场
计算机编程-吉哥20 分钟前
计算机毕业设计 二手图书交易系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
java·spring boot·毕业设计·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·二手图书交易系统
qq_353233538921 分钟前
【原创】java+springboot+mysql高校社团网系统设计与实现
java·spring boot·mysql