Deep understanding of ArrayList in Java

Source: Internet
Author: User

1, ArrayList Insert delete must be slow?

Depending on how far away you delete the element from the end of the array, it is quite appropriate for ArrayList to be used as a stack, and push and pop operations do not involve data movement at all.

2, how is the ArrayList traversal and linkedlist traversal performance compared?

On traversing ArrayList is much faster than LinkedList, the most important advantage of ArrayList traversal is the continuity of memory, the internal cache structure of the CPU caches contiguous memory fragments, which can greatly reduce the performance overhead of reading memory.

3, how to expand the ArrayList?

ArrayList expansion size is equal to 1.5 times times the size of the pre-expansion, when the ArrayList is very large, so the expansion is quite a waste of space, and even lead to insufficient memory thrown outofmemoryerror. It also takes a lot of time to copy the array when you are expanding. So when we use to avoid the expansion, to provide an initial estimate capacity parameters, so as to avoid the expansion of performance has a greater impact.

4. Why is the default array size of ArrayList 10?

In fact, little did not find specific reasons. It is said that the sun's programmers investigated a series of widely used program codes, and the result is that an array of length 10 is the most commonly used and most efficient. There is also said to be casually up a number, 8 12 is no different, just because 10 of this array comparison of the perfect.

5, ArrayList is thread-safe?

Of course not, the thread-safe version of the array container is a vector. The implementation of the vector is simple, that is, all the methods are added synchronized to the finish. You can also not use vectors, Using Collections.synchronizedlist to wrap an ordinary ArrayList into a thread-safe version of the array container is also possible, the principle is the same as the vector, that is, to all the methods set on a layer of synchronized.

6. Is the array used to make the queue suitable?

Arrays are well suited to queue. For example, arrayblockingqueue internal implementation is a ring queue, it is a fixed-length queue, the interior is a fixed-length array to achieve. Another well-known disruptor open-Source library is also a ring array to achieve the ultra-high-performance queue, the specific principle does not explain, more complex. The simple point is that it uses two offsets to mark the read and write positions of the array, and if they are longer than the length, they are retraced to the beginning of the array, provided they are fixed-length arrays.

7, ArrayList fit to do the queue?

ArrayList is not suitable for the queue. Because the queue is generally FIFO, if you use ArrayList to do the queue, you need to append the data at the end of the array, the array header to delete the array, and vice versa. But in any case there will always be an operation that involves the data relocation of the array, which is more expensive to perform.

This article transferred from: https://www.toutiao.com/i6506647221109260807/

Deep understanding of ArrayList in Java

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.