数据结构之数组

写在前面

看下数组。

1:巴拉巴拉

数组是一种线性数据结构,使用连续的内存空间来存储数据,存储的数据要求有相同的数据类型,并且每个元素占用的内存空间相同。获取元素速度非常快,为O(1)常量时间复杂度,所以数组在我们工作直接或者是间接的用到还是比较多的。

2:代码

定义接口:

java 复制代码
package com.dahuyou.datastructure.arraylist;

public interface List<E> {

    /**
     * Appends the specified element to the end of this list (optional
     * operation).
     * <p>
     * 追加元素到尾部!
     */
    boolean add(E e);

    /**
     * Removes the element at the specified position in this list (optional
     * operation).  Shifts any subsequent elements to the left (subtracts one
     * from their indices).  Returns the element that was removed from the
     * list.
     *
     * 删除指定位置的元素
     *
     */
    E remove(int index);

    /**
     * Returns the element at the specified position in this list.
     *
     * 返回指定位置的元素
     */
    E get(int index);

}

实现类,这里不贴所有代码了,只看下重要的方法。

  • add
java 复制代码
@Override
public boolean add(E e) {
    // 确保内部容量
    int minCapacity = size + 1;
    if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
        minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
    }
    // 判断扩容操作,即将满时扩容,扩容为原来的1.5倍,直接通过oldCapacity + (oldCapacity >> 1)移位操作完成,效率高
    if (minCapacity - elementData.length > 0) {
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        elementData = Arrays.copyOf(elementData, newCapacity);
    }
    // 添加元素
    elementData[size++] = e;
    return true;
}

这里在两种情况下会进行扩容,第一次插入元素时扩容到10,之后元素满时扩容到原来的1.5倍,这里使用了移位运算,效率更高。

  • remove
java 复制代码
@Override
public E remove(int index) {
    E oldValue = (E) elementData[index];
    int numMoved = size - index - 1;
    if (numMoved > 0) {
        // 从原始数组的某个位置,拷贝到目标对象的某个位置开始后n个元素
        System.arraycopy(elementData, index + 1, elementData, index, numMoved);
    }
    elementData[--size] = null; // clear to let GC do its work
    return oldValue;
}

注意这里删除元素并不是通过一个一个的交换元素来实现的,而是直接通过native方法java.lang.System#arraycopy:

java 复制代码
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

因为是底层直接操作内存,所以效率更高。

测试:

java 复制代码
package com.dahuyou.datastructure.arraylist;

public class TT {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("01");
        list.add("02");
        list.add("03");
        list.add("04");
        list.add("05");
        list.add("06");
        list.add("07");
        list.add("08");
        list.add("09");
        list.add("10");
        list.add("11");
        list.add("12");

        System.out.println(list);

        list.remove(9);

        System.out.println(list);
    }

}

运行:

复制代码
ArrayList{elementData=[01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, null, null, null], size=12}
ArrayList{elementData=[01, 02, 03, 04, 05, 06, 07, 08, 09, 11, 12, null, null, null, null], size=11}

Process finished with exit code 0

写在后面

参考文章列表

相关推荐
雾里看山3 小时前
顺序表VS单链表VS带头双向循环链表
数据结构·链表
好好研究5 小时前
学习栈和队列的插入和删除操作
数据结构·学习
挺菜的8 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
2401_858286118 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8369 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
学不动CV了12 小时前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
算法_小学生14 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
Wo3Shi4七17 小时前
哈希冲突
数据结构·算法·go
V我五十买鸡腿18 小时前
顺序栈和链式栈
c语言·数据结构·笔记·算法
七灵微19 小时前
数据结构实验习题
数据结构