ArrayList与Vector的区别
相同
这两个类都实现了List接口。
他们都是有序集合。
不同
ArrayList实现不是同步的,Vector实现是同步的。
ArrayList与Vector都有一个初始的容量大小,当存储进它们里面的元素的个数超过了容量时,就需要增加ArrayList与Vector的存储空间,每次要增加存储空间时,不是只增加一个存储单元,而是增加多个存储单元,每次增加的存储单元的个数在内存空间利用与程序效率之间要取得一定的平衡。Vector默认增长为原来两倍,而ArrayList的增长策略在文档中没有明确规定(从源代码看到的是增长为原来的1.5倍)。
下面两段代码主要演示了线程同步的区别:
ArrayList:
import java.util.ArrayList;
/**
*
* @author InJavaWeTrust
*
*/
public class ArrayListMultiThread implements Runnable {
public static ArrayList<Integer> li = new ArrayList<Integer>();
public void run() {
for (int i = 0; i < 100000; i++) {
li.add(i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new ArrayListMultiThread());
Thread t2 = new Thread(new ArrayListMultiThread());
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(li.size());
}
}
运行结果:

这是因为ArrayList在扩展过程中,内部一致性遭到破坏,由于不是同步的,另外一个线程访问到了不一致的内部状态,导致出现越界问题。
Vector:
import java.util.Vector;
/**
*
* @author InJavaWeTrust
*
*/
public class VectorMultiThread implements Runnable {
public static Vector<Integer> v = new Vector<Integer>();
public void run(){
for (int i = 0; i < 100000; i++) {
v.add(i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new VectorMultiThread());
Thread t2 = new Thread(new VectorMultiThread());
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(v.size());
}
}
运行结果:

ArrayList与Vector的区别的更多相关文章
- ArrayList和Vector的区别
3.ArrayList和Vector的区别 答: 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种 ...
- ArrayList 和 Vector 的区别
这两个类都实现了 List 接口( List 接口继承了 Collection 接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态的数组,我们以后可以按位置索引号取 ...
- ArrayList和Vector的区别?
ArrayList和Vector的区别? 解答:同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程不安全的,不是同步的:数据增长:当需要增长时,Vector默认增长为原来一 ...
- ArrayList和Vector的区别?HashMap和HashTable的区别?StringBuilder、StringBuffer和String的区别?
ArrayList和Vector的区别?从两个方面 1.同步性:ArrayList是线程不安全的,是非同步的:Vector是线程安全的,是同步的.(Java中线程的同步也就满足了安全性) 2.数值增长 ...
- 一、基础篇--1.2Java集合-ArrayList和Vector的区别
ArrayList和Vector的区别 ArrayList和Vector都是基于动态数组实现的. 区别 ArrayList是非线程安全的,Vector是线程安全的. Vector的方法都加了同步锁 ...
- 集合框架,ArrayList和Vector的区别,让arrayList线程安全的几种方案
boolean add(E e) 将指定的元素添加到此列表的尾部. void add(int index, E element) 将指定的元素插入此列表中的指定位置. boolean addAll(C ...
- 【Java面试题】32 ArrayList和Vector的区别
1. Vector & ArrayList 相同点: 1.ArrayList和Vector都是继承了相同的父类和实现了相同的接口 2.底层都是数组实现的 3.初始默认长度都为10. 不同点: ...
- Java中ArrayList和LinkedList区别、ArrayList和Vector的区别
一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,Ar ...
- hashmap和hashtable,arraylist和vector的区别
hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...
随机推荐
- 重构:从Promise到Async/Await
摘要: 夸张点说,技术的发展与历史一样,顺之者昌,逆之者亡.JS开发者们,赶紧拥抱Async/Await吧! GitHub仓库: Fundebug/promise-asyncawait 早在半年多之前 ...
- 一个使用 Web Components 的音乐播放器: MelodyPlayer
先上效果预览: Web Components 首先,什么是 Web Components ? MDN 给出的定义是: Web Components 是一套不同的技术,允许您创建可重用的定制元素(它们的 ...
- 继承自 DevExpress 17.2 的自定义控件如何在工具箱显示
最近把DevExpress版本从13.1升级到了17.2,结果发现继承自DevExpress的自定义控件居然在工具箱中消失了,弄了两天还是没有任何头绪,部分自定义Dev控件可以正常出现,但大部分自定义 ...
- 存出和载入Docker镜像
存出镜像 如果要导出镜像到本地文件,可以使用 docker save 命令. $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL ...
- windows上安装nodejs,升级npm,安装webpack指南
安装nodejs https://nodejs.org/en/ 安装webpack和其他一些常用的 npm install -g node-gyp webpack coffee-script 监控 w ...
- Linux下文件的mtime/atime/ctime研究
概述 在Linux下,对于某一个文件或文件夹时间的描述有三种:文件修改时间mtime,文件访问时间atime,文件状态改变时间ctime.在Linux下无法获取到文件的创建时间,因为根本就没有保存这个 ...
- linux TCP头部的构造的简单分析
TCP的头部的构造是在函数tcp_transmit_skb()中进行的 函数片段如下: /* Build TCP header and checksum it. */ th = tcp_hdr(skb ...
- Low-rank approximations
Low-rank approximations Give matrix and a positive integer , we wish to find an matrix of rank at mo ...
- 给定一数组,输出满足2a=b(a,b代表数组中的数)的数对,要求时间复杂度尽量低。
//时间复杂度O(n),空间复杂度O(n) void findSequence(int* arr, int len) { int* hashtable = new int[RANGE]; memset ...
- (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)
接下来,我们再讲一下图文消息: 如图: 大家可以先从开发者文档中了解一下图文消息的一些参数: 如上图,用户回复4时,ipastor返回了几条图文消息,上图中属于多图文消息,当然还有单图文消息,图文消息 ...