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 parsing of ArrayList source code in the java collection

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the parsing of ArrayList source code in the java collection, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Overall architecture

ArrayList is actually an array structure, as shown in the figure

Index: array subscript

ElementData: the array itself

Other basic concepts:

/ * Default initial capacity. * the initial size of the array * / private static final int DEFAULT_CAPACITY = 10 * The size of the ArrayList The size of the ArrayList (the number of elements it contains). * current array size * @ serial * / private int size;// indicates the number of versions of the current array that have been modified protected transient int modCount = 0

Class comments:

1. It can be expanded automatically.

2. Allow put null

3. Size, set, put, add, get time complexity O (1)

4. Non-thread-safe (existing as a shared variable), thread-safe SynchronizedList can be used if necessary (low performance)

Public boolean add (E e) {synchronized (mutex) {/ / synchronized is a lightweight lock, and mutex represents a current SynchronizedList return c.add (e);}}

5. When enhancing the for loop or iteration, if the size of the array changes, an exception is thrown

Source code parsing

First, initialize:

/ / 1. Initialize 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) }} / / 2. The size of the no-parameter initialization array is empty / / the array size is not the default 10 when initializing, and the first time add is expanded to 10public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;} / / 3. Specify initial data initialization public ArrayList (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.

Share To

Internet Technology

Wechat

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

12
Report