A question asked in almost all Java interviews: talk about the difference between ArrayList and LinkedList

Release: 2023-07-26 15:11:52
forward
826 people have browsed it

Preface

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.

Introduction to ArrayList and LinkedList

ArrayListThe 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.

LinkedListThe 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.

Difference

Query

  • ArrayList random access is very efficient because the elements are stored in order. You can know the location of the queried data in the memory through the subscript index. The addressing is fast and the time complexity is O(1);
  • LinkedList query efficiency is low. When querying the specified data, it needs to traverse the linked list one by one, and the time complexity is O(n).

Insertion

  • ArrayList inserts at the end more efficiently, with a time complexity of O(1), but in Insertion efficiency at other positions is relatively low, requiring a large amount of data movement, and the time complexity is O(n);
  • LinkedList is more efficient at inserting elements at the head and tail, and the time complexity is complex The degree is O(1), but to insert an element at a specified position in the middle, you need to traverse to find the position of the element first, and then insert it. The time complexity is O(n).

Delete

  • Removing elements from ArrayList requires a large amount of data movement except for the end node. Time complexity O(n);
  • LinkedList is relatively efficient in deleting elements. It only needs to change the pointing of the pointer, but deleting elements requires traversing to query the location of the data, and the time complexity is O (n).

Memory space

  • ArrayList is implemented based on arrays. The capacity is fixed after each expansion, so there will be Reserve a part of the space;
  • LinkedList is based on a doubly linked list, so in addition to saving data, each node also needs to save the pointers of the previous and next nodes, which will consume some space.

Expansion mechanism

  • Every time ArrayList is expanded, the elements of the original array need to be copied to the new array;
  • LinkedList is a linked list and there is no expansion.

Same points

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!

Related labels:
source:Java学习指南
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template