数组列表和向量这两种数据结构之间有什么区别,你应该在哪里使用它们?
当前回答
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
其他回答
Vector是一个破碎的类,它不是线程安全的,尽管它是“同步的”,而且只被学生和其他没有经验的程序员使用。
ArrayList是专业人员和有经验的程序员使用的List实现。
专业人士想要一个线程安全的列表实现使用CopyOnWriteArrayList。
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.
ArrayList更新,速度快20-30%。
如果你不需要在Vector中显式地使用一些东西,请使用ArrayList
基本上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
推荐文章
- JavaFX应用程序图标
- Java:强/软/弱/幻影引用的区别
- 在序列化和反序列化期间JSON属性的不同名称
- 获取Android设备名称
- Gradle代理配置
- 如何获得具有已知资源名称的资源id ?
- 在Android上将字符串转换为整数
- 为什么“System.out。”println“工作在Android?
- 在Java中什么时候使用可变参数?
- Mockito的argumentCaptor的例子
- 我如何告诉Spring Boot哪个主类用于可执行jar?
- 如何将Java8流的元素添加到现有的列表中
- 在Java 8中是否可以转换流?
- 不区分大小写的字符串作为HashMap键
- 什么是maven中的“pom”打包?