ArrayList is probably the simplest of the Java data structures, and even a non-Java programmer probably knows about it because there are similar data structures in all languages. But in the experience of countless interviews I found that not all partners are proficient in this simple data structure, such as the following questions, can answer the success of the general above are not much. Do not believe you to challenge!

Is ArrayList insertion and deletion slow?

Depending on how far away you are from the end of the array, an ArrayList is a good use for a stack, and push and POP operations don’t involve moving data at all.

How does ArrayList traverse compare to LinkedList traverse performance?

ArrayList traversal is much faster than LinkedList traversal. The biggest advantage of ArrayList traversal is the continuity of memory. The CPU’s internal cache structure caches contiguous memory fragments, which greatly reduces the performance overhead of reading memory.

How does ArrayList scale up?

The size of the expanded ArrayList is equal to 1.5 times the size before the expansion. When the ArrayList is too large, this expansion is still a waste of space and can even cause an OutOfMemoryError to be raised due to insufficient memory. You also need to copy the array when you expand it, which is also time-consuming. Therefore, we should try to avoid capacity expansion and provide an initial capacity estimation parameter to avoid large impact on performance.

Why is the default array size of an ArrayList 10?

In fact, xiaobian did not find specific reasons. It is said that sun programmers surveyed a wide range of code and found that arrays of length 10 were the most common and efficient. You can pick a random number, 8 or 12, and it doesn’t make any difference, just because 10 is a complete array comparison.

Is ArrayList thread safe?

Of course not. The thread-safe version of the array container is a Vector. Vector is simply implemented by adding all methods to synchronized. You can not use Vector, with Collections. SynchronizedList put an ordinary ArrayList packaging container into a thread-safe version of the array can also, with the Vector is the same, is to give all of the synchronized methods on a layer.

Are arrays good for queues?

Queues are usually FIFO, and if you use an ArrayList as a queue, you append data to the end of the array, delete the array from the head, and vice versa. However, there is always an operation that involves moving data from an array, which can be performance costly.

That’s the wrong answer!

Arraylists are not good for queues, but arrays are. For example, the internal implementation of ArrayBlockingQueue is a ring queue, which is a fixed-length queue, and is implemented internally with a fixed-length array. Disruptor, the well-known open source Library, is also a very high performance queue that uses a circular array. In simple terms, we use two offsets to mark the read and write positions of the array. If they exceed the length, we revert to the beginning of the array, provided they are fixed-length arrays.

-end-

If you see this, you like this article, please share, like and follow