数组列表和向量这两种数据结构之间有什么区别,你应该在哪里使用它们?
当前回答
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.
其他回答
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.
差异
向量是同步的,数组列表 不是。 数据增长方法
如果对vector没有特殊要求,请使用数组列表。
同步
如果多个线程同时访问一个数组列表,那么我们必须从外部同步修改列表结构或简单修改元素的代码块。结构修改是指从列表中添加或删除元素。设置现有元素的值不是结构修改。
集合。synchronizedList通常在创建列表时使用,以避免对列表的任何意外非同步访问。
数据增长
在内部,ArrayList和Vector都使用Array保存它们的内容。当一个元素被插入到一个数组列表或Vector对象中时,如果它的空间不足,对象将需要扩展它的内部数组。Vector默认将其数组大小增加一倍,而ArrayList则将其数组大小增加50%。
基本上ArrayList和Vector都使用内部对象数组。
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.
其他一些区别是:
ArrayList和Vector都实现了List接口,并维护了插入顺序。但是在ArrayList和Vector类之间有很多不同…
ArrayList。
数组列表没有同步。 如果元素数量超过其容量,ArrayList将增加当前数组大小的50%。 ArrayList不是一个遗留类,它是在JDK 1.2中引入的。 数组列表速度很快,因为它是非同步的。 ArrayList使用Iterator接口遍历元素。
向量,
矢量是同步的。 Vector增量100%表示如果元素总数超过其容量,则数组大小增加一倍。 Vector是一个遗留类。 Vector很慢,因为它是同步的,即在多线程环境中,它将保持其他线程处于可运行或不可运行状态,直到当前线程释放对象的锁。 Vector使用枚举接口遍历元素。但它也可以使用Iterator。
参见:https://www.javatpoint.com/difference-between-arraylist-and-vector
ArrayList更新,速度快20-30%。
如果你不需要在Vector中显式地使用一些东西,请使用ArrayList
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?