List source code interview questions summary

List source code interview questions summary

List is the most common type of collection at work. During the interview, various interview questions are often asked. Generally speaking, as long as you have read the source code, you have an understanding of the overall structure and details of List. If it is, the basic problem is not big.

1. Interview questions

1.1. Tell me about your understanding of ArrayList?

Many interviewers like this kind of questioning method, mainly to check whether the interview students have summarized the experience of ArrayList. Since ArrayList contains a lot of content, it is recommended to answer the overall structure first, and then proceed from a certain detail as a breakthrough, such as this: The
underlying data structure of ArrayList is a Array and its API have made a layer of encapsulation for the access to the bottom of the array. For example, the process of the add method is... (here we can refer to the process of add in the ArrayList source code analysis and design ).

Generally, if the interviewer sees your answers well and there are no loopholes, they will not go deep, so that the initiative of the interview is in their own hands. If you have to hesitate to answer, the interviewer may start. My own interview routine.

1.2. Expansion issues

1.2.1. ArrayList has no parameter constructor construction, now add a value in, what is the number of elements in the array at this time, and what is the maximum usable size before the next expansion?

Because it is a parameterless constructor, the expansion operation will be triggered when a value is added in. ArrayList has a default value when it is expanded for the first time. The default value is 10, so when a value is added for the first time, the number of elements in the array The number (size) is 1, and the array size (capacity) becomes 10.

1.2.2. If I continuously add new values ​​to the list, what is the size of the array when it reaches the 11th one?

At this time, we hope that the size of the array is 11, but in fact the maximum capacity of the array is only 10. If it is not enough, it needs to be expanded. The formula for expansion is: oldCapacity + (oldCapacity>> 1), oldCapacity represents the current size of the array. The formula is: 10 + 10 /2 = 15, and then we find that 15 is enough, so the size of the array will be expanded to 15.

1.2.3. After the array is initialized and a value is added, if I use the addAll method to add 15 values ​​at once, what is the size of the final array?

We already know that after adding a value, the available size of the array is 10. Now we need to add 15 values ​​at once, which will obviously trigger the expansion operation. After the expansion, the capacity of the array becomes 15, which is not enough. At this time, the source code has a strategy, if The expanded value <our expected value, our expected value is equal to the size of this expansion.

// newCapacity 本次扩容的大小,minCapacity 我们期望的数组最小大小
// 如果扩容后的值 < 我们的期望值,我们的期望值就等于本次扩容的大小
if (newCapacity - minCapacity < 0)
    newCapacity = minCapacity;

So the final size of the array after expansion is 16.

1.3, delete problems

After the execution of the following code is complete, what are the remaining elements in the List?

List<String> list = new ArrayList<String>() {
    
    {
    
    
  add("2");
  add("3");
  add("3");
  add("3");
  add("4");
}};
for (int i = 0; i < list.size(); i++) {
    
    
  if (list.get(i).equals("3")) {
    
    
    list.remove(i);
  }
}

The answer is {2, 3, 4}, the last element 3 will not be deleted, because using the remove() method, every time you delete an element 3, the element of the array will be reduced by 1, so when you want to delete the last 3, The program will exit the for loop.

1.4. Comparison problems

1.4.1. What is the difference between ArrayList and LinkedList application scenarios?

ArrayList is more suitable for fast search and matching, and is not suitable for frequent additions and deletions. It is more suitable for scenarios where elements are often matched and inquired during work. LinkedList is more suitable for scenarios where frequent additions and deletions are often performed, but there are few queries.

1.4.2. Is there a maximum capacity for ArrayList and LinkedList?

ArrayList has the largest capacity, which is the maximum value of Integer. JVM will not allocate memory space for the array if it is larger than this value. The bottom layer of LinkedList is a doubly linked list, which can theoretically be infinite. But in the source code, the actual size of the LinkedList uses the int type, which also shows that the LinkedList cannot exceed the maximum value of Integer, otherwise it will overflow.

1.4.3. Are ArrayList and LinedList thread-safe and why?

First of all, neither is thread safe. When the two are used as non-shared variables, for example, when they are only local variables in the method, there is no thread safety problem. Only when the two are shared variables, there will be thread safety problems. The main problem is that in a multithreaded environment, all threads can operate on arrays and linked lists at any time, which can lead to values ​​being overwritten and even confusion.

2. Summary

List is often encountered at work. Reading the source code is not only for the interview, but also for the ease of use at work. If you want to understand List more deeply, you can re-implement a List after reading the ArrayList source code. In this case, you will have a deeper understanding of the underlying data structure and operation details of the List.

Guess you like

Origin blog.csdn.net/weixin_38478780/article/details/107729921