问题描述
ArrayList 和 Vector 这两种数据结构有什么区别,你应该在哪里使用它们?
What are the differences between the two data structures ArrayList and Vector, and where should you use each of them?
推荐答案
差异
- 向量是同步的,ArrayLists不是.
- 数据增长方法
如果没有使用 Vectors 的特定要求,请使用 ArrayLists.
Use ArrayLists if there is no specific requirement to use Vectors.
同步
如果多个线程同时访问一个 ArrayList,那么我们必须在外部同步修改列表的代码块,无论是在结构上还是简单地修改一个元素.结构修改是指从列表中添加或删除元素.设置现有元素的值不是结构修改.
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.
Collections.synchronizedList
通常在创建列表时使用,以避免对列表的任何意外不同步访问.
Collections.synchronizedList
is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list.
数据增长
在内部,ArrayList 和 Vector 都使用数组来保存它们的内容.当一个元素被插入到 ArrayList 或 Vector 中时,如果空间不足,对象将需要扩展其内部数组.Vector 默认将其数组的大小加倍,而 ArrayList 将其数组大小增加 50%.
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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!