In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to implement classes in java List interface". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to implement classes in java List interface" together!
List interface introduction-ArrayList
Ordered and repeatable
Threads are unsafe because there is no synchronized modifier
ArrayList source code conclusion
ArrayList maintains an array elementData of type Object.
transient Object[] elementData; // transient indicates that the property will not be serialized
When creating an ArrayList object, if a parameterless constructor is used, the initial elementData capacity is 0, the elementData is expanded to 10 for the first time, and the elementData is expanded to 1.5 times if it needs to be expanded again.
If you use a constructor of a specified size, the initial elementData capacity is the specified size. If you need to expand, you can directly expand elementData to 1.5 times.
List list = new List (); for(int i = 0; i
< 10; i++) { list.add(1); } 容器初始化为一个空数组 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; 执行add方法 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } 先确定是否要扩容 再执行扩容操作 private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); //(10,1) } ensureExplicitCapacity(minCapacity); } 先确定elementData是否是空数组,如果是空数组就赋给minCapacity最小容量10(DEFAULT_CAPACITY为10) private void ensureExplicitCapacity(int minCapacity) { modCount++; //记录当前集合被修改的次数 // overflow-conscious code if (minCapacity - elementData.length >0) grow(minCapacity); }
If the minimum capacity is greater than the actual size of elementData, then the expansion is performed.
private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); //Capacity is 1.5 times if (newCapacity - minCapacity
< 0) //newCapacity=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); } Thank you for reading, the above is the content of "how to implement the class of List interface in java", after learning this article, I believe that everyone has a deeper understanding of how to implement the class of List interface in java, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.