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

How to expand ArrayList capacity in JDK1.8

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

Share

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

This article mainly explains "how to expand the capacity of ArrayList in JDK1.8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to expand ArrayList in JDK1.8".

Introduction to ArrayList:

ArrayList implements the List interface, which is a resizable array that can be used to store various forms of data. It also provides a variety of methods, including CRUD, to manipulate the data, but it is not thread-safe, and the external ArrayList stores the data in the order in which it is inserted.

Before we talk about the expansion mechanism, we need to take a look at the most important variables in ArrayList:

Private static final int DEFAULT_CAPACITY = 10 DEFAULTCAPACITY_EMPTY_ELEMENTDATA / array default initial capacity private static final Object [] EMPTY_ELEMENTDATA = {}; / / define an empty array instance for other places where an empty array is needed to call private static final Object [] array = {}; / / define an empty array, which is different from the previous one is that this empty array is used to determine how much data will be expanded when the empty array is first added. The default constructor returns the place where the empty array transient Object [] elementData;// data is stored, and its capacity is the length of the array, and as long as the default constructor (DEFAULTCAPACITY_EMPTY_ELEMENTDATA) is used to add data for the first time, the capacity is expanded to the length of the current array DEFAULT_CAPACITY = 10 private int size;//.

All the explanations of this topic are based on JDK8

This question examines the constructor of ArrayList and the understanding of the expansion mechanism. Based on this, this blog explains the expansion mechanism of ArrayList.

To solve this problem, you must understand the constructor of ArrayList. There are a total of three constructors for ArrayList:

ArrayList () constructs an empty array. Construct an empty list with an initial capacity of 10 in JDK7, but just an empty array in JDK8

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