欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

Vector和ArrayList的区别,vectorarraylist

来源: javaer 分享于  点击 48353 次 点评:180

Vector和ArrayList的区别,vectorarraylist


Vector和ArrayList两个集合类的本质没有太大的不同,都是实现了List接口,而且底层都是基于Java数据来存储集合元素的。

1、不同的在于序列化方面:ArrayList比Vector安全,

在ArrayList集合中:

//采用elementData数组来存储集合元素

private transient Object[] elementData;

在Vector集中中:

//采用elementData数组来保存集合元素

private Object[] elementData;

从源码可以看出,ArrayList提供的writeObject和readObject方法来实现定制序列化,而Vector只是提供了writeObject方法,并没有完全实现定制序列化。

2、不同点在于Vector是线性安全的,ArrayList是非线性安全的,从源码可以清晰的看出这点

ArrayList和Vector的绝大部分方法都是一样的,甚至连方法名都一样,只是Vector的方法大都添加关键之synchronized修饰。

在add方法中,Vector电泳的是insertElementAt(element,index);

public synchronized void isnertElementAt(E obj,int index);

将 ArrayList中的add(int index,E element)方法和Vector的isnertElementAt(E Obj,int index)方法进行对比,可以发现vectorde insertElementAt(E obj,int index)方法只是多了synchronized修饰。

3、扩容上区别

ArrayList集合和Vector集合底层都是数组实现的,在数组容量不足的时候采取的扩容机制不同。

ArrayList集合容量不足,采取在原有容量基础上扩充为原来的1.5倍。


而Vector则多了一个选择:当capacityIncrement实例变量大于0时,扩充为原有容量加上capacityIncrement的容量值。否则采取在原有容量基础上扩充为原来的1.5倍。

原创博文:转载请说明出处。

相关文章

    暂无相关文章

用户点评