Compare the differences between vector and arraylist

Source: Internet
Author: User

VectorOr arraylistThis is a problem.

-- Compare VectorAnd arraylistDifferent


 

Vector OrArraylist-which one is better? Why?

The answer to this question cannot be generalized. Sometimes it is better to use a vector. Sometimes it is an arraylist. Sometimes these two are not the best choices. Don't expect a simple affirmative answer, because it depends on what you use them. There are four factors to consider:

L API

L synchronous processing

L data growth

L usage mode

We will discuss these four aspects one by one.

API

In 《Java programming language(Addison-Wesley, June 2000): vector is similar to arraylist .. From the API perspective, these two classes are very similar. But there are still some major differences between them.

Synchronization

The vector is synchronized. Some methods in this class ensure that the object in the vector isThread Security. The arraylist is asynchronous, so the objects in the arraylist are notThread Security. Because the synchronization requirements will affect the execution efficiency, it is a good choice to use arraylist if you do not need a thread-safe set, this avoids unnecessary performance overhead due to synchronization.

Data Growth

In terms of the internal implementation mechanism, both arraylist and vector use arrays to control objects in the set. When you add elements to these two types, if the number of elements exceeds the current length of the internal array, both of them need to extend the length of the internal array, by default, vector automatically doubles the length of the original array, and arraylist is 50% of the original length. Therefore, the space occupied by this set is always larger than what you actually need. Therefore, if you want to save a large amount of data in the collection, using vector has some advantages, because you can avoid unnecessary resource overhead by setting the initialization size of the collection.

Usage mode

In arraylist and vector, it takes the same time to search for data from a specified position (through an index) or add or remove an element at the end of the set, this time is represented by O (1. However, if an element is added or removed from another position in the Set, the time consumed will grow linearly: O (n-I), where N represents the number of elements in the set, I indicates the index location where the element is added or removed. Why? It is assumed that all elements after the I and I elements in the collection must be displaced during the above operations. What does all this mean?

This means that you can only search for elements at a specific position or add or remove elements at the end of the set. You can use vector or arraylist. For other operations, you 'd better select another set operation class. For example, the time spent by the linklist collection class to add or remove any element in the set is the same-O (1 ), however, it is slow to use an element in the index-O (I), where I is the index position. it is also easy to use arraylist, because you can simply use indexes instead of creating iterator objects. Linklist also creates an object for each inserted element, and you need to understand that it also brings additional overhead.

Finally 《Practical JavaIn his book, Peter Haggar suggests using a simple array instead of vector or arraylist. This is especially true for programs with high execution efficiency requirements. Array is used to avoid synchronization, additional method calls, and unnecessary Space reallocation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.