java 语言实现线性表

线性表(List):零个或多个数据元素的有限序列

package Mooc_zju.linearList;

/**
 * 线性表的数组实现方式
 * @author chenhuan
 * @time 2018-3-16
 */
public class ArrayList<E> {
    Object [] data = null;   //用来保存内容的数组
    private int current;    //保存当前第几个元素
    private int capacity;   //保证数组大小的指标

    /**
     * 无参构造函数
     */
    public ArrayList(){
        this(10);//如果没有指定初始化大小,默认为10,调用有参构造函数
    }

    /**
     * 有参构造函数
     * @param intialSize
     */
    public ArrayList(int intialSize ){
        if (intialSize < 0){
            throw new  RuntimeException("数组大小错误: " + intialSize);
        }
        else {
            this.data = new Object[intialSize]; //初始化数组
            this.current = 0;   //当前值为0
            this.capacity = intialSize;     //数组大小为初始化大小
        }
    }

    /**
     * 保证数组的容量
     * @param cur
     */
    private void ensureCapacity(int cur){
        if (cur == capacity){    //如果当前传入的容量和最大容量相等,准备扩容
            this.capacity = capacity + 10;  //  每次扩容加10
            Object [] newData = new Object[capacity];
            for (int i=0;i<cur;i++){
                newData[i] = this.data[i];  //将当前数组转到新的扩容数组中
            }
            this.data = newData;
        }
    }

    /**
     * 插入元素
     * @param e
     * @return
     */
    public boolean add(E e){
        ensureCapacity(current);    //保证容量
        this.data[current] = e ;
        current++;
        return true;
    }

    /**
     * 根据指定的 index 获取元素
     * @param index
     * @return
     */
    public E get(int index){
        validateIndex(index);
        return (E)this.data[index];
    }

    /**
     * 验证元素的下标大小是否越界
     * @param i
     */
    private void  validateIndex(int i){
        if (i<0 || i> current){
            throw  new RuntimeException("获取元素位置错误: "+ i);
        }
    }

    /**
     * 在指定位置插入元素
     * @param index 指定的索引
     * @param element   插入的元素
     */
    public boolean insert(int index ,E element){
        validateIndex(index);   //验证下标是否合法
        Object[] temp = new Object[capacity];   //构建一个缓存数组
        for (int i = 0;i<=current;i++){
            if (i<index){
                temp[i] = this.data[i];
            }else if (i == index){
                temp[i] = element;
            }else if(i > index){
                temp[i] = this.data[i-1];
            }
        }
        this.data = temp;
        return true;
    }

    /**
     * 删除指定下标的元素
     * @param index
     * @return
     */
    private boolean delete(int index){
        validateIndex(index);
        for (int i = index ;i < current ;i++){
            this.data[i] = this.data[i+1];  //index 之后的元素全部向前移动一位
        }
        return false;
    }
    /**
     * 输出元素的长度
     * @return
     */
    public int length(){
        return current;
    }

    /**
     * 打印元素
     */
    public void printArray(){
        for (int i =0; i<current;i++){
            System.out.print(this.data[i]+" ");
        }
    }



}

转载于:https://www.cnblogs.com/HuanChen1025/p/8999280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
线性表是一种常见的数据结构,可以使用Java语言实现。下面是一个简单的线性表实现示例: ```java public class MyArrayList<E> { private int size; // 线性表大小 private Object[] elementData; // 存储元素的数组 // 构造函数 public MyArrayList() { this.size = 0; this.elementData = new Object[10]; // 初始化数组大小为10 } // 获取线性表大小 public int size() { return this.size; } // 判断线性表是否为空 public boolean isEmpty() { return this.size == 0; } // 获取指定位置的元素 public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of range: " + index); } return (E) elementData[index]; } // 添加元素到线性表尾部 public void add(E e) { ensureCapacity(size + 1); // 确保容量足够 elementData[size++] = e; // 将元素添加到数组中 } // 在指定位置插入元素 public void add(int index, E e) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index out of range: " + index); } ensureCapacity(size + 1); // 确保容量足够 System.arraycopy(elementData, index, elementData, index + 1, size - index); // 将元素后移 elementData[index] = e; // 插入新元素 size++; // 更新线性表大小 } // 删除指定位置的元素 public E remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of range: " + index); } E oldValue = (E) elementData[index]; // 获取要删除的元素 int numMoved = size - index - 1; if (numMoved > 0) { System.arraycopy(elementData, index + 1, elementData, index, numMoved); // 将元素前移 } elementData[--size] = null; // 将最后一个元素置空 return oldValue; } // 确保数组容量足够 private void ensureCapacity(int minCapacity) { int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { int newCapacity = oldCapacity * 2; if (newCapacity < minCapacity) { newCapacity = minCapacity; } elementData = Arrays.copyOf(elementData, newCapacity); // 扩容数组 } } } ``` 在上面的示例中,我们使用一个Object类型的数组来存储元素,并通过泛型来支持不同类型的元素。通过实现常用的方法,我们可以对线性表进行添加、删除、获取等操作。需要注意的是,当线性表大小超过数组容量时,我们需要动态扩容数组,以保证能够继续添加元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值