java arraylist 源代码_java中ArrayList的源代码是什么
展開全部
package?java.util;
public?class?ArrayList?extends?AbstractList
implements?List,?RandomAccess,?Cloneable,?java.io.Serializable
{
private?static?final?long?serialVersionUID?=?8683452581122892189L;
/**
*?The?array?buffer?into?which?the?elements?of?the?ArrayList?are?stored.
*?The?capacity?of?the?ArrayList?is?the?length?of?this?array?buffer.
*/
private?transient?E[]?elementData;
/**
*?The?size?of?the?ArrayList?(the?number?of?elements?it?contains).
*
*?@serial
*/
private?int?size;
/**
*?Constructs?an?empty?list?with?the?specified?initial?capacity.
*
*?@param???initialCapacity???the?initial?capacity?of?the?list.
*?@exception?IllegalArgumentException?if?the?specified?initial?capacity
*????????????is?negative
*/
public?ArrayList(int?initialCapacity)?{
super();
if?(initialCapacity?
throw?new?IllegalArgumentException("Illegal?Capacity:?"+
initialCapacity);
this.elementData?=?(E[])new?Object[initialCapacity];
}
public?ArrayList()?{
this(10);
}
public?ArrayList(Collection?extends?E>?c)?{
size?=?c.size();
//?Allow?10%?room?for?growth
int?capacity?=?(int)?Math.min((size*110L)/100,?Integer.MAX_VALUE);
elementData?=?(E[])?c.toArray(new?Object[capacity]);
}
public?void?trimToSize()?{
modCount++;
int?oldCapacity?=?elementData.length;
if?(size?
Object?oldData[]?=?elementData;
elementData?=?(E[])new?Object[size];
System.arraycopy(oldData,?0,?elementData,?0,?size);
}
}
public?void?ensureCapacity(int?minCapacity)?{
modCount++;
int?oldCapacity?=?elementData.length;
if?(minCapacity?>?oldCapacity)?{
Object?oldData[]?=?elementData;
int?newCapacity?=?(oldCapacity?*?3)/2?+?1;
if?(newCapacity?
636f707962616964757a686964616f31333363396332newCapacity?=?minCapacity;
elementData?=?(E[])new?Object[newCapacity];
System.arraycopy(oldData,?0,?elementData,?0,?size);
}
}
public?int?size()?{
return?size;
}
public?boolean?isEmpty()?{
return?size?==?0;
}
public?boolean?contains(Object?elem)?{
return?indexOf(elem)?>=?0;
}
public?int?indexOf(Object?elem)?{
if?(elem?==?null)?{
for?(int?i?=?0;?i?
if?(elementData[i]==null)
return?i;
}?else?{
for?(int?i?=?0;?i?
if?(elem.equals(elementData[i]))
return?i;
}
return?-1;
}
public?int?lastIndexOf(Object?elem)?{
if?(elem?==?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?(elem.equals(elementData[i]))
return?i;
}
return?-1;
}
public?Object?clone()?{
try?{
ArrayList?v?=?(ArrayList)?super.clone();
v.elementData?=?(E[])new?Object[size];
System.arraycopy(elementData,?0,?v.elementData,?0,?size);
v.modCount?=?0;
return?v;
}?catch?(CloneNotSupportedException?e)?{
//?this?shouldn't?happen,?since?we?are?Cloneable
throw?new?InternalError();
}
}
public?Object[]?toArray()?{
Object[]?result?=?new?Object[size];
System.arraycopy(elementData,?0,?result,?0,?size);
return?result;
}
public??T[]?toArray(T[]?a)?{
if?(a.length?
a?=?(T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(),?size);
System.arraycopy(elementData,?0,?a,?0,?size);
if?(a.length?>?size)
a[size]?=?null;
return?a;
}
//?Positional?Access?Operations
public?E?get(int?index)?{
RangeCheck(index);
return?elementData[index];
}
public?E?set(int?index,?E?element)?{
RangeCheck(index);
E?oldValue?=?elementData[index];
elementData[index]?=?element;
return?oldValue;
}
public?boolean?add(E?o)?{
ensureCapacity(size?+?1);??//?Increments?modCount!!
elementData[size++]?=?o;
return?true;
}
public?void?add(int?index,?E?element)?{
if?(index?>?size?||?index?
throw?new?IndexOutOfBoundsException(
"Index:?"+index+",?Size:?"+size);
ensureCapacity(size+1);??//?Increments?modCount!!
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;?//?Let?gc?do?its?work
return?oldValue;
}
public?boolean?remove(Object?o)?{
if?(o?==?null)?{
for?(int?index?=?0;?index?
if?(elementData[index]?==?null)?{
fastRemove(index);
return?true;
}
}?else?{
for?(int?index?=?0;?index?
if?(o.equals(elementData[index]))?{
fastRemove(index);
return?true;
}
}
return?false;
}
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;?//?Let?gc?do?its?work
}
public?void?clear()?{
modCount++;
//?Let?gc?do?its?work
for?(int?i?=?0;?i?
elementData[i]?=?null;
size?=?0;
}
public?boolean?addAll(Collection?extends?E>?c)?{
Object[]?a?=?c.toArray();
int?numNew?=?a.length;
ensureCapacity(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)?{
if?(index?>?size?||?index?
throw?new?IndexOutOfBoundsException(
"Index:?"?+?index?+?",?Size:?"?+?size);
Object[]?a?=?c.toArray();
int?numNew?=?a.length;
ensureCapacity(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;
}
protected?void?removeRange(int?fromIndex,?int?toIndex)?{
modCount++;
int?numMoved?=?size?-?toIndex;
System.arraycopy(elementData,?toIndex,?elementData,?fromIndex,
numMoved);
//?Let?gc?do?its?work
int?newSize?=?size?-?(toIndex-fromIndex);
while?(size?!=?newSize)
elementData[--size]?=?null;
}
private?void?RangeCheck(int?index)?{
if?(index?>=?size)
throw?new?IndexOutOfBoundsException(
"Index:?"+index+",?Size:?"+size);
}
private?void?writeObject(java.io.ObjectOutputStream?s)
throws?java.io.IOException{
int?expectedModCount?=?modCount;
//?Write?out?element?count,?and?any?hidden?stuff
s.defaultWriteObject();
//?Write?out?array?length
s.writeInt(elementData.length);
//?Write?out?all?elements?in?the?proper?order.
for?(int?i=0;?i
s.writeObject(elementData[i]);
if?(modCount?!=?expectedModCount)?{
throw?new?ConcurrentModificationException();
}
}
private?void?readObject(java.io.ObjectInputStream?s)
throws?java.io.IOException,?ClassNotFoundException?{
//?Read?in?size,?and?any?hidden?stuff
s.defaultReadObject();
//?Read?in?array?length?and?allocate?array
int?arrayLength?=?s.readInt();
Object[]?a?=?elementData?=?(E[])new?Object[arrayLength];
//?Read?in?all?elements?in?the?proper?order.
for?(int?i=0;?i
a[i]?=?s.readObject();
}
}
總結
以上是生活随笔為你收集整理的java arraylist 源代码_java中ArrayList的源代码是什么的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荆柯刺秦
- 下一篇: java 持续交付_【Java架构:持续