现在的位置: 首页 > 综合 > 正文

Difference between Vector and ArrayList in java?

2013年09月01日 ⁄ 综合 ⁄ 共 2746字 ⁄ 字号 评论关闭

面试的时候也总是问的一个问题,说一下Vector与ArrayList的区别?

这个问题以前没太注意过,所以查询资料的时候,看到网上对这个问题的解释几乎是相同(应该都是copy的偷笑),千篇一律且比较啰嗦。这次找到一个老外的解释,比较简单形象,转载一下,很简单的。印度阿三写的,连接地址是

http://javapapers.com/core-java/java-collection/difference-between-vector-and-arraylist-in-java/

有兴趣的可以自己去看看。

java.util.Vector came along with the first version of java development kit (JDK). java.util.ArrayList was introduced in java version1.2, as part of java collections framework. As per java API, in Java 2 platform v1.2,vector has been retrofitted to implement
List and vector also became a part of java collection framework.

All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized. All the new implementations of java collection framework is not synchronized.

Vector and ArrayList both uses Array internally as data structure. They are dynamically resizable. Difference is in the way they are internally resized. By default, Vector doubles the size of its array when its size is increased. But, ArrayList increases
by half of its size when its size is increased.

Therefore as per Java API the only main difference is, Vector’s methods are synchronized and ArrayList’s methods are not synchronized.

Vector or ArrayList? Which is better to use in java?

In general, executing a ‘synchronized’ method results in costlier performance than a unsynchronized method. Keeping the difference in mind, using Vector will incur a performance hit than the ArrayList. But, when there is a certain need for thread-safe operation
Vector needs to be used.

Is there an alternate available in java for Vector?
ArrayList can be synchronized using the java collections framework utility class and then ArrayList itself can be used in place of Vector.

When there is no need for synchronized operation and you still look for better performance ‘Array’ can be used instead of ArrayList. But the development is tedious, since it doesn’t provide user friendly methods.

When you use Vector or ArrayList, always initialize to the largest capacity that the java program will need. Since incrementing the size is a costlier operation.

 

翻译如下:

 

          java.util.Vector从jdk1.0开始就已经存在了,java.util.ArrayList是从jdk1.2引入的,并且是collection框架的一部分。根据java API,从jdk1.2开始Vector已经被重新定义,它实现了List接口并且Vector也成了collection框架的一部分。

         Vector所有的方法都是synchronized(注:是所有的public的非重载方法).但是,ArrayList所有的方法却不是synchronized所有java collection框架的新的实现方法也都不是synchronized的.

         Vector和ArrayList内部都是使用数组做数据结构.他们都是动态改变大小.不同的是他们内部改变大小的方式.默认情况下,Vector大小增长是正常数组大小的一倍,但是,ArrayList大小正常是正常数组的一半.

 因此根据java API,Vector和ArrayList主要的不同是,Vector的方法是synchronized,ArrayList的方法不是synchronized.

        在java中Vector和ArrayList在使用上哪个更好?


         一般而言,使用synchronized的方法和不用synchronized的方法相比,会导致消耗很高的性能.Keeping the difference in mind(不会翻译),使用Vector会比使用ArrayList消耗更高的性能.但,当需要某种线程安全操作时,Vector是必要的.

         在java中有没有可以替代Vector的的东西?

         ArrayList可以使用java集合框架的工具类实现synchronized,然后ArrayList就可以替代Vector了.

         当不需要synchronized操作时,你仍然可以寻找替代ArrayList的更好实现,但这种开发是无聊的,因为它不能提供给使用者友好的方法.

         当你使用Vector或是ArrayList时,他们总是被初始化为最大容量,以方便java程序的使用,因此增长其大小是一项昂贵的操作.

抱歉!评论已关闭.