Static and Dynamic Arrays in JAVA

Source: Internet
Author: User

The arrays we learned earlier are static arrays. In fact, in many cases, static arrays cannot meet the actual needs of programming, for example, when I need to dynamically add data to the array while the program is running, the size of our static array is fixed and it is obviously impossible to add data, to dynamically add data, a dynamic array must be used. The element types in the dynamic array are the same. However, this type already uses a very large type to include the-Object type. The Object class is the top-level superclass in the JAVA. LANG package. All types can be compatible with the Object type. Therefore, we can add any Object type to an array of the Object type. collections of the Object type can be added, including ArrayList, Vector, and struct list, they store data in an array and belong to the Collection class. Their features are as follows: feature 1: capacity expansion from the internal implementation mechanism, ArrayList and Vector are stored in the Objec array format. 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. Feature 2: Synchronous ArrayList and synchronized list are not synchronized, while Vestor is. Therefore, if thread security is required, ArrayList or javaslist can be used to save costs for synchronization. But in the case of multithreading, sometimes we have to use Vector. Of course, you can also package ArrayList and history list in some ways to synchronize them, but the efficiency may be reduced. Feature 3: data operation efficiency in ArrayList and Vector, it is the same to retrieve an object from a specified position (using index) or to insert or delete an object at the end of the set, can be expressed as 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? When performing the preceding operations, all the elements after the I and I elements in the set must perform the (n-I) object displacement operation. In the sorted list, the time spent on inserting or deleting elements at any position in the set is the same-O (1), but it is slow when indexing an element, is O (I), where I is the index location. Therefore, if you only search for elements at a specific position or add or remove elements only at the end of the set, you can use Vector or ArrayList. For other insert and delete operations at the specified position, it is best to select the arrays list ArrayList and Vector to store data. The number of elements in this array is greater than that in actual storage for adding and inserting elements, both allow direct serial number index elements, but the inserted data must be designed for memory operations such as the movement of array elements, so the index data is too slow to be inserted. Because the Vector uses the synchronized Method (thread-safe) therefore, the performance is worse than that of ArrayList. mongolist uses a two-way linked list for storage. Data indexed by serial number needs to be traversed forward or backward. However, when inserting data, you only need to record the items before and after this item, therefore, the insert speed is faster.

Related Article

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.