数组列表和向量这两种数据结构之间有什么区别,你应该在哪里使用它们?
当前回答
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更新,速度快20-30%。
如果你不需要在Vector中显式地使用一些东西,请使用ArrayList
在向量和数组列表之间有两个主要的区别。
默认情况下,Vector是同步的,而ArrayList不是。 注意:通过将ArrayList对象传递给Collections.synchronizedList()方法,可以使ArrayList也同步化。 Synchronized意味着:它可以与多个线程一起使用而没有任何副作用。 当空间不足以容纳新元素时,数组列表的大小将增长到先前大小的50%,而当没有空间容纳新传入元素时,as Vector将增长到先前大小的100%。
除此之外,就编程工作而言,它们之间还有一些实际的区别:
To get the element at a particular location from Vector we use elementAt(int index) function. 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. 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. In place of this ArrayList has add(int index, object) method which is easy to use and remember. Like this they have more programmer friendly and easy to use function names in ArrayList.
什么时候用哪个?
尽量避免完全使用vector。数组列表能做向量能做的所有事情。默认情况下,数组列表是不同步的。如果您愿意,可以使用Collections util类在需要时同步它。 ArrayList函数名易于记忆和使用。
注意:即使数组列表增长了100%,也可以通过ensurecapacity()方法避免这种情况,以确保在初始阶段本身分配了足够的内存。
希望能有所帮助。
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
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.
推荐文章
- 到底是什么导致了堆栈溢出错误?
- 为什么Android工作室说“等待调试器”如果我不调试?
- Java:路径vs文件
- ExecutorService,如何等待所有任务完成
- 连接两个向量的最佳方法是什么?
- Maven依赖Servlet 3.0 API?
- 如何在IntelliJ IDEA中添加目录到应用程序运行概要文件中的类路径?
- getter和setter是糟糕的设计吗?相互矛盾的建议
- Android room persistent: AppDatabase_Impl不存在
- Java的String[]在Kotlin中等价于什么?
- Intellij IDEA上的System.out.println()快捷方式
- 使用Spring RestTemplate获取JSON对象列表
- Spring JPA选择特定的列
- URLEncoder不能翻译空格字符
- Java中的super()