`
cywhoyi
  • 浏览: 421253 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Vector or ArrayList

 
阅读更多

Sometimes Vector is better; sometimes ArrayList is better; sometimes you don't want to use either. I hope you weren't looking for an easy answer because the answer depends upon what you are doing. There are four factors to consider:

  • API
  • Synchronization
  • Data growth
  • Usage patterns



Let's explore each in turn.

API

In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vectoras an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.

Synchronization

Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe. With that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?

Data growth

Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or aVector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while theArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vectordoes possess a slight advantage since you can set the increment value.

Usage patterns

Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and iis the index of the element added or removed. These operations are more expensive because you have to shift all elements at index iand higher over by one element. So what does this all mean?

It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1).However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.

Finally, in "PRAXIS 41" from Practical Java (Addison-Wesley, Feb. 2000) Peter Haggar suggests that you use a plain old array in place of either Vector or ArrayList -- especially for performance-critical code. By using an array you can avoid synchronization, extra method calls, and suboptimal resizing. You just pay the cost of extra development time.

分享到:
评论

相关推荐

    Java基础–为什么ArrayList,Vector等都不支持循环中remove?

    为什么ArrayList,Vector等都不支持循环中remove1 Vector 直接删除2 Vector 遍历元素2.1 for循环遍历2.2 迭代器循环2.3 任意方向遍历2.4 Vector的foreach3. Vector迭代器删除4. Vector不使用迭代器删除元素5. Vector...

    你知道synchronizedList和Vector底层原理实现和区别吗?其实开始我也不知道!(超详细源码分析)

    因为Vector和ArrayList除了数组扩容有点差别,还有加锁使Vector迈进了线程安全的行列外,底层实现大约是没有太大区别的!基本一致!线程安全问题更是另当别论了!继续往下看就OK! 扩容的区别: 从内部实现机制来讲...

    List实现类中的ArrayList、Vector、LinkedList

    可以把接口的好处5体现出来,如果ArrayList()不满足需求,直接更换就可以。 接口的好处: 1.程序的耦合度降低 2.更自然的使用多态 3.设计与实现完全分离 4.更容易搭建程序框架 5.更容易更换具体实现 ArrayList: ...

    Java中Vector类和Stack类的学习

    - **扩容策略**:`ArrayList`在扩展时,默认会将容量增加为原容量的1.5倍,而`Vector`如果设置了`capacityIncrement`,则会按照该值扩展,否则也扩展为原来的两倍。 **2. Stack类** `Stack`类继承自`Vector`,它...

    vector,map,list,queue的区别详细解析

    vector (连续的空间存储,可以使用[]操作符)快速的访问随机的元素,快速的在末尾插入元素,但是在序列中间岁间的插入,删除元素要慢,而且如果一开始分配的空间不够的话,有一个重新分配更大空间,然后拷贝的性能...

    J2EE高性能编程

    - **优先使用Array然后考虑Vector和ArrayList** 在J2EE高性能编程中,对于数据结构的选择至关重要。数组(Array)作为一种原生的数据结构,其性能通常高于Vector和ArrayList。这是因为数组在内存中是连续存储的,这...

    幸运数-vector

    需要注意的是,`Vector`类在Java中是线程安全的,但它的操作通常比`ArrayList`慢,因此在性能要求较高的情况下,可能需要考虑使用更高效的容器,如`ArrayList`。同时,对于大范围的幸运数搜索,可能需要优化筛选算法...

    java英文笔试

    - **Performance**: `ArrayList` is faster than `Vector` because it does not require the overhead of synchronization. If thread safety is not a concern, `ArrayList` is usually the preferred choice for ...

    2010年中兴软创新试题

    - ArrayList 和 Vector 的索引访问快速(O(1)),但在中间插入和删除元素时,需要移动大量元素,效率低(O(n-i))。 - LinkedList 使用链表结构,插入和删除元素速度快(O(1)),但索引访问速度慢(O(i))。 5. ...

    java应届毕业生笔试题.docx

    - **B**: 错误,`ArrayList`通常比`Vector`快,因为`Vector`是线程安全的,而`ArrayList`不是。 - **C**: 正确,`ArrayList`没有同步保护,而`Vector`提供了同步保护。 - **D**: 错误,`ArrayList`和`Vector`都是...

    面试总结2019年9月3日.doc

    22. **Vector和ArrayList的区别**:除了线程安全,ArrayList在容量不足时会扩大容量的一半,而Vector扩大一倍,可能导致性能问题。 23. **生产者消费者模型**:常见实现包括wait/notify、阻塞队列BlockingQueue、...

    开发多媒体播放器

    /* 将所有文件添加ArrayList中 */ for (int i = 0; i ; i++) { if (files[i].isDirectory()) { items.add(files[i].getName()); paths.add(files[i].getPath()); sizes.add(""); } ...

    Java企业级开发综合技能知识总结

    48. **说出ArrayList, Vector, LinkedList的存储性能和特性HashMap和Hashtable的区别** - `ArrayList`: 动态数组,随机访问快,插入慢。 - `Vector`: 线程安全版本的`ArrayList`。 - `LinkedList`: 双向链表,...

    2010年蓝点决赛题+编程练习题(java)

    // Print solution or check if it's correct } if (Character.isDigit(chars[index])) { digits[index] = chars[index] - '0'; } else { for (int i = 1; i ; i++) { if (!used[i]) { digits[index] = i; ...

    最全的java面试题

    // swap arr[i+1] and arr[high] (or pivot) int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } } ``` ##### 9. Overload和Override的区别。Overloaded的方法是否可以...

    整合了所有java经典面试题

    - 类:`String`, `ArrayList`, `HashMap`, `Thread`, `File` - 包:`java.lang`, `java.util`, `java.io`, `java.net`, `javax.servlet` - 接口:`Comparable`, `Comparator`, `Runnable`, `Callable`, `List` ...

    java常见笔试题目总结

    // swap arr[i+1] and arr[high] (or pivot) int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } ``` #### 9. Overload和Override的区别 - **Overload**:在同一类中,方法...

Global site tag (gtag.js) - Google Analytics