ArrayList和LinkedList的插入和访问的时间复杂度?

本文深入探讨了ArrayList和LinkedList两种数据结构的插入和访问操作的时间复杂度,详细分析了源码实现,指出ArrayList插入可能达到O(n),访问为O(1),而LinkedList插入为O(1),访问为O(n)。

ArrayList和LinkedList的插入和访问的时间复杂度?**

ArrayList插入源码:

如果ArrayList没有扩张,那么插入的复杂度为O(1);

如果ArrayList扩张了,那么插入的复杂度主要集中在System.arraycopy()这个方法的复杂度上,复杂度大概最差O(n)。(作者实在是没找到资料,于是猜想最差就是for循环复制,最好应该就是用并行流)

/**
* This helper method split out from add(E) to keep method
* bytecode size under 35 (the -XX:MaxInlineSize default value),
* which helps when add(E) is called in a C1-compiled loop.
*/
private void add(E e, Object[] elementData, int s) {
    if (s == elementData.length)
   		elementData = grow();
    elementData[s] = e;
    size = s + 1;
}
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
* @throws OutOfMemoryError if minCapacity is less than zero
*/
private Object[] grow(int minCapacity) {
    return elementData = Arrays.copyOf(elementData,
                                       newCapacity(minCapacity));
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

ArrayList访问源码:

复杂度为O(1)。

/**
* Returns the element at the specified position in this list.
*
* @param  index index of the element to return
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
    Objects.checkIndex(index, size);
    return elementData(index);
}
public static <X extends RuntimeException>
    int checkIndex(int index, int length,
                   BiFunction<String, List<Integer>, X> oobef) {
    if (index < 0 || index >= length)
    	throw outOfBoundsCheckIndex(oobef, index, length);
    return index;
}

LinkedList插入源码:

插入复杂度为O(1)。

/**
* Links e as last element.
*/
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
    	first = newNode;
    else
    	l.next = newNode;
    size++;
    modCount++;
}

LinkedList访问源码:

访问的复杂度为O(n)。

/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
    if (index < (size >> 1)) {
        Node<E> x = first;
        for (int i = 0; i < index; i++)
        	x = x.next;
        return x;
    } else {
        Node<E> x = last;
        for (int i = size - 1; i > index; i--)
        	x = x.prev;
        return x;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值