The difference between arrays and linked lists

Features of arrays

In memory, an array is a contiguous region. The amount of space that an array needs to be reserved.

Data insertion and deletion are inefficient. Before data insertion, data after this location is moved backward in memory. When data is deleted, all data after the data is moved forward.

Random reading is very efficient. Because the array is contiguous, you know the memory address of each data, and you can directly find the data at that address.

It’s not good for scaling. You need to redefine the array when you run out of space.

Characteristics of linked lists

It can exist anywhere in memory and does not require continuity. Each piece of data holds the memory address of the next piece of data, which is used to find the next piece of data. Adding data and deleting data are easy. Data lookup is inefficient because there are no random access rows, so data accessing a location starts with the first data and then finds the second data based on the address of the next data stored in the first data. Do not specify the size, easy to expand. Linked list size is not defined, data can be added or deleted at will.

The advantages and disadvantages

Advantages of arrays

Strong random access and fast search

Disadvantages of arrays

Inefficient insertion and deletion may waste memory Memory space requirements are high and there must be sufficient contiguous memory space. Arrays are fixed in size and cannot be dynamically expanded.

Advantages of linked lists

Fast insertion speed, high memory utilization, no waste of memory size is not specified, very flexible expansion

Disadvantages of linked lists

It cannot be searched randomly, but must be traversed from the first, which is inefficient

The difference between volatile and synchronized

Volatile essentially tells the JVM that the value of the current variable in the register (working memory) is indeterminate and needs to be read from main memory; Synchronized locks the current variable so that only the current thread can access it and other threads are blocked.

Volatile can only be used at the variable level; Synchronized can be used at the variable, method, and class levels

Volatile only enables change visibility of variables, not atomicity. Synchronized can guarantee the change visibility and atomicity of variables

Volatile does not block threads; Synchronized can cause threads to block.

Volatile variables are not optimized by the compiler; Variables of the synchronized tag can be optimized by the compiler