In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the maximum length of Arraylist in Java, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.
Maximum length of Arraylist MAX_ARRAY_SIZE of Arraylist = Integer.MAX_VALUE-8
What is the maximum length of the ArrayList collection?
/ * The maximum size of array to allocate. * Some VMs reserve some header words in an array. * Attempts to allocate larger arrays may result in * OutOfMemoryError: Requested array size exceeds VM limit * / private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8; / * * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @ param minCapacity the desired minimum capacity * / private void grow (int minCapacity) {/ / overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity > > 1); if (newCapacity-minCapacity
< 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE >0) newCapacity = hugeCapacity (minCapacity); / / minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf (elementData, newCapacity);} private static int hugeCapacity (int minCapacity) {if (minCapacity)
< 0) // overflow throw new OutOfMemoryError(); return (minCapacity >MAX_ARRAY_SIZE)? Integer.MAX_VALUE: MAX_ARRAY_SIZE;}
MAX_ARRAY_SIZE = Integer.MAX_VALUE-8 is defined in the source code; the comments above are also clear.
Some vm may retain some header information in the array, and allocating a larger length may result in an OutOfMemoryError exception.
The reason for doing this here is to avoid, as far as possible, the OutOfMemoryError exception caused by the allocation of a larger length because vm uses data to store header information. But it is not necessary to exceed this length will be abnormal. It's just to avoid it as much as possible. However, if an vm uses an array to hold some header, and the length of these header is greater than 8, then the OutOfMemoryError exception will still occur when the array is expanded to 2 ^ 31-1, minus the information length of the header.
The maximum length of Arraylist is 2147483647, that is, 2 ^ 31-1
The underlying structure of arrayList is based on an array, and the most big data as a subscript should be Integer.MAX_VALUE, that is, 2 ^ 31-1.
If we look at the grow (int minCapacity) in the above code, we will find that there is a special place in it.
If (newCapacity-MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity (minCapacity)
The hugeCapacity (int minCapacity) method shows that
Private static int hugeCapacity (int minCapacity) {if (minCapacity)
< 0) // overflow throw new OutOfMemoryError(); return (minCapacity >MAX_ARRAY_SIZE)? Integer.MAX_VALUE: MAX_ARRAY_SIZE;}
The new length can be Integer.MAX_VALUE when certain conditions are met.
So the maximum length of Arraylist is 2147483647, that is, 2 ^ 31-1.
The expansion problem of ArrayList there are two kinds of capacity of ArrayList
There are two construction methods in the source code of Sun company
1. Construction method without parameters
The no-parameter construction method is to create an empty array in advance and initialize the capacity to 10 when the first element is added to the array.
two。 Construction method with parameters
The parameter construction method is to pass in a capacity value and then define the size of the capacity.
Then there is the problem of capacity expansion after the capacity is full.
The reason for capacity expansion is that because the capacity is full, the method to add elements is add (), so the capacity expansion has something to do with the add () method.
From the following add () method, we can see that the add () calls another add () method. When we click on the past, we find that the expansion is also related to the grow () method.
This is the final expansion of the grow () method. We can find one of these.
The minimum capacity growth is minCapacity-oldCapacity, and then the oldCapacity is a bit operation that moves one bit to the right.
That is, increased capacity = raw capacity-raw capacity / 20.5 raw capacity
Therefore, the capacity after expansion is 1.5 times that before expansion.
In addition, the bottom layer of HashSet is the multiple expansion factor of HashMap initialization capacity 16, expansion capacity * 2 and initialization capacity 2.
The initialization capacity of Properties is 11, and the expansion factor is 0.75.
Thank you for reading this article carefully. I hope the article "what is the maximum length of Arraylist in Java" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.