Collection of classic Java interview questions (6)

王林
Release: 2020-07-13 17:17:38
forward
2407 people have browsed it

Collection of classic Java interview questions (6)

1. What is an array? What is a linked list?

(Recommended related interview questions: java interview questions)

Arrays are like people standing in a row with numbers on their bodies. It is very difficult to find the 10th person. Easy, you can find it quickly based on the serial number on the person. But insertion and deletion are slow. When a person is inserted or deleted at a certain position, the numbers on the subsequent people will change. Of course, people are always added or deleted quickly.

The linked list is a storage structure in which the reference of the previous element points to the next element. The linked list connects elements with pointers;

The linked list is like people standing in a circle holding hands. It's not easy to find the 10th person. You have to count them one by one from the first person. But insertion and deletion are fast. When inserting, just untie the hands of two people and re-take the hand of the newly added person. Same thing with deletion.

In Java, ArrayList and LinkedList are internally implemented using arrays and linked lists respectively.

2. What is the difference between an array and a linked list?

Difference: a linked list is a linked storage structure; an array is a sequential storage structure.

Linked lists connect elements to elements through pointers, while arrays store all elements in order.

(recommended related tutorials: java introductory tutorial)

Inserting and deleting elements in a linked list is simpler than in an array. There is no need to move elements, and it is easier to achieve length expansion. However, It is more difficult to find an element;

It is relatively simple to find an element in an array, but insertion and deletion are more complicated. Since the maximum length needs to be specified at the beginning of programming, when the maximum length is reached, the expansion length is not as good as that of a linked list. convenient.

Same: Both structures can realize sequential storage of data, and the constructed model has a linear structure.

3. Characteristics of java collections and arrays

Array characteristics: fixed size, can only store data of the same data type

Collection characteristics: size It can be dynamically expanded and can store various types of data

(Related video tutorial recommendations: java video tutorial)

4. LinkedList underlying implementation

LinkedList is implemented through a doubly linked list. Since it is implemented as a linked list, its random access efficiency is lower than ArrayList, and its sequential access efficiency is relatively high. Each node has a predecessor (pointer to the previous node) and a successor (pointer to the subsequent node). The effect is as follows:

Collection of classic Java interview questions (6)

1. Use for for looping ArrayLIst and arrays, the program will get stuck when looping LinkedList in large quantities. for is suitable for looping array structures and traversing through subscripts.

2. Using foreach is suitable for looping LinkedList. For implementation using double linked list structure, foreach loop should be used.

The above is the detailed content of Collection of classic Java interview questions (6). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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