java基础之ArrayList和Vector的主要区别;

List接口下一共实现了三个类:ArrayList,Vector,LinkedList。
LinkedList主要保持数据的插入顺序的时候使用,采用链表结构。

ArrayList,Vector主要区别为以下几点:
(1):Vector是线程安全的,源码中有很多的synchronized可以看出,而ArrayList不是。导致Vector效率无法和ArrayList相比;
(2):ArrayList和Vector都采用线性连续存储空间,当存储空间不足的时候,ArrayList默认增加为原来的50%,Vector默认增加为原来的一倍;
(3):Vector可以设置capacityIncrement,而ArrayList不可以,从字面理解就是capacity容量,Increment增加,容量增长的参数。

源码分析:
首先看看构造器:
ArrayList:三个

  /**
* Constructs an empty list with an initial capacity of ten.
* 构造一个默认初始容量为10的list
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
} /**
* 构造一个指定默认长度的list initialCapacity 不能小于0;
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
} /** 构造一个包含collection 元素的list
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
...
}

Vector:四个

//构造一个指定默认长度的list
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
//构造一个默认初始容量为10的list
public Vector() {
this(10);
}
//构造一个包含collection 元素的list
public Vector(Collection<? extends E> c) {
...
}
//区别在于可以设置capacityIncrement
public Vector(int initialCapacity, int capacityIncrement) {
super();
...
}

vector多了一个public Vector(int initialCapacity, int capacityIncrement)构造器,可以设置容量增长,arraylist是没有的。

主要添加源码分析

ArrayList类:

    public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
} private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code //如果添加一个元素之后,新容器的大小大于容器的容量,那么就无法存值了,需要扩充空间
if (minCapacity - elementData.length > 0) grow(minCapacity);
} private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); //扩充的空间增加原来的50%(即是原来的1.5倍)
if (newCapacity - minCapacity < 0) //如果容器扩容之后还是不够,那么干脆直接将minCapacity设为容器的大小
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0) //如果扩充的容器太大了的话,那么就执行hugeCapacity
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}

Vector类:

     public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
} private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
} private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity); /** 这个扩容需要做个判断:如果容量增量初始化的不是0,即使用的public Vector(int initialCapacity,int capacityIncrement) 构造方法进行的初始化,那么扩容的容量是(oldCapacity+capacityIncrement),就是原来的容量加上容量增量的值; 如果没有设置容量增量,那么扩容后的容量就是(oldCapacity+oldCapacity),就是原来容量的二倍。 **/ if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}

注:转载自:blog.csdn.net/ldxlz224/article/details/52574821

ArrayList和Vector区别的更多相关文章

  1. ArrayList与Vector区别

    ArrayList与Vector区别表 ArrayList Vector 1.实现原理:采用动态对象数组实现,默认构造方法创建了一个空数组 1.实现原理:采用动态数组对象实现,默认构造方法创建了一个大 ...

  2. java类集框架(ArrayList,LinkedList,Vector区别)

    主要分两个接口:collection和Map 主要分三类:集合(set).列表(List).映射(Map)1.集合:没有重复对象,没有特定排序方式2.列表:对象按索引位置排序,可以有重复对象3.映射: ...

  3. ArrayList和Vector区别及源码

    本文基于jdk1.7 1.ArrayList 类图来自:作者 Java3y 源码分析: 1.1 属性 1.2 构造方法 Arrays.copyOf源码: 1.3 trimToSize方法, 修改当前  ...

  4. List的三个子类ArrayList,LinkedList,Vector区别

    一:List的三个子类的特点 ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高.Vector: 底层数据结构是数组,查询快,增删慢. 线程安全,效率低.Vector相对A ...

  5. HashMap、HashTable、ArrayList、LinkedList、Vector区别

    HashTable和HashMap区别 ①继承不同. public class Hashtable extends Dictionary implements Map public class Has ...

  6. java arrayList vector 区别

    1. 关系图 List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList 2. ArrayList.Vector和LinkedList区别 ArrayList是最常用的 ...

  7. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  8. ArrayList和Vector的区别

    3.ArrayList和Vector的区别 答: 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种 ...

  9. Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法

    Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...

随机推荐

  1. spring的PathVariable和value={}小技巧(shiro项目中来的三)

    <property name="successUrl" value="/main/index" /> @RequestMapping(value=& ...

  2. xml的SAX解析和dom解析的区别

    一,区别 DOM解析 SAX解析 原理: 一次性加载xml文档,不适合大容量的文件读取 原理: 加载一点,读取一点,处理一点.适合大容量文件的读取 DOM解析可以任意进行增删改成 SAX解析只能读取 ...

  3. Java开发速度神器Lombok,Eclipse端安装使用教程

    一.Lombok简介 Lombok是一个代码生成器,可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法. 使用 ...

  4. docker学习系列(三):docker镜像的分层结构

    docker的镜像分层 docker里的镜像绝大部分都是在别的镜像的基础上去进行创建的,也就是使用镜像的分层结构. 实验 比如说使用dockerfile去创建一个最简单的hello镜像.创建好对应的d ...

  5. PHP 数组模糊查询

    function search() { $a=array( '0' => array('id'=>1,'pid'=>0,'name'=>'水果'), '1' => arr ...

  6. 关于js的语句类型运算符等

    JavaScript的基本语句有以下几种: 1.for(判断条件){执行内容}: 2.if(条件){...}else{ ....}: 3.while(条件){....}:先判断条件再执行: do{.. ...

  7. 【BZOJ1010】【HNOI2008】玩具装箱(斜率优化,动态规划)

    [BZOJ1010][HNOI2008]玩具装箱 题面 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一 ...

  8. 牛刀小试——记一次帮朋友小幅优化SQL

    和一个小朋友聊天,小朋友愁眉苦脸,不爱说话,我问怎么了,他说:经理交代的一个任务完成不了.我问:什么任务?他说:程序里的一个功能对应的SQL,太慢了.我问:现在性能是什么样?他说:一分钟.我问:达到什 ...

  9. 建立简单的Hash table(哈希表)by C language

    #define SIZE 1000 //定义Hash table的初始大小 struct HashArray { int key; int count; struct HashArray* next; ...

  10. AI行业需要什么样的人才

    自AI人工智能诞生以来,它的领域逐步扩大,技术层面也越来越多样化,投身于该领域的人才也越来越多,那么AI行业到底需要什么样的人才?我们应该如何定位自己,找到适合的领域?3月8日晚,在飞马网线上直播中, ...