In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to deeply understand the ArrayList source code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
ArrayList is the most common collection class, and as the name implies, ArrayList is a collection implemented as an array. It has the characteristics of the List collection:
Access order
With index
Allow repeating elements
Its own characteristics are:
Find elements quickly
Sequential insertion fast
Then why does ArrayList have these features? In fact, we can understand how it is implemented from the source code.
Resizable-array implementation of the {@ code List} interface. Implements all optional list operations, and permits all elements, including {@ code null}. In addition to implementing the {@ code List} interface,this class provides methods to manipulate the size of the array that is used internally to store the list.
Implements a resizable array of the List interface. You can store all elements, including null, and in addition to implementing the methods of the List interface, you also provide methods for manipulating the size of the array used internally to store elements.
Public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable
The RandomAccess tagging interface indicates that ArrayList supports random access. What is random access? Random access means that you can access any node in the data structure at will. Assuming that the data structure has 10000 nodes, you can access the 1st to 10000 nodes at will. Because ArrayList uses arrays to store data, when Java divides memory into arrays to store elements, it divides a contiguous memory address, and these adjacent elements are stored sequentially. As long as you know the address of the starting element First, directly first+N, you can get the address of the Nth element. This is why the ArrayList search is fast.
The Cloneable tag interface indicates that ArrayList can be cloned.
The java.io.Serializable tag interface indicates that ArrayList can be serialized.
Main member variables:
@ java.io.Serial private static final long serialVersionUID = 8683452581122892189L
Used to indicate the version number when serialized.
Private static final int DEFAULT_CAPACITY = 10
Default initialization size.
Private static final Object [] EMPTY_ELEMENTDATA = {}
The default empty array size.
Private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}
Initializes the empty array size by default.
Some people may wonder that this is also an empty array, so why should there be two reference variables to represent it? Let's take a look at the following explanation of the expansion mechanism.
Transient Object [] elementData
This is the array maintained by ArrayList to store data. Isn't it paradoxical that transient indicates that the array that stores data will not be serialized, while ArrayList marks the interface java.io.Serializable to say it is serializable? Press not Table for the time being to look at the internal mechanism of ArrayList and then go back to explain.
Private int size
The size of the ArrayList (that is, the number of elements it contains).
Construction method:
/ / public ArrayList (int initialCapacity) {if (initialCapacity > 0) {this.elementData = new Object [initialCapacity];} else if (initialCapacity = = 0) {this.elementData = EMPTY_ELEMENTDATA;} else {throw new IllegalArgumentException ("Illegal Capacity:" + initialCapacity) }} / / public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;} / / with no initial value passed in the public ArrayList (Collection) of a Collection collection
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.