Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the java data structure ArrayList

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what is java data structure ArrayList". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the java data structure ArrayList?"

Brief introduction

ArrayList is a common data structure in the java collection framework. Inherits from AbstractList and implements the List interface. The bottom layer realizes the dynamic change of capacity based on the array. Allow the existence of null. At the same time, RandomAccess, Cloneable, and Serializable interfaces are implemented, so ArrayList supports fast access, replication, and serialization.

Member variable

The underlying layer of ArrayList is based on the array to realize the dynamic change of capacity.

/ * The size of the ArrayList (the number of elements it contains). * / private int size; / / actual number of elements transient Object [] elementData

Note: the above size refers to the actual number of elements in the elementData, while elementData.length is the collection capacity, indicating the maximum number of elements that can be contained.

Default initial capacity size is 10

/ * Default initial capacity.*/private static final int DEFAULT_CAPACITY = 10

This variable is defined in AbstractList. Record the number of operations on the List. It is mainly used in Iterator to prevent collections from being modified during iterations.

Protected transient int modCount = 0

The following two variables are used in the constructor

/ * Shared empty array instance used for empty instances.*/private static final Object [] EMPTY_ELEMENTDATA = {}; / * Shared empty array instance used for default sized empty instances. We* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when* first element is added.*/private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}

What's the difference between two empty arrays? We distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when first element is added. To put it simply, the first time you add an element, you know that the elementData is initialized from an empty constructor or a parameter constructor. In order to confirm how to expand capacity.

Constructor No-parameter constructor / * Constructs an empty list with an initial capacity of ten.*/public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}

Note: the comment is to construct an empty list collection with a capacity of 10, but the constructor only assigns an empty array to elementData, which is actually expanded to 10 the first time an element is added.

Construct an initial capacity size of ArrayListpublic 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) }}

As can be seen from the above source code: when using the no-parameter constructor, the DEFAULTCAPACITY_EMPTY_ELEMENTDATA is assigned to elementData. When initialCapacity is 00:00, EMPTY_ELEMENTDATA is assigned to elementData. When initialCapacity is greater than 00:00, initialize an object array of size initialCapacity and assign it to elementData.

Use the specified Collection to construct the constructor public ArrayList (Collection) of ArrayList

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: 231

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

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report