ArrayList和Vector有什么区别?

本文翻译自:What are the differences between ArrayList and Vector?

两个数据结构ArrayListVector之间有什么区别,你应该在哪里使用它们?


#1楼

参考:https://stackoom.com/question/CWs4/ArrayList和Vector有什么区别


#2楼

Vector is a broken class that is not threadsafe, despite it being "synchronized" and is only used by students and other inexperienced programmers. Vector是一个破碎的类,虽然它是“同步的”,但它不是线程安全的, 只能由学生和其他没有经验的程序员使用。

ArrayList is the go-to List implementation used by professionals and experienced programmers. ArrayList是专业人士和有经验的程序员使用的ArrayList列表实现。

Professionals wanting a threadsafe List implementation use a CopyOnWriteArrayList . 想要线程安全List实现的专业人员使用CopyOnWriteArrayList


#3楼

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: ArrayList类扩展AbstractList并实现List接口和RandomAccess(标记接口)。 ArrayList supports dynamic arrays that can grow as needed. ArrayList支持可根据需要增长的动态数组。 It gives us first iteration over elements. 它为我们提供了对元素的第一次迭代。 ArrayList uses internal Object Array; ArrayList使用内部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. 它们的默认初始大小为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: Vector类似于ArrayList,但差异在于,它是同步的,其默认初始大小为10,当大小超过其大小时,增加到原始大小的两倍,这意味着新大小将为20. Vector是唯一的类除了ArrayList以实现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(int initialCapacity,int capacityIncrement) capacityIncrement是向量溢出时容量增加的量,因此它可以更好地控制加载因子。

Some other differences are: 其他一些差异是: 在此输入图像描述


#4楼

There are 2 major differentiation's between Vector and ArrayList. Vector和ArrayList之间有两个主要的区别。

  1. Vector is synchronized by default, and ArrayList is not. 默认情况下,Vector是同步的,而ArrayList则不是。 Note : you can make ArrayList also synchronized by passing arraylist object to Collections.synchronizedList() method. 注意:您可以通过将arraylist对象传递给Collections.synchronizedList()方法来使ArrayList同步。 Synchronized means : it can be used with multiple threads with out any side effect. 同步意味着:它可以与多个线程一起使用,没有任何副作用。

  2. 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. 当空间不足以容纳新元素时,ArrayLists增加前一个大小的50%,而当没有新传入元素的空间时,Vector将增加前一个大小的100%。

Other than this, there are some practical differences between them, in terms of programming effort: 除此之外,在编程工作方面,它们之间存在一些实际差异:

  1. To get the element at a particular location from Vector we use elementAt (int index) function. 要从Vector获取元素在特定位置,我们使用elementAt (int index)函数。 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. 在ArrayList中,我们得到 (int index)非常容易记住和使用。
  2. 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. 类似于用Vector中的新元素替换现有元素,我们使用setElementAt ()方法,这又是非常冗长的,可能会激怒程序员重复使用。 In place of this ArrayList has add (int index, object) method which is easy to use and remember. 代替这个ArrayList有add (int index,object)方法,它易于使用和记忆。 Like this they have more programmer friendly and easy to use function names in ArrayList. 像这样,他们在ArrayList中拥有更多程序员友好且易于使用的函数名称。

When to use which one? 什么时候使用哪一个?

  1. Try to avoid using Vectors completely. 尽量避免完全使用向量。 ArrayLists can do everything what a Vector can do. ArrayLists可以完成Vector可以执行的所有操作。 More over ArrayLists are by default not synchronized. 默认情况下,ArrayLists的更多内容不会同步。 If you want, you can synchronize it when ever you need by using Collections util class. 如果需要,可以使用Collections util类在需要时同步它。
  2. ArrayList has easy to remember and use function names. 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. 希望能帮助到你。


#5楼

Differences 差异

  • Vectors are synchronized, ArrayLists are not. 向量是同步的,ArrayLists不是。
  • Data Growth Methods 数据增长方法

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. 如果多个线程同时访问ArrayList,那么我们必须从外部同步代码块,从而在结构上修改列表或简单地修改元素。 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 is normally used at the time of creation of the list to avoid any accidental unsynchronized access to the list. Collections.synchronizedList通常在创建列表时使用,以避免对列表进行任何意外的不同步访问。

Reference 参考

Data growth 数据增长

Internally, both the ArrayList and Vector hold onto their contents using an Array. 在内部,ArrayList和Vector都使用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. 当元素插入到ArrayList或Vector中时,如果对象耗尽空间,则该对象将需要扩展其内部数组。 A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Vector默认将其数组的大小加倍,而ArrayList将其数组大小增加50%。

Reference 参考


#6楼

ArrayList is newer and 20-30% faster. ArrayList更新,速度提高20-30%。

If you don't need something explitly apparent in Vector , use ArrayList 如果你不需要在Vector显而易见的东西,请使用ArrayList

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值