ArrayList LinkedList  Vector 顺序添加

抽象数据类型(ADT)是一个实现包括储存数据元素的存储结构以及实现基本操作的算法。
ArrayList 
(1)ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长。  
(2)ArrayList不是线程安全的, ArrayList实现了Serializable接口,因此它支持序列化。
(3)实现了RandomAccess接口,支持快速随机访问,实际上就是通过下标序号进行快速访问。
(4)实现了Cloneable接口,能被克隆
ArrayList类提供了List ADT的一种可增长数组的实现。使用ArrayList的优点在于,对get和set的调用花费常数时间。其缺点是新项的插入和现有项的删除代价昂贵,除非变动是在ArrayList的末端是在ArrayList的末端运行。
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
transient Object[] elementData
public boolean add(E e) {
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    elementData[size++] = e;
    return true;
}
public E remove(int index) {
    rangeCheck(index);
 
    modCount++;
    E oldValue = elementData(index);
 
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
    return oldValue;
}
 
public boolean remove(Object o) {
    if (o == null) {
        for (int index = 0; index < size; index++)
            if (elementData[index] == null) {
                fastRemove(index);
                return true;
            }
    } else {
        for (int index = 0; index < size; index++)
            if (o.equals(elementData[index])) {
                fastRemove(index);
                return true;
            }
    }
    return false;
}
private void fastRemove(int index) {
    modCount++;
    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work
}
}
 
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(1);
arrayList.add(3);
 
arrayList.remove(2);
//arrayList.remove(Integer.valueOf(3));
for (Integer var : arrayList) {
    System.out.println(var);
}
1
1
 
LinkedList
LinkedList类则提供了List ADT的双链表实现。使用LinkedList的优点在于,新项的插入和现有项的删除均开销很小,这里假设变动项的位置是已知的。
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable{}
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;
    }
    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++;
    }
    Node<E> node(int index) {   //get方法
    // 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;
        }
    }
 
    public E remove(int index) {  //remove方法
        checkElementIndex(index);
        return unlink(node(index));
    }
 
    public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
}
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.add(1);
linkedList.add(3);
 
//linkedList.remove(2);
//linkedList.remove(Integer.valueOf(3));
for (Integer var : linkedList) {
    System.out.println(var);
}
1
1
3
 
Vector
Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。
vector是线程(Thread)同步(Synchronized)的,所以它也是线程安全的,而Arraylist是线程异步(ASynchronized)的,是不安全的。
public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{   protected Object[] elementData; 
 
    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
 
        return elementData(index);
    }
}
public synchronized boolean add(E e) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = e;
    return true;
}
public synchronized E remove(int index) {
    modCount++;
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    E oldValue = elementData(index);
 
    int numMoved = elementCount - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--elementCount] = null; // Let gc do its work
 
    return oldValue;
}
 
public boolean remove(Object o) {
    return removeElement(o);
}
 
public synchronized boolean removeElement(Object obj) {
    modCount++;
    int i = indexOf(obj);
    if (i >= 0) {
        removeElementAt(i);
        return true;
    }
    return false;
}
 
public synchronized int indexOf(Object o, int index) {
    if (o == null) {
        for (int i = index ; i < elementCount ; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = index ; i < elementCount ; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}
 
Vector<Integer> vector = new Vector<>();
vector.add(1);
vector.add(1);
vector.add(2);
 
vector.remove(2);  // remove index 2
for (Integer var : vector) {
    System.out.println(var);
}
1
1
      

Collection Lists的更多相关文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q116-Q120)

    Question  116 You are helping a corporate IT department create a SharePoint 2010 information archite ...

  2. mybatis使用foreach进行批量插入和删除操作

    一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...

  3. mybatis 批量删除添加

    mybatis使用foreach进行批量插入和删除操作   转发与    https://www.cnblogs.com/Amaris-Lin/p/8615977.html     一.批量插入 1. ...

  4. mybatis注解版in查询、字符串判空模糊匹配 、批量插入、插入返回主键

    IN查询 @Select({"<script> " + " select * "+ " from business_threat bt \ ...

  5. MySQL进行 批量插入,批量删除,批量更新,批量查询

    1.批量插入 ServiceImpl层 List<Person> addPeople = new ArrayList<>(); //addPeople存放多个Person对象 ...

  6. [Java Collection]List分组之简单应用.

    前言 今天有一个新需求, 是对一个List进行分组, 于是便百度到一些可用的代码以及我们项目使用的一些tools, 在这里总结下方便以后查阅. 一: 需求 现在我们一个数据库表t_series_val ...

  7. Guava库介绍之集合(Collection)相关的API

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  8. [Java Basics] Collection

    除了Java collection class/interface外,方便的有Google guava的utility class: Lists/Sets/Maps/Queues, 用它们可以方便地创 ...

  9. Collection List Set和Map用法与区别

    labels:Collection List Set和Map用法与区别 java 散列表 集合 Collection           接 口的接口      对 象的集合   ├   List   ...

随机推荐

  1. 【git】如何ignore一个文件的更改又保留其初始版本

    参考: https://compiledsuccessfully.dev/git-skip-worktree/ https://stackoverflow.com/questions/9794931/ ...

  2. SecureCRT key登录linux ssh设置

    一.首先用secureCrt创建密钥 1.使用SecureCRT创建私钥和公钥. SecureCRT quick Connect-> Authentiation -> Public Key ...

  3. flex属性flex-grow、flex-shrink、flex-basis

    tip: 1)这些属性写在子元素中,作用于子元素(父元素中应设置display:flex) 2)作用是子元素如何分配父元素的空间 3) flex-grow 是扩展比率,当子元素宽度总和小于父元素宽度时 ...

  4. maven命名

    <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...

  5. Vue手把手教你撸一个 beforeEnter 钩子函数

    地址 :https://www.jb51.net/article/138821.htm 地址 :https://www.jb51.net/article/108964.htm

  6. 关于原生js中ie的attacheEvent事件用匿名函数改变this指向后,不能用detachEvent删除绑定事件的解决办法?

    博客搬迁,给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/11/28/%e5%85%b3%e4%ba%8e%e5%8e%9f%e7%94%9fjs%e ...

  7. python- 粘包 struct,socketserver

    黏包 黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cmd.decode('utf-8'), sh ...

  8. 在知乎上看到的几个关于C的奇淫技巧

    有一个鲜为人知的运算符叫”趋向于”, 写作“-->”.比如说如果要实现一个倒数的程序,我们可以定义一个变量x,然后让它趋向与0: 输出: 然后我们把 "x-->0" 换 ...

  9. 【mysql升级步骤】windows mysql版本升级 ,mysql 5.6 升级到5.7.27

    最近博主由于工作原因需要把之前安装好的的mysql 5.6.44版本卸载,然后安装mysql 5.7.*版本. 前提:为什么要升级到5.7版本? 因为博主在5.6版本上执行脚本时候报出异常:to yo ...

  10. strcoll - 用当前的区域选项来比较两个字符串

    总览 (SYNOPSIS) #include <string.h> int strcoll(const char *s1, const char *s2); 描述 (DESCRIPTION ...