ArrayList VS Vector(ArrayList和Vector的区别)_面试的时候经常出现

数据增长:当需要增长时,Vector默认增长为原来一培,而ArrayList却是原来的一半
同步性:Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步的 

这个我们可以看看他们的源码就可以知道:

ArrayList的size()方法:

1 /**
2      * Returns the number of elements in this list.
3      *
4      * @return the number of elements in this list
5      */
6     public int size() {
7         return size;
8     }

Vector的size()方法:

1 /**
2      * Returns the number of components in this vector.
3      *
4      * @return  the number of components in this vector
5      */
6     public synchronized int size() {
7         return elementCount;
8     }

其实Vector的其他很多方法都是同步的:

 1 /**
 2      * Sets the size of this vector. If the new size is greater than the
 3      * current size, new {@code null} items are added to the end of
 4      * the vector. If the new size is less than the current size, all
 5      * components at index {@code newSize} and greater are discarded.
 6      *
 7      * @param  newSize   the new size of this vector
 8      * @throws ArrayIndexOutOfBoundsException if the new size is negative
 9      */
10     public synchronized void setSize(int newSize) {
11         modCount++;
12         if (newSize > elementCount) {
13             ensureCapacityHelper(newSize);
14         } else {
15             for (int i = newSize ; i < elementCount ; i++) {
16                 elementData[i] = null;
17             }
18         }
19         elementCount = newSize;
20     }
21 
22     /**
23      * Returns the current capacity of this vector.
24      *
25      * @return  the current capacity (the length of its internal
26      *          data array, kept in the field {@code elementData}
27      *          of this vector)
28      */
29     public synchronized int capacity() {
30         return elementData.length;
31     }
32 
33     /**
34      * Returns the number of components in this vector.
35      *
36      * @return  the number of components in this vector
37      */
38     public synchronized int size() {
39         return elementCount;
40     }
41 
42     /**
43      * Tests if this vector has no components.
44      *
45      * @return  {@code true} if and only if this vector has
46      *          no components, that is, its size is zero;
47      *          {@code false} otherwise.
48      */
49     public synchronized boolean isEmpty() {
50         return elementCount == 0;
51     }

那现在我们应该设计一个方案来研究我们的这些结论的正确性!!

下面给出ArrayList线程不安全证明方案:

 1 /**
 2  * 
 3  */
 4 package com.b510;
 5 
 6 import java.util.ArrayList;
 7 import java.util.Collections;
 8 import java.util.List;
 9 
10 /**
11  * @date 2013-5-2
12  * @author xhw
13  * 
14  */
15 public class HongtenArrayList implements Runnable {
16     //线程不安全
17      List<String> list = new ArrayList<String>(1);
18     //线程安全
19     //List<String> list = Collections.synchronizedList(new ArrayList<String>());
20     public void run() {
21         try {
22             Thread.sleep((int) (Math.random() * 2));
23         } catch (InterruptedException e) {
24             e.printStackTrace();
25         }
26         System.out.println(Thread.currentThread().getName());
27         list.add(Thread.currentThread().getName());
28     }
29 
30     public static void main(String[] args) throws InterruptedException {
31         ThreadGroup group = new ThreadGroup("hongtenGroup");
32         HongtenArrayList t = new HongtenArrayList();
33         for (int i = 0; i < 10000; i++) {
34             Thread th = new Thread(group, t, String.valueOf(i));
35             th.start();
36         }
37         while (group.activeCount() > 0) {
38             Thread.sleep(10);
39         }
40         System.out.println("result=============");
41         //如果线程安全,那么结果是10000,如果线程不安全,则结果不可预测
42         System.out.println(t.list.size()); 
43     }
44 
45 }

下面贴出部分运行结果:

线程不安全:

当然线程安全下面会出现:(运行结果都是10000)

9982
9988
9990
9979
9981
9977
9965
9971
9848
9846
9989
9993
9991
9678
9995
9992
9998
9994
9996
9997
9999
result=============
10000

对于增长,我们可以看看ArrayList的源码:

ArrayList:grow()

 1 /**
 2      * Increases the capacity to ensure that it can hold at least the
 3      * number of elements specified by the minimum capacity argument.
 4      *
 5      * @param minCapacity the desired minimum capacity
 6      */
 7     private void grow(int minCapacity) {
 8         // overflow-conscious code
 9         int oldCapacity = elementData.length;
10         int newCapacity = oldCapacity + (oldCapacity >> 1);
11         if (newCapacity - minCapacity < 0)
12             newCapacity = minCapacity;
13         if (newCapacity - MAX_ARRAY_SIZE > 0)
14             newCapacity = hugeCapacity(minCapacity);
15         // minCapacity is usually close to size, so this is a win:
16         elementData = Arrays.copyOf(elementData, newCapacity);
17     }

Vector:grow()

 1 private void grow(int minCapacity) {
 2         // overflow-conscious code
 3         int oldCapacity = elementData.length;
 4         int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
 5                                          capacityIncrement : oldCapacity);
 6         if (newCapacity - minCapacity < 0)
 7             newCapacity = minCapacity;
 8         if (newCapacity - MAX_ARRAY_SIZE > 0)
 9             newCapacity = hugeCapacity(minCapacity);
10         elementData = Arrays.copyOf(elementData, newCapacity);
11     }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏java闲聊

JDK1.8 ArrayList 源码解析

当运行 ArrayList<Integer> list = new ArrayList<>() ; ,因为它没有指定初始容量,所以它调用的是它的无参构造

14520
来自专栏猿人谷

判断二叉树是不是平衡

平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超...

21560
来自专栏Android相关

平衡二叉树

它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

60030
来自专栏Bingo的深度学习杂货店

Q110 Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a hei...

29250
来自专栏Android干货

Python枚举类

Enum可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较。

15220
来自专栏java学习

让你更好的理解什么是二叉树?

二叉树 6.2.1 二叉树的概念 二叉树(Binary Tree)是结点的有限集合,这个集合或者为空,或者是由一个根结点和两颗互不相交的分别称为左子树和右子树的...

800110
来自专栏章鱼的慢慢技术路

笔试常考题型之二叉树的遍历

19240
来自专栏WindCoder

二叉树的动态链式存储实现—C语言

34410
来自专栏mathor

二叉树的下一个结点&二叉树的上一个结点

 最笨的方法就是一直网上回溯,直到找到了头结点,然后从头结点开始重新中序遍历一次树,然后得到答案  还有一种比较巧妙的方法,先判断当前结点有没有右子树,如果有...

6720
来自专栏琯琯博客

PHP二叉树(一):平衡二叉树(AVL)

平衡二叉树 <?php /** * description: 平衡二叉树 */ //结点 class Node { public $key; ...

39190

扫码关注云+社区

领取腾讯云代金券