数组列表和向量这两种数据结构之间有什么区别,你应该在哪里使用它们?
当前回答
差异
向量是同步的,数组列表 不是。 数据增长方法
如果对vector没有特殊要求,请使用数组列表。
同步
如果多个线程同时访问一个数组列表,那么我们必须从外部同步修改列表结构或简单修改元素的代码块。结构修改是指从列表中添加或删除元素。设置现有元素的值不是结构修改。
集合。synchronizedList通常在创建列表时使用,以避免对列表的任何意外非同步访问。
数据增长
在内部,ArrayList和Vector都使用Array保存它们的内容。当一个元素被插入到一个数组列表或Vector对象中时,如果它的空间不足,对象将需要扩展它的内部数组。Vector默认将其数组大小增加一倍,而ArrayList则将其数组大小增加50%。
其他回答
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和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。
ArrayList更新,速度快20-30%。
如果你不需要在Vector中显式地使用一些东西,请使用ArrayList
差异
向量是同步的,数组列表 不是。 数据增长方法
如果对vector没有特殊要求,请使用数组列表。
同步
如果多个线程同时访问一个数组列表,那么我们必须从外部同步修改列表结构或简单修改元素的代码块。结构修改是指从列表中添加或删除元素。设置现有元素的值不是结构修改。
集合。synchronizedList通常在创建列表时使用,以避免对列表的任何意外非同步访问。
数据增长
在内部,ArrayList和Vector都使用Array保存它们的内容。当一个元素被插入到一个数组列表或Vector对象中时,如果它的空间不足,对象将需要扩展它的内部数组。Vector默认将其数组大小增加一倍,而ArrayList则将其数组大小增加50%。
推荐文章
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列