java 单列集合

集合:

  • Collection:所有单列集合得基类接口【list 和 set】:

  • Collection的常用方法:

    boolean add(E e);
    void clear();
    boolean remove(E e);
    boolean contains();
    boolean isEmpty();
    int size();
    //Colltection 的iterator对象方法;
    hasNext();
    next();
    remove();  //禁止使用集合方法增删;
    //List 的iterator对象方法,增加了一个插入方法;
    add();	
    //此外还有:从后往前便利,要和next配套使用,鸡肋;
    hasPrevious();
    previous();
    
  • List集合,因为有索引,且继承了Collection方法;

  • List常用方法,基本对索引操作,后返回操作数值:

    void add(int index, E e); // collection的add()默认加入到最后;
    E remove();
    E set(int index, E e);
    E get(int index);
    
  • ArrayList集合:

    • ArrayList底层数据的结构是数组形式;
    • 数组初始化为空集合;
      • 当第一次add()后,会扩容至10;
      • 后面加满了会自动扩容为原来的1.5倍;
      • 当使用addAll(),添加多个数据时,而且需要扩容量大于默认扩容量,则会扩容需要量
  • LinkedList集合,双链表。

  • LinkedList特有方法:

    void addFirst(E e);
    void addLast(E e);
    E getFirst();
    E getLast();
    E removeFirst();
    E removeLast();
    //add(E e) 的核心代码:
    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++;  // 在iterator中使用,防止并发修改异常【在迭代过程中,允许使用集合方法增删集合】;
    }
    
posted @ 2025-03-02 11:07  烟雨断桥  阅读(8)  评论(0)    收藏  举报