Analysis of Java static and dynamic array features

Source: Internet
Author: User
Tags requires

Usually we are exposed to most of the static array, in fact, in many cases, the static array does not meet the actual needs of our programming, for example, I need to be in the process of running the program to add data dynamically to the array, when our static array size is fixed, obviously can not add data, To add data dynamically, you must use a dynamic array, and the various element types in the dynamic array are consistent, although this type is already a very large type to enclose the-object type. The object class is the top-level superclass in a Java.lang package. All types can be compatible with object types, so we can add any object type to an array of type object that can add a collection of type object with ArrayList, vectors, and LinkedList. They are stored in the form of an array, which belongs to the collection class, and the following are their characteristics:

1, synchronization of

Arraylist,linkedlist is not synchronized, and vestor yes. So if thread safety is required, you can use ArrayList or LinkedList to save the cost of synchronization. But in the case of multithreading, vectors are sometimes used. Of course, there are some ways to package arraylist,linkedlist, so that they can also achieve synchronization, but the efficiency may be reduced.

2. Data operation efficiency

In ArrayList and vectors, retrieving an object from a specified position (with index) or inserting or deleting an object at the end of the collection is the same, which can be expressed as O (1). However, if an element is added or removed elsewhere in the collection, the time it takes to grow linearly increases: O (n-i), where n represents the number of elements in the collection, and I represents the index position of the element's increase or removal. Why would that be? It is assumed that all elements after the first and first elements of the collection in the preceding operation perform the displacement operation of the (N-i) object. In LinkedList, the time spent inserting and deleting elements anywhere in the collection is the same as-O (1), but it is slower when indexing an element, which is O (i), where I is the location of the index.

3, capacity expansion of

From the internal implementation mechanism, both ArrayList and vectors are stored using the OBJEC array form. When you add elements to both types, if the number of elements exceeds the current length of the internal array they all need to extend the length of the internal array, the vector automatically increases the original one times the length of the array by default, ArrayList is the original 50%, So at the end of the collection you get a lot more space than you really need. So if you want to save a lot of data in a collection, there are some advantages to using vectors because you can avoid unnecessary resource overhead by setting the initialization size of the collection.

So, if you're just looking for elements in a particular location or just adding and removing elements at the end of a collection, you can use either vector or ArrayList. If you are inserting, deleting operations at other specified locations, it is best to choose linkedlistarraylist and vectors to store the data in an array, which is larger than the actual stored data in order to add and insert elements, allowing for direct ordinal index elements. But inserting the data to design to the array element movement and so on memory operation, therefore the index data fast inserts the data slow, the vector because uses the Synchronized method (thread security) to be worse than ArrayList, LinkedList uses the bidirectional linked list to realize the storage, Indexing data by ordinal requires a forward or backward traversal, but inserting the data requires only a log of the items before and after, so the inserts are several times faster.

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.