数据结构一:数组ArrayList

目录

相关文章:

一、ArrayList:

二、LinkedList:

三、自己写的ArrayList:

四、测试类:

5、时间复杂度分析

5.1、动态数组的时间复杂度分析

5.2、均摊复杂度 amortized time complexity

5.3、复杂度震荡


相关文章:

Java集合源码剖析(系列文章 8篇)

一、ArrayList:

Access(查询):O(1)

Insert:平均O(n) 插入到最后一个,时间复杂度为O(1),插入到第一个位置,时间复杂度为O(n),平均为O(n/2)即O(n)

Delete:平均O(n) 同上

ArrayList :特点查询快,增删慢!图解如下

二、LinkedList:

Access(查询):O(n)

Insert:平均O(1) 

Delete:平均O(1) 

LinkedList:特点查询慢,增删快(基于链表结构的数据图解)

三、自己写的ArrayList:

public class LearnArray<E> {

    private E[] data;
    //数组存储的元素个数
    private int size;

    //构造函数,传入数组的容量capacity构造Array
    public LearnArray(int capacity) {
        data = (E[]) new Object[capacity];
        this.size = 0;
    }

    //无参数的构造函数,默认数组的容量capacity = 10
    public LearnArray() {
        this(10);
    }

    //获取数组的元素个数
    public int getSize() {
        return size;
    }

    //获取数组的容量
    public int getCapacity() {
        return data.length;
    }

    //返回数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //所有元素后面添加一个元素
    public void addLast(E e) {
        add(size, e);
    }

    //所有元素前面添加一个元素
    public void addFirst(E e) {
        add(0, e);
    }

    //在第index个位置插入一个新元素e
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add Failed. Require index >= 0 and index <= size");
        }
        if (size == data.length) {
//            throw new IllegalArgumentException("Add Failed. Array is Full");
            resize(data.length * 2);
        }

        //把插入位置后面所有的元素后移以为
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    //获取index索引位置的元素
    public E get(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Get Failed. Index is illegal. Require index >= 0 and index <= size");
        }
        return data[index];
    }

    //修改index位置的元素
    public void set(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Get Failed. Index is illegal. Require index >= 0 and index <= size");
        }
        data[index] = e;
    }

    //查找数组中是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return true;
        }
        return false;
    }

    //查找数组中元素e的索引,如果不存在该元素,返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    //从数组中删除index位置的元素,返回删除的元素
    public E remove(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Remove Failed. Index is illegal. Require index >= 0 and index <= size");
        }
        E ret = data[index];
//        for (int i = index; i < size; i++) {
//            data[index] = data[index + 1];
//        }
        for (int i = index; i < size; i++) {
            data[i] = data[i + 1];
        }
        size--;
        data[size] = null;//不是必须的,但能更好的释放内存

        //元素个数为容量的四分之一时,缩容为二分之一
        if (size == data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }
        return ret;
    }

    //从数组中删除第一个位置的元素,返回删除的元素
    public E removeFirst(int index) {
        return remove(0);
    }

    //从数组中删除最后一个位置的元素,返回删除的元素
    public E removeLast(int index) {
        return remove(size - 1);
    }

    //从数组中删除元素e
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1) {
            remove(index);
        }
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        sb.append('[');
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i != size - 1) {
                sb.append(", ");
            }
        }
        sb.append(']');
        return sb.toString();
    }
}

四、测试类:

public class TestArray2 {
    public static void main(String[] args) {
        LearnArray<Integer> array = new LearnArray<>();
        for (int i = 0; i < 10; i++) {
            array.addLast(i);
        }
        System.out.println(array);

        array.add(1,100);
        System.out.println(array);

        array.addFirst(-1);
        System.out.println(array);

        array.remove(2);
        System.out.println(array);

        array.removeElement(4);
        System.out.println(array);

//        LearnArray<Student> array2 = new LearnArray<>();
//        array2.addLast(new Student("小明",22));
//        array2.addLast(new Student("小明",33));
//        array2.addLast(new Student("小明",44));
//        System.out.println(array2);
    }
}

5、时间复杂度分析

5.1、动态数组的时间复杂度分析

5.2、均摊复杂度 amortized time complexity

resize的复杂度分析

9次addLast操作,触发resize,总共进行了17次基本操作,平均每次addLast操作,进行2次基本操作;

假设capacity = n ,n+1次addLast,触发resize,总共进行2n+1次基本操作,平均次addLast操作,进行2次基本操作。

这样均摊计算,时间复杂度是O(1)的!

在这个例子里,这样均摊计算,比计算最坏情况有意义,因为最坏的情况不会每次都出现。这样算下来,addLast的均摊复杂度为O(1)。这种均摊复杂度在很多教材中都不会提到,但在实际工程中,这种考虑是很有意义的。同理,我们看removeLast操作,均摊复杂度也为O(1)。

5.3、复杂度震荡

但是,当我们同时看addLast和removeLast操作:

原因和方案:

代码体现:

 if (size == data.length / 4 && data.length / 2 != 0){
    resize(data.length / 2);
 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值