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

What's the use of ArrayList?

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

Share

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

This article introduces the relevant knowledge of "what is the use of ArrayList". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ArrayList is an ordered list.

Public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable

RandomAccess identifies the interface with no internal method, indicating that internal elements can be accessed randomly

Cloneable identifies the API. There is no internal method, indicating that you can be responsible for it.

Java.io.Serializable identity interface, no internal method, indicating support for serialization

The underlying data structure is an array of objects

Transient Object [] elementData

ArrayList object has a storage limit

Private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8

Capacity reduction. When the space capacity allocated by buffer is greater than the actual number of elements stored, the resources are released by capacity reduction.

Public void trimToSize () {modCount++; if (size

< elementData.length) { elementData = (size == 0) ? EMPTY_ELEMENTDATA : Arrays.copyOf(elementData, size); }} 扩容,步长为原容量的 50%, 扩容后将原buffer复制到新创建的buffer中。所以ArrayList如果存在频繁扩容的情况,会引起程序性能下降。 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);}

Inserting or deleting an Element at a specified location will cause batch copy of the underlying data.

insert

Public void add (int index, E element) {rangeCheckForAdd (index); ensureCapacityInternal (size + 1); / / Increments modCounting! System.arraycopy (elementData, index, elementData, index + 1, size-index); elementData [index] = element; size++;}

Delete

Public E remove (int index) {rangeCheck (index); modCount++; E oldValue = elementData (index); int numMoved = size-index-1; if (numMoved > 0) System.arraycopy (elementData, index+1, elementData, index, numMoved); elementData [--size] = null; / / clear to let GC do its work return oldValue;} private void fastRemove (int index) {modCount++; int numMoved = size-index-1 If (numMoved > 0) System.arraycopy (elementData, index+1, elementData, index, numMoved); elementData [--size] = null; / / clear to let GC do its work}

When emptying the data, it does not release the space allocated by the underlying array, but copies the object holdings in the array into null, which makes it convenient for the virtual machine to release the object memory directly when GC.

Public void clear () {modCount++; / / clear to let GC do its work / / releases the handle, telling GC that the object can be recycled, but not immediately for (int I = 0; I < size; iTunes +) elementData [I] = null; size = 0;}

There is a set intersection operation in data calculation, and this method is also provided in ArrayList.

Find the intersection

Public boolean retainAll (Collection c) {Objects.requireNonNull (c); return batchRemove (c, true);} private boolean batchRemove (Collection c, boolean complement) {final Object [] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try {for (; r < size; rang +) if (c.contains (elementData[ r]) = = complement) elementData [walled +] = elementData [r] } finally {/ / Preserve behavioral compatibility with AbstractCollection, / / even if c.contains () throws. If (r! = size) {System.arraycopy (elementData, r, elementData, w, size-r); w + = size-r;} if (w! = size) {/ / clear to let GC do its work for (int I = w; I < size) ElementData [I] = null; modCount + = size-w; size = w; modified = true;}} return modified;}

ArrayList implements the Serializable interface, indicating that it can be serialized, and internally provides methods for serialization and deserialization of the jdk standard. Through serialization and sending serialization methods, you can find that JDK does not write all the properties of ArrayList to the file, but serializes the size and every object in the array.

Private void writeObject (java.io.ObjectOutputStream s) throws java.io.IOException {/ / Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject (); / / Write out size as capacity for behavioural compatibility with clone () s.writeInt (size); / / Write out all elements in the proper order. For (int iTuno; I 0) {/ / be like clone (), allocate array based upon size not capacity ensureCapacityInternal (size); Object [] a = elementData; / / Read in all elements in the proper order. For (int item0; I

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