ArrayList婧愮爜鍒嗘瀽
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArrayList婧愮爜鍒嗘瀽相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/rac' title='rac'>rac
绱犳暟 搴忓彿 warning set 鎸囧畾鍏冪礌 range 鎵撳嵃 turn
ArrayList鏍稿績婧愮爜
package java.util;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
/**
* 榛樿鍒濆瀹归噺澶у皬
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 绌烘暟缁勶紙鐢ㄤ簬绌哄疄渚嬶級銆? */
private static final Object[] EMPTY_ELEMENTDATA = {};
//鐢ㄤ簬榛樿澶у皬绌哄疄渚嬬殑鍏变韩绌烘暟缁勫疄渚嬨€? //鎴戜滑鎶婂畠浠嶦MPTY_ELEMENTDATA鏁扮粍涓尯鍒嗗嚭鏉ワ紝浠ョ煡閬撳湪娣诲姞绗竴涓厓绱犳椂瀹归噺闇€瑕佸鍔犲灏戙€? private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* 淇濆瓨ArrayList鏁版嵁鐨勬暟缁? */
transient Object[] elementData; // non-private to simplify nested class access
/**
* ArrayList 鎵€鍖呭惈鐨勫厓绱犱釜鏁? */
private int size;
/**
* 甯﹀垵濮嬪閲忓弬鏁扮殑鏋勯€犲嚱鏁般€傦紙鐢ㄦ埛鑷繁鎸囧畾瀹归噺锛? */
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
//鍒涘缓initialCapacity澶у皬鐨勬暟缁? this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
//鍒涘缓绌烘暟缁? this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
*榛樿鏋勯€犲嚱鏁帮紝DEFAULTCAPACITY_EMPTY_ELEMENTDATA 涓?.鍒濆鍖栦负10锛屼篃灏辨槸璇村垵濮嬪叾瀹炴槸绌烘暟缁?褰撴坊鍔犵涓€涓厓绱犵殑鏃跺€欐暟缁勫閲忔墠鍙樻垚10
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
/**
* 鏋勯€犱竴涓寘鍚寚瀹氶泦鍚堢殑鍏冪礌鐨勫垪琛紝鎸夌収瀹冧滑鐢遍泦鍚堢殑杩唬鍣ㄨ繑鍥炵殑椤哄簭銆? */
public ArrayList(Collection<? extends E> c) {
//
elementData = c.toArray();
//濡傛灉鎸囧畾闆嗗悎鍏冪礌涓暟涓嶄负0
if ((size = elementData.length) != 0) {
// c.toArray 鍙兘杩斿洖鐨勪笉鏄疧bject绫诲瀷鐨勬暟缁勬墍浠ュ姞涓婁笅闈㈢殑璇彞鐢ㄤ簬鍒ゆ柇锛? //杩欓噷鐢ㄥ埌浜嗗弽灏勯噷闈㈢殑getClass()鏂规硶
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// 鐢ㄧ┖鏁扮粍浠f浛
this.elementData = EMPTY_ELEMENTDATA;
}
}
/**
* 淇敼杩欎釜ArrayList瀹炰緥鐨勫閲忔槸鍒楄〃鐨勫綋鍓嶅ぇ灏忋€?搴旂敤绋嬪簭鍙互浣跨敤姝ゆ搷浣滄潵鏈€灏忓寲ArrayList瀹炰緥鐨勫瓨鍌ㄣ€?
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
//涓嬮潰鏄疉rrayList鐨勬墿瀹规満鍒?//ArrayList鐨勬墿瀹规満鍒舵彁楂樹簡鎬ц兘锛屽鏋滄瘡娆″彧鎵╁厖涓€涓紝
//閭d箞棰戠箒鐨勬彃鍏ヤ細瀵艰嚧棰戠箒鐨勬嫹璐濓紝闄嶄綆鎬ц兘锛岃€孉rrayList鐨勬墿瀹规満鍒堕伩鍏嶄簡杩欑鎯呭喌銆? /**
* 濡傛湁蹇呰锛屽鍔犳ArrayList瀹炰緥鐨勫閲忥紝浠ョ‘淇濆畠鑷冲皯鑳藉绾冲厓绱犵殑鏁伴噺
* @param minCapacity 鎵€闇€鐨勬渶灏忓閲? */
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// any size if not default element table
? 0
// larger than default for default empty table. It's already
// supposed to be at default size.
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
//寰楀埌鏈€灏忔墿瀹归噺
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
// 鑾峰彇榛樿鐨勫閲忓拰浼犲叆鍙傛暟鐨勮緝澶у€? minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
//鍒ゆ柇鏄惁闇€瑕佹墿瀹? private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
//璋冪敤grow鏂规硶杩涜鎵╁锛岃皟鐢ㄦ鏂规硶浠h〃宸茬粡寮€濮嬫墿瀹逛簡
grow(minCapacity);
}
/**
* 瑕佸垎閰嶇殑鏈€澶ф暟缁勫ぇ灏? */
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* ArrayList鎵╁鐨勬牳蹇冩柟娉曘€? */
private void grow(int minCapacity) {
// oldCapacity涓烘棫瀹归噺锛宯ewCapacity涓烘柊瀹归噺
int oldCapacity = elementData.length;
//灏唎ldCapacity 鍙崇Щ涓€浣嶏紝鍏舵晥鏋滅浉褰撲簬oldCapacity /2锛? //鎴戜滑鐭ラ亾浣嶈繍绠楃殑閫熷害杩滆繙蹇簬鏁撮櫎杩愮畻锛屾暣鍙ヨ繍绠楀紡鐨勭粨鏋滃氨鏄皢鏂板閲忔洿鏂颁负鏃у閲忕殑1.5鍊嶏紝
int newCapacity = oldCapacity + (oldCapacity >> 1);
//鐒跺悗妫€鏌ユ柊瀹归噺鏄惁澶т簬鏈€灏忛渶瑕佸閲忥紝鑻ヨ繕鏄皬浜庢渶灏忛渶瑕佸閲忥紝閭d箞灏辨妸鏈€灏忛渶瑕佸閲忓綋浣滄暟缁勭殑鏂板閲忥紝
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//鍐嶆鏌ユ柊瀹归噺鏄惁瓒呭嚭浜咥rrayList鎵€瀹氫箟鐨勬渶澶у閲忥紝
//鑻ヨ秴鍑轰簡锛屽垯璋冪敤hugeCapacity()鏉ユ瘮杈僲inCapacity鍜?MAX_ARRAY_SIZE锛? //濡傛灉minCapacity澶т簬MAX_ARRAY_SIZE锛屽垯鏂板閲忓垯涓篒nterger.MAX_VALUE锛屽惁鍒欙紝鏂板閲忓ぇ灏忓垯涓?MAX_ARRAY_SIZE銆? 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);
}
//姣旇緝minCapacity鍜?MAX_ARRAY_SIZE
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
*杩斿洖姝ゅ垪琛ㄤ腑鐨勫厓绱犳暟銆?
*/
public int size() {
return size;
}
/**
* 濡傛灉姝ゅ垪琛ㄤ笉鍖呭惈鍏冪礌锛屽垯杩斿洖 true 銆? */
public boolean isEmpty() {
//娉ㄦ剰=鍜?=鐨勫尯鍒? return size == 0;
}
/**
* 濡傛灉姝ゅ垪琛ㄥ寘鍚寚瀹氱殑鍏冪礌锛屽垯杩斿洖true 銆? */
public boolean contains(Object o) {
//indexOf()鏂规硶锛氳繑鍥炴鍒楄〃涓寚瀹氬厓绱犵殑棣栨鍑虹幇鐨勭储寮曪紝濡傛灉姝ゅ垪琛ㄤ笉鍖呭惈姝ゅ厓绱狅紝鍒欎负-1
return indexOf(o) >= 0;
}
/**
*杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾鍏冪礌鐨勯娆″嚭鐜扮殑绱㈠紩锛屽鏋滄鍒楄〃涓嶅寘鍚鍏冪礌锛屽垯涓?1
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
//equals()鏂规硶姣旇緝
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
* 杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾鍏冪礌鐨勬渶鍚庝竴娆″嚭鐜扮殑绱㈠紩锛屽鏋滄鍒楄〃涓嶅寘鍚厓绱狅紝鍒欒繑鍥?1銆?
*/
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
* 杩斿洖姝rrayList瀹炰緥鐨勬祬鎷疯礉銆?锛堝厓绱犳湰韬笉琚鍒躲€傦級
*/
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
//Arrays.copyOf鍔熻兘鏄疄鐜版暟缁勭殑澶嶅埗锛岃繑鍥炲鍒跺悗鐨勬暟缁勩€傚弬鏁版槸琚鍒剁殑鏁扮粍鍜屽鍒剁殑闀垮害
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// 杩欎笉搴旇鍙戠敓锛屽洜涓烘垜浠槸鍙互鍏嬮殕鐨? throw new InternalError(e);
}
}
/**
*浠ユ纭殑椤哄簭锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍銆?
*杩斿洖鐨勬暟缁勫皢鏄€滃畨鍏ㄧ殑鈥濓紝鍥犱负璇ュ垪琛ㄤ笉淇濈暀瀵瑰畠鐨勫紩鐢ㄣ€?锛堟崲鍙ヨ瘽璇达紝杩欎釜鏂规硶蹇呴』鍒嗛厤涓€涓柊鐨勬暟缁勶級銆? *鍥犳锛岃皟鐢ㄨ€呭彲浠ヨ嚜鐢卞湴淇敼杩斿洖鐨勬暟缁勩€?姝ゆ柟娉曞厖褰撳熀浜庨樀鍒楀拰鍩轰簬闆嗗悎鐨凙PI涔嬮棿鐨勬ˉ姊併€? */
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
/**
* 浠ユ纭殑椤哄簭杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級;
*杩斿洖鐨勬暟缁勭殑杩愯鏃剁被鍨嬫槸鎸囧畾鏁扮粍鐨勮繍琛屾椂绫诲瀷銆?濡傛灉鍒楄〃閫傚悎鎸囧畾鐨勬暟缁勶紝鍒欒繑鍥炲叾涓€?
*鍚﹀垯锛屽皢涓烘寚瀹氭暟缁勭殑杩愯鏃剁被鍨嬪拰姝ゅ垪琛ㄧ殑澶у皬鍒嗛厤涓€涓柊鏁扮粍銆?
*濡傛灉鍒楄〃閫傜敤浜庢寚瀹氱殑鏁扮粍锛屽叾浣欑┖闂达紙鍗虫暟缁勭殑鍒楄〃鏁伴噺澶氫簬姝ゅ厓绱狅級锛屽垯绱ц窡鍦ㄩ泦鍚堢粨鏉熷悗鐨勬暟缁勪腑鐨勫厓绱犺缃负null 銆? *锛堣繖浠呭湪璋冪敤鑰呯煡閬撳垪琛ㄤ笉鍖呭惈浠讳綍绌哄厓绱犵殑鎯呭喌涓嬫墠鑳界‘瀹氬垪琛ㄧ殑闀垮害銆傦級
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// 鏂板缓涓€涓繍琛屾椂绫诲瀷鐨勬暟缁勶紝浣嗘槸ArrayList鏁扮粍鐨勫唴瀹? return (T[]) Arrays.copyOf(elementData, size, a.getClass());
//璋冪敤System鎻愪緵鐨刟rraycopy()鏂规硶瀹炵幇鏁扮粍涔嬮棿鐨勫鍒? System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
// Positional Access Operations
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
/**
* 杩斿洖姝ゅ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€? */
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
/**
* 鐢ㄦ寚瀹氱殑鍏冪礌鏇挎崲姝ゅ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€?
*/
public E set(int index, E element) {
//瀵筰ndex杩涜鐣岄檺妫€鏌? rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
//杩斿洖鍘熸潵鍦ㄨ繖涓綅缃殑鍏冪礌
return oldValue;
}
/**
* 灏嗘寚瀹氱殑鍏冪礌杩藉姞鍒版鍒楄〃鐨勬湯灏俱€?
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
//杩欓噷鐪嬪埌ArrayList娣诲姞鍏冪礌鐨勫疄璐ㄥ氨鐩稿綋浜庝负鏁扮粍璧嬪€? elementData[size++] = e;
return true;
}
/**
* 鍦ㄦ鍒楄〃涓殑鎸囧畾浣嶇疆鎻掑叆鎸囧畾鐨勫厓绱犮€?
*鍏堣皟鐢?rangeCheckForAdd 瀵筰ndex杩涜鐣岄檺妫€鏌ワ紱鐒跺悗璋冪敤 ensureCapacityInternal 鏂规硶淇濊瘉capacity瓒冲澶э紱
*鍐嶅皢浠巌ndex寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃紱灏唀lement鎻掑叆index浣嶇疆锛涙渶鍚巗ize鍔?銆? */
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
//arraycopy()杩欎釜瀹炵幇鏁扮粍涔嬮棿澶嶅埗鐨勬柟娉曚竴瀹氳鐪嬩竴涓嬶紝涓嬮潰灏辩敤鍒颁簡arraycopy()鏂规硶瀹炵幇鏁扮粍鑷繁澶嶅埗鑷繁
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
/**
* 鍒犻櫎璇ュ垪琛ㄤ腑鎸囧畾浣嶇疆鐨勫厓绱犮€?灏嗕换浣曞悗缁厓绱犵Щ鍔ㄥ埌宸︿晶锛堜粠鍏剁储寮曚腑鍑忓幓涓€涓厓绱狅級銆?
*/
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;
}
/**
* 浠庡垪琛ㄤ腑鍒犻櫎鎸囧畾鍏冪礌鐨勭涓€涓嚭鐜帮紙濡傛灉瀛樺湪锛夈€?濡傛灉鍒楄〃涓嶅寘鍚鍏冪礌锛屽垯瀹冧笉浼氭洿鏀广€? *杩斿洖true锛屽鏋滄鍒楄〃鍖呭惈鎸囧畾鐨勫厓绱? */
public 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;
}
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
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
}
/**
* 浠庡垪琛ㄤ腑鍒犻櫎鎵€鏈夊厓绱犮€?
*/
public void clear() {
modCount++;
// 鎶婃暟缁勪腑鎵€鏈夌殑鍏冪礌鐨勫€艰涓簄ull
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
/**
* 鎸夋寚瀹氶泦鍚堢殑Iterator杩斿洖鐨勯『搴忓皢鎸囧畾闆嗗悎涓殑鎵€鏈夊厓绱犺拷鍔犲埌姝ゅ垪琛ㄧ殑鏈熬銆? */
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
/**
* 灏嗘寚瀹氶泦鍚堜腑鐨勬墍鏈夊厓绱犳彃鍏ュ埌姝ゅ垪琛ㄤ腑锛屼粠鎸囧畾鐨勪綅缃紑濮嬨€? */
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
* 浠庢鍒楄〃涓垹闄ゆ墍鏈夌储寮曚负fromIndex 锛堝惈锛夊拰toIndex涔嬮棿鐨勫厓绱犮€? *灏嗕换浣曞悗缁厓绱犵Щ鍔ㄥ埌宸︿晶锛堝噺灏戝叾绱㈠紩锛夈€? */
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
/**
* 妫€鏌ョ粰瀹氱殑绱㈠紩鏄惁鍦ㄨ寖鍥村唴銆? */
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* add鍜宎ddAll浣跨敤鐨剅angeCheck鐨勪竴涓増鏈? */
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 杩斿洖IndexOutOfBoundsException缁嗚妭淇℃伅
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
/**
* 浠庢鍒楄〃涓垹闄ゆ寚瀹氶泦鍚堜腑鍖呭惈鐨勬墍鏈夊厓绱犮€?
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
//濡傛灉姝ゅ垪琛ㄨ淇敼鍒欒繑鍥瀟rue
return batchRemove(c, false);
}
/**
* 浠呬繚鐣欐鍒楄〃涓寘鍚湪鎸囧畾闆嗗悎涓殑鍏冪礌銆? *鎹㈠彞璇濊锛屼粠姝ゅ垪琛ㄤ腑鍒犻櫎鍏朵腑涓嶅寘鍚湪鎸囧畾闆嗗悎涓殑鎵€鏈夊厓绱犮€?
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
/**
* 浠庡垪琛ㄤ腑鐨勬寚瀹氫綅缃紑濮嬶紝杩斿洖鍒楄〃涓殑鍏冪礌锛堟寜姝g‘椤哄簭锛夌殑鍒楄〃杩唬鍣ㄣ€? *鎸囧畾鐨勭储寮曡〃绀哄垵濮嬭皟鐢ㄥ皢杩斿洖鐨勭涓€涓厓绱犱负next 銆?鍒濆璋冪敤previous灏嗚繑鍥炴寚瀹氱储寮曞噺1鐨勫厓绱犮€?
*杩斿洖鐨勫垪琛ㄨ凯浠e櫒鏄痜ail-fast 銆?
*/
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
/**
*杩斿洖鍒楄〃涓殑鍒楄〃杩唬鍣紙鎸夐€傚綋鐨勯『搴忥級銆?
*杩斿洖鐨勫垪琛ㄨ凯浠e櫒鏄痜ail-fast 銆? */
public ListIterator<E> listIterator() {
return new ListItr(0);
}
/**
*浠ユ纭殑椤哄簭杩斿洖璇ュ垪琛ㄤ腑鐨勫厓绱犵殑杩唬鍣ㄣ€?
*杩斿洖鐨勮凯浠e櫒鏄痜ail-fast 銆?
*/
public Iterator<E> iterator() {
return new Itr();
}
ArrayList婧愮爜鍒嗘瀽
System.arraycopy()鍜孉rrays.copyOf()鏂规硶
銆€銆€閫氳繃涓婇潰婧愮爜鎴戜滑鍙戠幇杩欎袱涓疄鐜版暟缁勫鍒剁殑鏂规硶琚箍娉涗娇鐢ㄨ€屼笖寰堝鍦版柟閮界壒鍒阀濡欍€傛瘮濡備笅闈?font color="red">add(int index, E element)
鏂规硶灏卞緢宸у鐨勭敤鍒颁簡arraycopy()鏂规硶璁╂暟缁勮嚜宸卞鍒惰嚜宸卞疄鐜拌index寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃?
/**
* 鍦ㄦ鍒楄〃涓殑鎸囧畾浣嶇疆鎻掑叆鎸囧畾鐨勫厓绱犮€?
*鍏堣皟鐢?rangeCheckForAdd 瀵筰ndex杩涜鐣岄檺妫€鏌ワ紱鐒跺悗璋冪敤 ensureCapacityInternal 鏂规硶淇濊瘉capacity瓒冲澶э紱
*鍐嶅皢浠巌ndex寮€濮嬩箣鍚庣殑鎵€鏈夋垚鍛樺悗绉讳竴涓綅缃紱灏唀lement鎻掑叆index浣嶇疆锛涙渶鍚巗ize鍔?銆? */
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
//arraycopy()鏂规硶瀹炵幇鏁扮粍鑷繁澶嶅埗鑷繁
//elementData:婧愭暟缁?index:婧愭暟缁勪腑鐨勮捣濮嬩綅缃?elementData锛氱洰鏍囨暟缁勶紱index + 1锛氱洰鏍囨暟缁勪腑鐨勮捣濮嬩綅缃紱 size - index锛氳澶嶅埗鐨勬暟缁勫厓绱犵殑鏁伴噺锛? System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}
鍙堝toArray()鏂规硶涓敤鍒颁簡copyOf()鏂规硶
/**
*浠ユ纭殑椤哄簭锛堜粠绗竴涓埌鏈€鍚庝竴涓厓绱狅級杩斿洖涓€涓寘鍚鍒楄〃涓墍鏈夊厓绱犵殑鏁扮粍銆?
*杩斿洖鐨勬暟缁勫皢鏄€滃畨鍏ㄧ殑鈥濓紝鍥犱负璇ュ垪琛ㄤ笉淇濈暀瀵瑰畠鐨勫紩鐢ㄣ€?锛堟崲鍙ヨ瘽璇达紝杩欎釜鏂规硶蹇呴』鍒嗛厤涓€涓柊鐨勬暟缁勶級銆? *鍥犳锛岃皟鐢ㄨ€呭彲浠ヨ嚜鐢卞湴淇敼杩斿洖鐨勬暟缁勩€?姝ゆ柟娉曞厖褰撳熀浜庨樀鍒楀拰鍩轰簬闆嗗悎鐨凙PI涔嬮棿鐨勬ˉ姊併€? */
public Object[] toArray() {
//elementData锛氳澶嶅埗鐨勬暟缁勶紱size锛氳澶嶅埗鐨勯暱搴? return Arrays.copyOf(elementData, size);
}
涓よ€呰仈绯讳笌鍖哄埆
鑱旂郴锛?/strong>
鐪嬩袱鑰呮簮浠g爜鍙互鍙戠幇copyOf()
鍐呴儴璋冪敤浜?code>System.arraycopy()
鏂规硶鍖哄埆锛?/strong>
- arraycopy()闇€瑕佺洰鏍囨暟缁勶紝灏嗗師鏁扮粍鎷疯礉鍒颁綘鑷繁瀹氫箟鐨勬暟缁勯噷锛岃€屼笖鍙互閫夋嫨鎷疯礉鐨勮捣鐐瑰拰闀垮害浠ュ強鏀惧叆鏂版暟缁勪腑鐨勪綅缃?/li>
copyOf()鏄郴缁熻嚜鍔ㄥ湪鍐呴儴鏂板缓涓€涓暟缁勶紝骞惰繑鍥炶鏁扮粍銆?/p>
//涓嬮潰鏄疉rrayList鐨勬墿瀹规満鍒?//ArrayList鐨勬墿瀹规満鍒舵彁楂樹簡鎬ц兘锛屽鏋滄瘡娆″彧鎵╁厖涓€涓紝 //閭d箞棰戠箒鐨勬彃鍏ヤ細瀵艰嚧棰戠箒鐨勬嫹璐濓紝闄嶄綆鎬ц兘锛岃€孉rrayList鐨勬墿瀹规満鍒堕伩鍏嶄簡杩欑鎯呭喌銆? /** * 濡傛湁蹇呰锛屽鍔犳ArrayList瀹炰緥鐨勫閲忥紝浠ョ‘淇濆畠鑷冲皯鑳藉绾冲厓绱犵殑鏁伴噺 * @param minCapacity 鎵€闇€鐨勬渶灏忓閲? */ public void ensureCapacity(int minCapacity) { int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } //寰楀埌鏈€灏忔墿瀹归噺 private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { // 鑾峰彇榛樿鐨勫閲忓拰浼犲叆鍙傛暟鐨勮緝澶у€? minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } //鍒ゆ柇鏄惁闇€瑕佹墿瀹?涓婇潰涓や釜鏂规硶閮借璋冪敤 private void ensureExplicitCapacity(int minCapacity) { modCount++; // 濡傛灉璇磎inCapacity涔熷氨鏄墍闇€鐨勬渶灏忓閲忓ぇ浜庝繚瀛楢rrayList鏁版嵁鐨勬暟缁勭殑闀垮害鐨勮瘽锛屽氨闇€瑕佽皟鐢╣row(minCapacity)鏂规硶鎵╁銆? //杩欎釜minCapacity鍒板簳涓哄灏戝憿锛熶妇涓緥瀛愬湪娣诲姞鍏冪礌(add)鏂规硶涓繖涓猰inCapacity鐨勫ぇ灏忓氨涓虹幇鍦ㄦ暟缁勭殑闀垮害鍔? if (minCapacity - elementData.length > 0) //璋冪敤grow鏂规硶杩涜鎵╁锛岃皟鐢ㄦ鏂规硶浠h〃宸茬粡寮€濮嬫墿瀹逛簡 grow(minCapacity); }
/** * ArrayList鎵╁鐨勬牳蹇冩柟娉曘€? */ private void grow(int minCapacity) { //elementData涓轰繚瀛楢rrayList鏁版嵁鐨勬暟缁? ///elementData.length姹傛暟缁勯暱搴lementData.size鏄眰鏁扮粍涓殑鍏冪礌涓暟 // oldCapacity涓烘棫瀹归噺锛宯ewCapacity涓烘柊瀹归噺 int oldCapacity = elementData.length; //灏唎ldCapacity 鍙崇Щ涓€浣嶏紝鍏舵晥鏋滅浉褰撲簬oldCapacity /2锛? //鎴戜滑鐭ラ亾浣嶈繍绠楃殑閫熷害杩滆繙蹇簬鏁撮櫎杩愮畻锛屾暣鍙ヨ繍绠楀紡鐨勭粨鏋滃氨鏄皢鏂板閲忔洿鏂颁负鏃у閲忕殑1.5鍊嶏紝 int newCapacity = oldCapacity + (oldCapacity >> 1); //鐒跺悗妫€鏌ユ柊瀹归噺鏄惁澶т簬鏈€灏忛渶瑕佸閲忥紝鑻ヨ繕鏄皬浜庢渶灏忛渶瑕佸閲忥紝閭d箞灏辨妸鏈€灏忛渶瑕佸閲忓綋浣滄暟缁勭殑鏂板閲忥紝 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; //鍐嶆鏌ユ柊瀹归噺鏄惁瓒呭嚭浜咥rrayList鎵€瀹氫箟鐨勬渶澶у閲忥紝 //鑻ヨ秴鍑轰簡锛屽垯璋冪敤hugeCapacity()鏉ユ瘮杈僲inCapacity鍜?MAX_ARRAY_SIZE锛? //濡傛灉minCapacity澶т簬MAX_ARRAY_SIZE锛屽垯鏂板閲忓垯涓篒nterger.MAX_VALUE锛屽惁鍒欙紝鏂板閲忓ぇ灏忓垯涓?MAX_ARRAY_SIZE銆? 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); }
銆€銆€鎵╁鏈哄埗浠g爜宸茬粡鍋氫簡璇︾粏鐨勮В閲娿€傚彟澶栧€煎緱娉ㄦ剰鐨勬槸澶у寰堝鏄撳拷鐣ョ殑涓€涓繍绠楃锛?strong>绉讳綅杩愮畻绗?/strong>
銆€銆€绠€浠?/strong>锛氱Щ浣嶈繍绠楃灏辨槸鍦ㄤ簩杩涘埗鐨勫熀纭€涓婂鏁板瓧杩涜骞崇Щ銆傛寜鐓у钩绉荤殑鏂瑰悜鍜屽~鍏呮暟瀛楃殑瑙勫垯鍒嗕负涓夌:<<(宸︾Щ)銆?font color="red">>>(甯︾鍙峰彸绉?鍜?font color="red">>>>(鏃犵鍙峰彸绉?銆?br /> 銆€銆€浣滅敤锛?strong>瀵逛簬澶ф暟鎹殑2杩涘埗杩愮畻,浣嶇Щ杩愮畻绗︽瘮閭d簺鏅€氳繍绠楃鐨勮繍绠楄蹇緢澶?鍥犱负绋嬪簭浠呬粎绉诲姩涓€涓嬭€屽凡,涓嶅幓璁$畻,杩欐牱鎻愰珮浜嗘晥鐜?鑺傜渷浜嗚祫婧?/strong>
銆€銆€姣斿杩欓噷锛歩nt newCapacity = oldCapacity + (oldCapacity >> 1);
鍙崇Щ涓€浣嶇浉褰撲簬闄?锛屽彸绉籲浣嶇浉褰撲簬闄や互 2 鐨?n 娆℃柟銆傝繖閲?oldCapacity 鏄庢樉鍙崇Щ浜?浣嶆墍浠ョ浉褰撲簬oldCapacity /2銆?/p>鍙﹀闇€瑕佹敞鎰忕殑鏄細
java 涓殑length 灞炴€?/strong>鏄拡瀵规暟缁勮鐨?姣斿璇翠綘澹版槑浜嗕竴涓暟缁?鎯崇煡閬撹繖涓暟缁勭殑闀垮害鍒欑敤鍒颁簡 length 杩欎釜灞炴€?
java 涓殑length()鏂规硶鏄拡瀵瑰瓧 绗︿覆String璇寸殑,濡傛灉鎯崇湅杩欎釜瀛楃涓茬殑闀垮害鍒欑敤鍒?length()杩欎釜鏂规硶.
.java 涓殑size()鏂规硶鏄拡瀵规硾鍨嬮泦鍚堣鐨?濡傛灉鎯崇湅杩欎釜娉涘瀷鏈夊灏戜釜鍏冪礌,灏辫皟鐢ㄦ鏂规硶鏉ユ煡鐪?
(1)private class Itr implements Iterator<E> (2)private class ListItr extends Itr implements ListIterator<E> (3)private class SubList extends AbstractList<E> implements RandomAccess (4)static final class ArrayListSpliterator<E> implements Spliterator<E>
銆€銆€ArrayList鏈夊洓涓唴閮ㄧ被锛屽叾涓殑Itr鏄疄鐜颁簡Iterator鎺ュ彛锛屽悓鏃堕噸鍐欎簡閲岄潰鐨?strong>hasNext()
锛?next()锛?remove() 绛夋柟娉曪紱鍏朵腑鐨?strong>ListItr
ArrayList缁忓吀Demo
package list;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] srgs){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
System.out.printf("Before add:arrayList.size() = %d
",arrayList.size());
arrayList.add(1);
arrayList.add(3);
arrayList.add(5);
arrayList.add(7);
arrayList.add(9);
System.out.printf("After add:arrayList.size() = %d
",arrayList.size());
System.out.println("Printing elements of arrayList");
// 涓夌閬嶅巻鏂瑰紡鎵撳嵃鍏冪礌
// 绗竴绉嶏細閫氳繃杩唬鍣ㄩ亶鍘? System.out.print("閫氳繃杩唬鍣ㄩ亶鍘?");
Iterator<Integer> it = arrayList.iterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
System.out.println();
// 绗簩绉嶏細閫氳繃绱㈠紩鍊奸亶鍘? System.out.print("閫氳繃绱㈠紩鍊奸亶鍘?");
for(int i = 0; i < arrayList.size(); i++){
System.out.print(arrayList.get(i) + " ");
}
System.out.println();
// 绗笁绉嶏細for寰幆閬嶅巻
System.out.print("for寰幆閬嶅巻:");
for(Integer number : arrayList){
System.out.print(number + " ");
}
// toArray鐢ㄦ硶
// 绗竴绉嶆柟寮?鏈€甯哥敤)
Integer[] integer = arrayList.toArray(new Integer[0]);
// 绗簩绉嶆柟寮?瀹规槗鐞嗚В)
Integer[] integer1 = new Integer[arrayList.size()];
arrayList.toArray(integer1);
// 鎶涘嚭寮傚父锛宩ava涓嶆敮鎸佸悜涓嬭浆鍨? //Integer[] integer2 = new Integer[arrayList.size()];
//integer2 = arrayList.toArray();
System.out.println();
// 鍦ㄦ寚瀹氫綅缃坊鍔犲厓绱? arrayList.add(2,2);
// 鍒犻櫎鎸囧畾浣嶇疆涓婄殑鍏冪礌
arrayList.remove(2);
// 鍒犻櫎鎸囧畾鍏冪礌
arrayList.remove((Object)3);
// 鍒ゆ柇arrayList鏄惁鍖呭惈5
System.out.println("ArrayList contains 5 is: " + arrayList.contains(5));
// 娓呯┖ArrayList
arrayList.clear();
// 鍒ゆ柇ArrayList鏄惁涓虹┖
System.out.println("ArrayList is empty: " + arrayList.isEmpty());
}
}
!-->以上是关于ArrayList婧愮爜鍒嗘瀽的主要内容,如果未能解决你的问题,请参考以下文章