In Java, static arrays and dynamic arrays are all static arrays. In fact, in many cases, static arrays cannot meet the actual needs of programming. For example
Program When we dynamically add data to the array during running, the size of our static array is fixed. Obviously, we cannot add data. to dynamically add data, we must use a dynamic array, the element types in the dynamic array are the same, but this type already uses a very large type to include-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
In terms of the internal implementation mechanism, both 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: Synchronization
Arraylist and inclulist 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, retrieving an object from the specified position (using index) or inserting or deleting an object at the end of the set takes the same time, 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. If you want to insert or delete other operations at the specified position, you are advised to select the recommended list.
Arraylist and vector use arrays to store data. The number of elements in the array is greater than that in the actual storage to add and insert elements. Both allow direct serial number index elements, however, data insertion is designed to move array elements and other memory operations. Therefore, index data is inserted slowly. Because vector uses the Synchronized Method (thread-safe), its performance is inferior to that of arraylist, the sort list 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, so the insertion speed is faster.