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 analyze ArrayList with source code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to use source code to analyze ArrayList. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Initialization capacity of member variable / * * ArrayList * * / public static final int DEFAULT_CAPACITY = 10 maximum capacity of elements that can be stored in an array * * / private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8 * empty array, default to an empty array * * / public static final Object [] EMPTY_ELEMENTDATA = {} when the parameterless constructor is called / * * Array that actually holds data * * / transient Object [] number of elements of elementData;/** actual ArrayList * * / private int size

The initialization capacity of ArrayList is 10, the real data is stored in elementData, and the actual number of elements stored is expressed in size.

Constructor / * * No-parameter constructor * * / public ArrayList () {super (); this.elementData = EMPTY_ELEMENTDATA;} / * * initialize capacity constructor * * / public ArrayList (int initialCapacity) {super (); if (initialCapacity

< 0) { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } this.elementData = new Object[initialCapacity];}/** 已有集合构造函数 **/public ArrayList(Collection c) boolean retainAll(Collection c) clear() /** 清空当前集合中所有元素 **/public void clear() { modCount ++; //将所有元素设置为null让GC回收掉 for (int i = 0; i < size; i++) { elementData[i] = null; } size = 0;} 将所有元素设置为null并设置size为0。 remove(int index) /** 移除指定位置index元素 **/@Overridepublic E remove(int index) { rangeCheck(index); //获取到旧的元素 E oldValue = (E) elementData[index]; //计算需要移动的元素 int needMoveNum = size - index - 1; if (needMoveNum >

0) {/ / move all elements after removal forward System.arraycopy (elementData, index + 1, elementData, index, needMoveNum);} / / set the last element of the array to null elementData [--size] = null; return oldValue;} / * * detect whether the array is out of bounds * * / private void rangeCheck (int index) {if (index > = size | | index

< 0) { throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size); }} 先检测元素是否越界,然后根据索引获取到原有元素oldValue。 数组移除时,需要将index后面的元素往前移动一位,所以先根据移除元素的位置计算出需要移动元素的个数needMoveNum,如果大于0就通过System.arraycopy方法移动。 然后将最后一个元素设置为null,返回原有元素oldValue。 remove(Object o) /** 移除指定元素o **/@Overridepublic boolean remove(Object o) { if (o == null) { for (int index = 0; index < size; index++) { if (elementData[index] == null) { fastRemove(index); return true; } } } else { for (int index = 0; index < size; index++) { if (o.equals(elementData[index])) { fastRemove(index); return true; } } } return false;}/** 相比remove(int index)方法,不需要检测数组越界和返回旧元素 **/private void fastRemove(int index) { modCount++; //计算需要移动的元素 int needMoveNum = size - index - 1; if (needMoveNum >

0) {/ / move all elements after removal forward System.arraycopy (elementData, index + 1, elementData, index, needMoveNum);} / / set the last element of the array to null elementData [--size] = null;}

If the specified element o is null, the loop finds an element with null and removes it.

Otherwise, loop the array element, find the element through the equals method and remove it.

RemoveAll (Collection c)

/ * * remove all elements in collection c from the current collection * * / public boolean removeAll (Collection c) {Objects.requireNonNull (c); return batchRemove (c, false);}

RetainAll (Collection c)

/ * * only the elements that exist in both the current collection and the specified collection c are retained * * / public boolean retainAll (Collection c) {Objects.requireNonNull (c); return batchRemove (c, true);}

BatchRemove (Collection c, boolean complement)

Private boolean batchRemove (Collection c, boolean complement) {final Object [] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try {for (; r < size; renders +) {/ / determine whether to keep the same or different if (c.contains (elementdata [r]) = complement) {elementData [walled +] = elementData [r] } finally {/ / normally r and size are equal if an exception is thrown, copy the remaining uncompared to elementData if (r! = size) {System.arraycopy (elementData, r, elementData, w, size-r); w + = size-r } / / if there is removed data, w and size are not equal. At this time, leave the data after W empty and let GC recycle if (w! = size) {for (int I = w; I < size; I +) {elementData [I] = null;} modCount + = size-w; size = w Query for modified = true;}} return modified;} element

Get (int index)

/ * * query element * * / @ Overridepublic E get (int index) {/ / check whether the index is out of bounds rangeCheck (index); / / returns the element return elementData (index) at the specified position in the array;}

Determine whether the array is out of bounds

Just return the array elements at the specified location.

ElementData (int index)

E elementData (int index) {return (E) elementData [index]; modification of element

Set (int index, E e)

/ * * change the element * * / @ Overridepublic E set (int index, E) {rangeCheck (index); / / query the original element E oldValue = (E) elementData [index]; / / set to the latest element elementData [index] = e; return oldValue;}

First determine whether the array is out of bounds

Get the old element

Set the current location to a new element

Returns the old element.

Summary

The bottom layer of ArrayList is mainly realized by Object array elementData.

The initialization capacity of ArrayList is 10, and it increases by 1.5 times each time during capacity expansion.

Query update operation is the easiest, through the array subscript can quickly locate the location of elements for operation

When adding and deleting, it will be more time-consuming because you need to move the position of the following element, and the System.arraycopy method is mainly used to move the position of the element.

After reading the above, do you have any further understanding of how to use source code to analyze ArrayList? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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