Hello everyone, I am your old friend Qing Ge. I know you miss me, so I am here again Really?
The data structure of Java is the focus of the interview. I believe that students who have participated in Java interviews will have some experience. When interviewers ask such questions, they often want to check whether you have studied the underlying structures of commonly used data types in Java, rather than simply staying at the level of "knowing how to use". So how do we answer this question well during the interview and satisfy the interviewer?
In this issue, I will analyze the principles of Java high-frequency test points ArrayList and LinkedList
, hoping to help you.
ArrayList
The bottom layer is an array of type Object. The initial The capacity is 10 and supports dynamic expansion. The expanded capacity is 1.5 times the current capacity. Its maximum capacity is Integer.MAX_VALUE - 8 (but it can still be expanded to Integer.MAX_VALUE). For the vacated 8 bits, the current explanation is It isavoiding some machine memory overflows and reducing the chance of errors
.
LinkedList
The bottom layer is a two-way linked list with an initial capacity of 0. To expand the capacity, you only need to create a new node and point the pointer to it.
In order to simplify it into verbally expressable language and facilitate students to explain to the interviewer during the interview, I will not post the source code auxiliary instructions here. Interested students can check the source code themselves. Internal structures and methods to deepen your understanding of this area.
Query
Insertion
Delete
Memory space
Expansion mechanism
Thread safety
ArrayList and LinkedList are both thread-unsafe and can easily cause dirty reading problems in multi-threaded environments. You can use the Collections.synchronizedList()
method to ensure thread safety
Storage Features
The stored elements are all ordered and can be repeated. New elements are stored at the end of the List.
The above is the detailed content of A question asked in almost all Java interviews: talk about the difference between ArrayList and LinkedList. For more information, please follow other related articles on the PHP Chinese website!