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

An example Analysis of the principle and Operation of ArrayList underlying capacity expansion in Java

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

Share

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

This article mainly introduces the principle of ArrayList underlying capacity expansion in Java and the example analysis of expansion operation. The article is very detailed and has certain reference value. Interested friends must finish reading it!

ArrayList is a commonly used data structure in the Java collection framework, and its underlying layer is based on arrays.

Chapter 1 introduction Overview Section 01 Overview

Underlying description

ArrayList is the implementation class of List. Its bottom layer is stored in Object array, so it is not thread-safe.

Late application

It is suitable for frequent query work, because the underlying layer is an array, which can be quickly found through the array subscript.

Section 02 differences

Different directions ArrayList collection LinkedList collection thread is not safe underlying principle Object type array two-way linked list random access support (implement RandomAccess interface) does not support memory occupation ArrayList waste space, the bottom is the array, at the end of the reserved part of the capacity space LinkedList occupies more space than ArrayList, there is a head-tail address value occupation space

Summary

Features of the ArrayList collection:

1. Thread is not safe

two。 The underlying data structure is an array (fast query, slow addition and deletion, support for fast random access)

3. There will be some waste of memory footprint, and some capacity space will be reserved at the end.

Chapter 2 Core Code Section 01 member variables

Code

Public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable {/ * default initial capacity size, default initialization capacity is 10 * / private static final int DEFAULT_CAPACITY = 10; / * empty array. When the content of the collection is empty, the underlying layer uses the empty array tag * / private static final Object [] EMPTY_ELEMENTDATA = {}; / * * is used for the no-parameter construction method, and this array definition is used when creating objects. * it can be distinguished from the array EMPTY_ELEMENTDATA above, knowing how much the capacity increases during the process of adding elements * / private static final Object [] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {} / * an array that holds ArrayList data, which is constantly changing. It is not modified by final to indicate that the address value will change * / transient Object [] elementData. The number of elements contained in / / non-private to simplify nested class access / * ArrayList, that is, the number of elements obtained by the size () method at the bottom of the ArrayList collection * / private int size;}

Supplement

1. There are six member variables at the bottom of the ArrayList collection and a private static final long serialVersionUID = 8683452581122892189L. Serialization is not available for the current operation. 2. The two core member variables of the ArrayList set, A. The underlying maintenance array transient Object [] elementData; B. The number of elements stored private int size; Section 02 Construction method

Code

Public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable {/ * construct an empty array of initial length 0. * / public ArrayList () {this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA } / * in the construction method, pass a parameter set c to convert the set c into the data in the new list * elementData, that is, the data stored in the new set * c.toArray is to take out the data of the original set * if the length of the extracted set is not zero Then copy the parameter set c to the elementData * if the length of the fetched set is zero, the value is assigned to the empty array EMPTY_ELEMENTDATA * / 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

Development

Wechat

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

12
Report