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 the capacity of ArrayList Collection in java

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to expand the ArrayList collection in java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to expand the ArrayList collection in java".

1. Capacity expansion depends on the adding method. You can see that there is a DEFAULT_CAPACITY attribute value of 10 from the attribute.

Public boolean add (E e) {/ / verify whether the expansion operation ensureCapacityInternal (size+ 1) is required; / / Increments mod Counting! / / add the value elementData [size++] = e; return true;} under the corresponding subscript

2. Just entered ensureCapacityInternal (size+1) size is a complete variable.

The size of the ArrayList is marked (the number of elements contained), while the one that has no elements at this time is ensureCapacityInternal (0room1).

/ 1. Enter the method private void ensureCapacityInternal (int minCapacity) {/ / 2, first call calculateCapacity (elementData, minCapacity), this method is to check the first time to add data, and return the default container size (10) / / 4, execute this method ensureExplicitCapacity (10) / / why the parameter is 10, because this method calculateCapacity (elementData, minCapacity) does the operation. EnsureExplicitCapacity (calculateCapacity (elementData, minCapacity)) } / / 3, array capacity calculation private static int calculateCapacity (Object [] elementData Int minCapacity) {/ / this is true if (elementData = = DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {/ / when the data is added for the first time. This method takes parameter 1 and parameter 2, the value / / DEFAULT_CAPACITY between the two numbers: default is 10 / / minCapacity: the first time to add data is 1 / / so 10 and 1, 10, Finally, 10 is returned to return Math.max (DEFAULT_CAPACITY) MinCapacity) } return minCapacity;} / / 5, ensure explicit capacity private void ensureExplicitCapacity (int minCapacity) {modCount++ / / 6. 10-0 > 0 when making judgment, / / adding data for the first time: 2-10 > 0 for the first time when data is added; 2-10 > 0 for the second time; if (minCapacity-elementData.length > 0) / / 7 for the second time. Execute the following method, which is the real method to expand capacity and determine the size of ArrayList container for the first time, grow (minCapacity). / / capacity expansion} Thank you for reading. The above is the content of "how to expand the ArrayList collection in java". After the study of this article, I believe you have a deeper understanding of how to expand the ArrayList collection in java, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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