Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the similarities and differences between ArrayList and Vector

2025-02-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the similarities and differences between ArrayList and Vector". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ArrayList VS Vector

The similarities: 1 > are all based on the Object array. 2 > both allow null elements to be added. The difference: 1 > ArrayList is not thread-safe, Vector is thread-safe (the method is Synchronized). 2 > after expansion, the capacity of ArrayList is 1.5 times that of before, and that of Vector is 2 times that of before, so ArrayList saves more space. Capacity: ArrayList initial capacity: 0 expansion: the first time (when calling the add method) is expanded to 10, and then each expansion is 1.5 times as much as before. Note: 1 > although the initial capacity of the ArrayList class defaults to 10 (private static final int DEFAULT_CAPACITY = 10;) 2 > in fact, after new ArrayList (), ArrayList's elementData (transient Object [] elementData) The length of) is 0 3 > when the add method is called for the first time, the length of elementData is extended to 10 4 > if capacity expansion is needed later Each time the capacity is expanded to 1.5 times the previous code: 1 > No-parameter construction method: this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA / / private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; 2 > expand 1.5x: newCapacity = oldCapacity + (oldCapacity > > 1); 3 > copy the array when expanding: elementData = Arrays.copyOf (elementData, newCapacity) Vector initial capacity: 10 expansion: each expansion is twice as much as before. Code: 1 > No parameter construction method: this (10); 2 > expand capacity 2 times: int newCapacity = oldCapacity + ((capacityIncrement > 0)? CapacityIncrement: oldCapacity); / / capacityIncrement defaults to 0 3 > copy the array when expanding: elementData = Arrays.copyOf (elementData, newCapacity)

ArrayList VS LinkedList

The same point: 1 > allows you to add null elements. Differences: 1 > for random access, get and set,ArrayList are faster than LinkedList. 2 > for insert and delete operations, LinkedList is faster.

Related collections:

1) Collections.synchronizedList (List list) for example: List syncArraylist = Collections.synchronizedList (new ArrayList ()); List syncLinkedList = Collections.synchronizedList (new LinkedList ()); note: 1 > Collections.synchronizedList (List list) actually creates an inner class SynchronizedList of java.util.Collections. 2 > SynchronizedList only wraps the corresponding methods in List (encapsulated ArrayList, linkedList, etc.) with synchronous code blocks in its own methods, so SynchronizedList expands in the same way as its encapsulated list. 3 > SynchronizedList can also specify the lock object. If not, it defaults to this. 2) CopyOnWriteArrayList: 1 > use ReentrantLock to achieve thread synchronization. 2 > every time an element is added, the copy of the array is performed, so the write performance of CopyOnWriteArrayList is very poor. 3 > in a multithreaded environment, the read performance of CopyOnWriteArrayList is better than that of Collections.SynchronizedList (the get method of the latter is also wrapped in synchronous code blocks, so the read performance is slightly worse). 3) Stack inherits from Vector and adds push and pop methods. This is the end of the introduction of "what are the similarities and differences between ArrayList and Vector". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report