美文网首页
LinkList与ArrayList

LinkList与ArrayList

作者: 蜗牛1991 | 来源:发表于2017-09-20 14:17 被阅读0次

1.LinkList

  • java实现双向链表
 //链表节点模型
 private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

2.ArrayList

//初始化大小为10;
int DEFAULT_CAPACITY = 10;
//存储结构为数组
private transient Object[] elementData;
//扩容方式,扩大一倍,复制数组
private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

3.HashSet

//底层为hashMap,默认为16;
 public HashSet(Collection<? extends E> c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }
//hashSet对象为key,value为固定的object对象的hashMap,具体看hashMap方法
  public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

相关文章

网友评论

      本文标题:LinkList与ArrayList

      本文链接:https://www.haomeiwen.com/subject/dargjxtx.html