Java ArrayList 和 Vector 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2986296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between ArrayList and Vector?
提问by KushalP
What are the differences between the two data structures ArrayListand Vector, and where should you use each of them?
ArrayList和Vector两种数据结构有什么区别,你应该在哪里使用它们?
采纳答案by Sev
Differences
差异
- Vectors are synchronized, ArrayLists are not.
- Data Growth Methods
- 向量是同步的,ArrayLists 不是。
- 数据增长方法
Use ArrayLists if there is no specific requirement to use Vectors.
如果没有使用 Vectors 的特定要求,请使用 ArrayLists。
Synchronization
同步
If multiple threads access an ArrayList concurrently then we must externally synchronize the block of code which modifies the list either structurally or simply modifies an element. Structural modification means addition or deletion of element(s) from the list. Setting the value of an existing element is not a structural modification.
如果多个线程同时访问一个 ArrayList,那么我们必须在外部同步代码块,该代码块在结构上修改列表或简单地修改一个元素。结构修改是指从列表中添加或删除元素。设置现有元素的值不是结构修改。
Collections.synchronizedList
is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.
Collections.synchronizedList
通常在创建列表时使用,以避免对列表的任何意外不同步访问。
Data growth
数据增长
Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
在内部,ArrayList 和 Vector 都使用 Array 来保存它们的内容。当一个元素被插入到 ArrayList 或 Vector 中时,如果空间不足,对象将需要扩展其内部数组。Vector 默认将其数组的大小加倍,而 ArrayList 将其数组大小增加 50%。
回答by Oli
ArrayList
is newer and 20-30% faster.
ArrayList
更新,速度提高 20-30%。
If you don't need something explitly apparent in Vector
, use ArrayList
如果您不需要在 中明确显示的内容Vector
,请使用ArrayList
回答by Antal Spector-Zabusky
As the documentation says, a Vector
and an ArrayList
are almost equivalent. The difference is that access to a Vector
is synchronized, whereas access to an ArrayList
is not. What this means is that only one thread can call methods on a Vector
at a time, and there's a slight overhead in acquiring the lock; if you use an ArrayList
, this isn't the case. Generally, you'll want to use an ArrayList
; in the single-threaded case it's a better choice, and in the multi-threaded case, you get better control over locking. Want to allow concurrent reads? Fine. Want to perform one synchronization for a batch of ten writes? Also fine. It does require a little more care on your end, but it's likely what you want. Also note that if you have an ArrayList, you can use the Collections.synchronizedList
function to create a synchronized list, thus getting you the equivalent of a Vector
.
正如文档所说, aVector
和 anArrayList
几乎是等价的。区别在于对 a 的访问Vector
是同步的,而对 an 的访问ArrayList
不是。这意味着一次只有一个线程可以调用 aVector
上的方法,并且获取锁的开销很小;如果您使用ArrayList
,则情况并非如此。通常,您需要使用ArrayList
; 在单线程情况下这是一个更好的选择,在多线程情况下,您可以更好地控制锁定。想要允许并发读取吗?美好的。想要为一批 10 次写入执行一次同步吗?也不错。它确实需要您多加小心,但这可能是您想要的。另请注意,如果您有一个 ArrayList,则可以使用Collections.synchronizedList
函数来创建一个同步列表,从而让你相当于一个Vector
.
回答by Bohemian
Vector
is a brokenclass that is notthreadsafe, despite it being "synchronized" and is onlyused by students and other inexperienced programmers.
Vector
是一个不是线程安全的损坏类,尽管它是“同步的”,并且仅供学生和其他没有经验的程序员使用。
ArrayList
is the go-to List implementation used by professionals and experienced programmers.
ArrayList
是专业人士和有经验的程序员使用的首选 List 实现。
Professionals wanting a threadsafe List implementation use a CopyOnWriteArrayList
.
想要线程安全 List 实现的专业人员使用CopyOnWriteArrayList
.
回答by subhashis
Basically both ArrayList and Vector both uses internal Object Array.
基本上 ArrayList 和 Vector 都使用内部对象数组。
ArrayList:The ArrayList class extends AbstractList and implements the List interface and RandomAccess (marker interface). ArrayList supports dynamic arrays that can grow as needed. It gives us first iteration over elements. ArrayList uses internal Object Array; they are created with an default initial size of 10. When this size is exceeded, the collection is automatically increases to half of the default size that is 15.
ArrayList:ArrayList 类扩展了 AbstractList 并实现了 List 接口和 RandomAccess(标记接口)。ArrayList 支持可以根据需要增长的动态数组。它为我们提供了对元素的第一次迭代。ArrayList 使用内部对象数组;它们创建时的默认初始大小为 10。当超过此大小时,集合会自动增加到默认大小 15 的一半。
Vector:Vector is similar to ArrayList but the differences are, it is synchronized and its default initial size is 10 and when the size exceeds its size increases to double of the original size that means the new size will be 20. Vector is the only class other than ArrayList to implement RandomAccess. Vector is having four constructors out of that one takes two parameters Vector(int initialCapacity, int capacityIncrement)capacityIncrement is the amount by which the capacity is increased when the vector overflows, so it have more control over the load factor.
Vector:Vector 与 ArrayList 类似,但不同之处在于,它是同步的,其默认初始大小为 10,当大小超过其大小时会增加到原始大小的两倍,这意味着新大小将为 20。 Vector 是唯一的类除了 ArrayList 来实现 RandomAccess。Vector 有四个构造函数,其中一个需要两个参数Vector(int initialCapacity, int capacityIncrement)capacityIncrement 是向量溢出时容量增加的量,因此它可以更好地控制负载因子。
Some other differences are:
其他一些区别是:
回答by user1923551
There are 2 major differentiation's between Vector and ArrayList.
Vector 和 ArrayList 之间有两个主要区别。
Vector is synchronized by default, and ArrayList is not. Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method. Synchronized means : it can be used with multiple threads with out any side effect.
ArrayLists grow by 50% of the previous size when space is not sufficient for new element, where as Vector will grow by 100% of the previous size when there is no space for new incoming element.
Vector 默认是同步的,而 ArrayList 不是。注意:您可以通过将 arraylist 对象传递给 Collections.synchronizedList() 方法来使 ArrayList 同步。同步意味着:它可以与多个线程一起使用而没有任何副作用。
当空间不足以容纳新元素时,ArrayLists 将增长前一个大小的 50%,而当没有新传入元素的空间时,Vector 将增长前一个大小的 100%。
Other than this, there are some practical differences between them, in terms of programming effort:
除此之外,就编程工作而言,它们之间存在一些实际差异:
- To get the element at a particular location from Vector we use elementAt(int index) function. This function name is very lengthy. In place of this in ArrayList we have get(int index) which is very easy to remember and to use.
- Similarly to replace an existing element with a new element in Vector we use setElementAt() method, which is again very lengthy and may irritate the programmer to use repeatedly. In place of this ArrayList has add(int index, object) method which is easy to use and remember. Like this they have more programmer friendly and easy to use function names in ArrayList.
- 要从 Vector 获取特定位置的元素,我们使用elementAt(int index) 函数。这个函数名很长。在 ArrayList 中,我们有get(int index) 来代替它,它很容易记住和使用。
- 同样,在 Vector 中用新元素替换现有元素我们使用setElementAt() 方法,该方法再次非常冗长,可能会刺激程序员重复使用。代替此 ArrayList 具有易于使用和记忆的add(int index, object) 方法。像这样,他们在 ArrayList 中有更多的程序员友好和易于使用的函数名称。
When to use which one?
什么时候用哪一种?
- Try to avoid using Vectors completely. ArrayLists can do everything what a Vector can do. More over ArrayLists are by default not synchronized. If you want, you can synchronize it when ever you need by using Collections util class.
- ArrayList has easy to remember and use function names.
- 尽量避免完全使用 Vectors。ArrayLists 可以做 Vector 可以做的所有事情。更多关于 ArrayLists 默认情况下是不同步的。如果需要,您可以在需要时使用 Collections util 类同步它。
- ArrayList 具有易于记忆和使用的函数名。
Note: even though arraylist grows by 100%, you can avoid this by ensurecapacity() method to make sure that you are allocating sufficient memory at the initial stages itself.
注意:即使 arraylist 增长了 100%,您也可以通过 ensurecapacity() 方法避免这种情况,以确保您在初始阶段分配足够的内存。
Hope it helps.
希望能帮助到你。
回答by roottraveller
ArrayList
and Vector
both implements List interface and maintains insertion order.But there are many differences between ArrayList
and Vector
classes...
ArrayList
并且Vector
都实现了List接口并维护了插入顺序。但是ArrayList
和Vector
类之间有很多区别......
数组列表-
ArrayList
is not synchronized.ArrayList
increments 50% of current array size if number of element exceeds from its capacity.ArrayList
is not a legacy class, it is introduced in JDK 1.2.ArrayList
is fast because it is non-synchronized.ArrayList
uses Iterator interface to traverse the elements.
ArrayList
不同步。ArrayList
如果元素数量超出其容量,则增加当前数组大小的 50%。ArrayList
不是遗留类,它是在 JDK 1.2 中引入的。ArrayList
速度很快,因为它是非同步的。ArrayList
使用 Iterator 接口来遍历元素。
矢量-
Vector
is synchronized.Vector
increments 100% means doubles the array size if total number of element exceeds than its capacity.Vector
is a legacy class.Vector
is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.Vector
uses Enumeration interface to traverse the elements. But it can use Iterator also.
Vector
是同步的。Vector
增量 100% 意味着如果元素总数超过其容量,则将数组大小加倍。Vector
是一个遗留类。Vector
慢是因为它是同步的,即在多线程环境中,它会将其他线程保持在可运行或不可运行状态,直到当前线程释放对象锁定。Vector
使用 Enumeration 接口遍历元素。但它也可以使用迭代器。
See Also : https://www.javatpoint.com/difference-between-arraylist-and-vector
另见:https: //www.javatpoint.com/difference-between-arraylist-and-vector