Java顺序表的实现和常用的所有基本操作(全)

##顺序表的实现和基本操作

#顺序表的实现
public class SeqList{
 	private int length;//表长
    private int[] datas;//数据
}
#顺序表的创建和初始化
	/**
     * 创建空的顺序表
     */
    public SeqList() {
    }

    /**
     * 创建一个指定容量的顺序表
     *
     * @param capacity
     */
    public SeqList(int capacity) {
        length = 0;
        datas = new int[capacity];
    }

    /**
     * 构造指定容量和指定数组元素的顺序表
     * @param capacity
     * @param array
     */
    public SeqList(int capacity, int[] array) {
        if (capacity < array.length) {
            throw new IndexOutOfBoundsException();
        } else {
            length = 0;
            datas = new int[capacity];
            for (int i = 0; i < array.length; i++) {
                datas[i] = array[i];
                length++;
            }
        }
    }

    /**
     * 初始化表的最大容量
     *
     * @param capacity
     */
    public void create(int capacity) {
        if (datas != null) {
            throw new RuntimeException();
        } else {
            length = 0;
            datas = new int[capacity];
        }

    }
#获取指定下标的元素
   /**
     * 获取指定下标的元素
     *
     * @param index
     * @return
     */
    public int get(int index) {
        if (index <= length && index >= 0) {
            return datas[index];
        } else {
            throw new IndexOutOfBoundsException();
        }
    }
#指定位置插入元素
  /**
     * 指定位置插入元素
     *
     * @param index
     * @param element
     */
    public void insert(int index, int element) {
        if (index >= length || index < 0) {
            throw new IndexOutOfBoundsException();
        } else {
            if (length + 1 <= datas.length) {
                for (int i = length; i > index; i--) {
                    datas[i] = datas[i - 1];
                }
                datas[index] = element;
                length++;
            } else {
                System.out.println("表已满,无法插入");
            }
        }
    }
#删除指定下标元素
    /**
     * 删除指定下标元素
     *
     * @param index
     */
    public void delete(int index) {
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        } else {
            for (int i = index; i < length - 1; i++) {
                datas[i] = datas[i + 1];
            }
            length--;
        }
    }
#其他操作
    /**
     * 在表尾添加元素
     *
     * @param element
     */
    public void add(int element) {
        if (datas.length <= length) {
            System.out.println("表已满,无法添加");
        } else {
            datas[length] = element;
            length++;
        }
    }


     /**
     * 判断表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        if (datas == null || (datas.length == 0 && length == 0)) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 清空表
     */
    public void clear() {
        datas = null;
        length = 0;
    }

    /**
     * 获取表长
     *
     * @return
     */
    public int length() {
        return length;
    }

    /**
     * 获取表的最大容量
     *
     * @return
     */
    public int getMaxSize() {
        return datas.length;
    }
#输出顺序表的元素
     /**
     * 重写toString(),输出表中元素
     *
     * @return
     */
    @Override
    public String toString() {
        if (length <= 0) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            for (int i = 0; i < length; i++) {
                if (i == length - 1) {
                    sb.append(datas[i]);
                } else {
                    sb.append(datas[i] + ",");
                }
            }
            return sb.append("]").toString();
        }
    }

##代码测试

public class SeqListDemo {
    public static void main(String[] args) {
        int[] array=new int[]{2,4,8,3,6,1,4};
        System.out.println("//创建最大容量为10,指定数组元素的顺序表");
        SeqList seqList=new SeqList(10,array);//创建最大容量为10,指定数组元素的顺序表
        System.out.print("获取顺序表最大容量:");
        int maxSize = seqList.getMaxSize();
        System.out.println(maxSize);
        System.out.print("获取顺序表元素:");
        System.out.println(seqList.toString());
        System.out.print("获取顺序表长度:");
        System.out.println(seqList.length());
        System.out.print("添加元素0:");
        seqList.add(0);
        System.out.println(seqList.toString());
        System.out.print("在第1个数前插入元素10:");
        seqList.insert(0,10);
        System.out.println(seqList.toString());
        System.out.print("获取顺序表长度:");
        System.out.println(seqList.length());
        System.out.print("获取顺序表第3个数字:");
        System.out.println(seqList.get(2));
        System.out.println("清空顺序表");
        seqList.clear();
        System.out.print("顺序表元素:");
        System.out.println(seqList.toString());
    }
}

请添加图片描述

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值