Java顺序表实现

**
顺序存储定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构
顺序表的特点:以物理位置相邻表达逻辑关系
顺序表的优点:任一元素可随机存取
顺序表的缺点:进行插入和删除操作时,需要移动大量元素
**
依次存取,地址连续——中间没有空余的存储结构

package SequentialList;

import java.util.Arrays;

public class IList {
    public int[] elem;  //存储空间的基地址
    public int usedSize;  //当前已被使用的空间大小
    private static  final int DEFAULT_SIZE = 10;  //默认容量

    public IList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    //顺序表的打印
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

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

    //判断数组容量是否充足
    public boolean isAdequate() {
        if (this.elem.length >= this.usedSize) {
            return true;
        }else{
            return false;
        }
    }

    /*
    默认在数组最后位置新增的
    数组容量进行二倍扩容
    */
    public void addTail(int data){
        if (!this.isAdequate()){
            this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    /*
    在数组的指定位置新增
     */
    public void addSpecified(int data, int position) {
        if (!this.isAdequate()){
            this.elem = Arrays.copyOf(this.elem, this.usedSize * 2);
        }
        if (position < 0 || position > this.usedSize) {
            System.out.println("position参数不合法");
        }else{
            for (int i = this.usedSize - 1; i >= position; i--) {
                this.elem[i + 1] = this.elem[i];
            }
            this.elem[position] = data;
            this.usedSize++;
        }
    }

    /*
    查找是否包含某个元素
     */
    public boolean toFind(int digital) {
        for (int i = 0; i < this.elem.length; i++) {
            if (elem[i]==digital) {
                return true;
            }
        }
        return false;
    }

    /*
    查找某个元素的index
     */
    public int indexOf(int digital) {
        for (int i = 0; i < this.elem.length; i++) {
            if (elem[i]==digital) {
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }

    /*
    获取position的位置
     */
    public int get(int position) {
        if (this.isEmpty()) {
            System.out.println("顺序表里没有元素");  //感觉抛异常好一点

        }
        if (position < 0 || position > this.usedSize) {
            System.out.println("position参数不合法");
        }
        return this.elem[this.indexOf(position)];
    }

    /*
    修改指定位置的值
     */
    public void setValue(int position, int value) {
        if (this.isEmpty()){
            System.out.println("顺序表里没有元素");  //感觉抛异常好一点
        }
        if (position < 0 || position > this.usedSize) {
            System.out.println("position参数不合法");
        }
        this.elem[position] = value;
    }

    /*
    删除指定元素
     */
    public void removeDigital(int value) {
        if (this.isEmpty()){
            System.out.println("顺序表里没有元素");
        }
        int key = this.indexOf(value);
        if (key==-1){
            System.out.println("顺序表里没有该元素");
        }else{
            for (int i = key+1; i < this.usedSize; i++) {
                this.elem[i-1] = this.elem[i];
            }
            this.usedSize--;
        }

    }

    /*
    删除指定pos的value
     */
    public void removePos(int pos) {
        if (!this.isEmpty()){
            if (pos < 0 || pos > this.usedSize){
                System.out.println("输入参数非法");
            }else{
                for (int i = pos-1; i < this.usedSize; i++) {
                    this.elem[i] = this.elem[i+1];
                }
                this.usedSize--;
            }
        }
    }

    /*
    清空顺序表
     */
    public void clear() {
        this.usedSize = 0;
    }
}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值