1.4数据结构学习-列表

1.4数据结构学习-列表

列表

1.描述:列表是一种数组形式存储有序列表
2.存储方式:以动态数组存储,默认扩展因数为1.5倍
3.内存分布:存储的每个节点在内存中并不连续,如产生增删元素,则会产生后续元素移位

列表DEMO

public class MyArrayList<T>
{
    /**
     * 默认列表长度
     */
    private final static int DEFAULT_SIZE = 10;
    /**
     * 内部存储数组
     */
    private Object[] elementData;
    /**
     * 数组中有效数据的大小
     */
    private int size;

    /**
     * 默认构造函数
     * 定义一个长度为10的数组
     */
    public MyArrayList()
    {
        elementData = new Object[DEFAULT_SIZE];
    }

    /**
     * 有参构造函数
     * 定义自定义长度数组
     */
    public MyArrayList(int max)
    {
        elementData = new Object[max];
    }
    /**
     * @param value 加入目标元素
     * @return boolean 成功失败
     * @desc : 不指定索引新增元素
     * @author 李旭辉
     * @date 2021/5/28 0028 22:58
     **/
    public boolean add(T value)
    {
        ensureExplicitCapacity(size + 1);
        elementData[size++] = value;
        return true;
    }
    /**
     * @param value 加入目标元素
     * @return boolean 成功失败
     * @desc : 指定索引新增元素
     * @author 李旭辉
     * @date 2021/5/28 0028 22:58
     **/
    public boolean add(int index, T value)
    {
        ensureExplicitCapacity(size + 1);
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = value;
        size++;
        return true;
    }
    /**
     * @param element 查找目标
     * @return int 返回下标
     * @desc : 查找目标元素
     * @author 李旭辉
     * @date 2021/5/28 0028 22:59
     **/
    public int find(T element)
    {
        int i;
        for (i = 0; i < size; i++)
        {
            if (element.equals(elementData[i]))
            {
                return i;
            }
        }
        return -1;
    }

    /**
     * @param element 删除目标对象
     * @return boolean 操作成功失败
     * @desc : 删除指定目标元素
     * @author 李旭辉
     * @date 2021/5/28 0028 22:59
     **/
    public boolean remove(T element)
    {
        int index;
        if ((index = find(element)) == -1)
        {
            System.out.println("删除目标不存在");
            return false;
        }
        System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
        elementData[--size] = null;
        return true;
    }

    /**
     * @param index 指定索引
     * @return boolean 操作成功失败
     * @desc : 删除指定索引上的目标元素
     * @author 李旭辉
     * @date 2021/5/28 0028 22:59
     **/
    public boolean remove(int index)
    {
        rangeCheck(index);
        System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
        elementData[--size] = null;
        return true;
    }


    /**
     * @param index 指定索引
     * @return void
     * @desc : 判断索引是否越界
     * @author 李旭辉
     * @date 2021/5/28 0028 23:01
     **/
    private void rangeCheck(int index)
    {
        if (index >= size)
        {
            throw new IndexOutOfBoundsException(index + ": 索引越界");
        }
    }

    /**
     * @param minCapacity 所需满足的最小长度
     * @return void
     * @desc : 判断当前数组是否满足并扩容
     * @author 李旭辉
     * @date 2021/5/28 0028 23:01
     **/
    private void ensureExplicitCapacity(int minCapacity)
    {
        int oldCapacity = elementData.length;
        if (minCapacity > oldCapacity)
        {
            System.out.println("数组长度不够,需要扩容,尝试扩容到1.5倍,当前数组长度:" + oldCapacity);
            int newCapacity = oldCapacity + (oldCapacity >> 1);
            if (newCapacity - minCapacity < 0)
            {
                System.out.println("数组尝试扩容到1.5倍不能满足,扩容至指定长度");
                newCapacity = minCapacity;
            }
            T[] copy = (T[]) Array.newInstance(elementData.getClass().getComponentType(), newCapacity);
            System.arraycopy(elementData, 0, copy, 0, Math.min(elementData.length, newCapacity));
            elementData = copy;
        }
    }

    /**
     * @param
     * @return void
     * @desc : 展示列表
     * @author 李旭辉
     * @date 2021/5/28 0028 23:04
     **/
    public void display()
    {
        for (int i = 0; i < size; i++)
        {
            System.out.print(elementData[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        System.out.println("--------------------------------------");
        System.out.println("初始化长度为5的列表");
        MyArrayList myArrayList = new MyArrayList<Integer>(5);
        for (int i = 0; i < 10; i++)
        {
            myArrayList.add(i);
            System.out.println("加入元素: " + i);
        }
        System.out.println("--------------------------------------");
        System.out.println("列表元素为:");
        myArrayList.display();
        System.out.println("--------------------------------------");
        System.out.println("按照索引删除索引为8元素");
        myArrayList.remove(8);
        Integer o = 9;
        System.out.println("按照对象删除元素:" + o);
        myArrayList.remove(o);
        System.out.println("--------------------------------------");
        System.out.println("列表元素为:");
        myArrayList.display();
        System.out.println("--------------------------------------");
    }
}


结果

--------------------------------------
初始化长度为5的列表
加入元素: 0
加入元素: 1
加入元素: 2
加入元素: 3
加入元素: 4
数组长度不够,需要扩容,尝试扩容到1.5,当前数组长度:5
加入元素: 5
加入元素: 6
数组长度不够,需要扩容,尝试扩容到1.5,当前数组长度:7
加入元素: 7
加入元素: 8
加入元素: 9
--------------------------------------
列表元素为:
0 1 2 3 4 5 6 7 8 9 
--------------------------------------
按照索引删除索引为8元素
按照对象删除元素:9
0 1 2 3 4 5 6 7 
--------------------------------------
列表元素为:
0 1 2 3 4 5 6 7 
--------------------------------------


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值