Vector or arraylist this is a problem-comparing the differences between vector and arraylist

Source: Internet
Author: User

Vector or arraylist-which one is better? Why?


The answer to this question cannot be generalized. Sometimes it is better to use vector. Sometimes it is an arraylist. Sometimes neither of them is

The best choice. Don't expect a simple affirmative answer, because it depends on what you use them. Four of the following are to be considered:

Factors:

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) compiled by Ken Arnold

Sample Description: vector is similar to arraylist .. From the API perspective, these two classes are very similar to [B. But they also

There are some major differences.

Synchronization

The vector is synchronized. Some methods in this class ensure that the objects in the vector are thread-safe. While arraylist is asynchronous

So the objects in the arraylist are not thread-safe. Because the synchronization requirements will affect the execution efficiency, if you do not

The use of arraylist is a good choice for thread-safe sets, which can avoid unnecessary synchronization.

Performance overhead.

Data Growth

In terms of the internal implementation mechanism, both arraylist and vector use arrays to control objects in the set. When you send

When adding elements, if the number of elements exceeds the current length of the internal array, they must expand 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.

The space occupied by the combination is always larger than what you actually need. So if you want to save a large amount of data in the collection, use the Vector

Because you can set the initialization size of the set to avoid unnecessary resource overhead.

Usage mode

In arraylist and vector, you can search for data from a specified position (through an index) or add or remove one at the end of the set.

Elements take the same time, which is represented by O (1. However, if it is added or removed elsewhere in the set

The time it takes for an element to grow linearly: O (n-I), where N represents the number of elements in the Set, And I represents the addition or removal of elements.

The index location. Why? Assume that all elements after the I and I are in the set during the above operations

You must perform the displacement operation. What does all this mean?

This means that you only search for elements at a specific position or add or remove elements at the end of the set, then use the vector or

All arraylist functions are supported. For other operations, you 'd better select another set operation class. For example, the linklist collection class is being added.

Or the time it takes to remove an element from any position in the set is the same-O (1), but it is slower to use to index an element.

-O (I), where I is the index location. It is also easy to use arraylist, because you can simply use indexes instead of creating

Iterator object operations. Linklist also creates an object for each inserted element.

Sales.

Finally, in practical Java, Peter Haggar recommends a simple array instead of vector.

Or arraylist. This is especially true for programs with high execution efficiency requirements. Because array is used to avoid synchronization and additional

Method call and unnecessary Space reallocation operations.

 

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.