int indexOfRange(Object o, int start, int end) {
Object[] es = elementData; if (o == null) { for (int i = start; i < end; i++) { if (es[i] == null) { return i;
}
}
} else { for (int i = start; i < end; i++) { if (o.equals(es[i])) { return i;
}
}
} return -1;
}
int lastIndexOfRange(Object o, int start, int end) {
Object[] es = elementData; if (o == null) { for (int i = end - 1; i >= start; i--) { if (es[i] == null) { return i;
}
}
} else { for (int i = end - 1; i >= start; i--) { if (o.equals(es[i])) { return i;
}
}
} return -1;
}
/** *Returnsashallowcopyofthis{@codeArrayList}instance.(The *elementsthemselvesarenotcopied.) * *@returnacloneofthis{@codeArrayList}instance
*/ public Object clone() { try {
ArrayList<?> v = (ArrayList<?>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0; return v;
} catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable thrownew InternalError(e);
}
}
/** *Insertsthespecifiedelementatthespecifiedpositioninthis *list.Shiftstheelementcurrentlyatthatposition(ifany)and *anysubsequentelementstotheright(addsonetotheirindices). * *@paramindexindexatwhichthespecifiedelementistobeinserted *@paramelementelementtobeinserted *@throwsIndexOutOfBoundsException{@inheritDoc}
*/ publicvoid add(int index, E element) {
rangeCheckForAdd(index);
modCount++; finalint s;
Object[] elementData; if ((s = size) == (elementData = this.elementData).length)
elementData = grow();
System.arraycopy(elementData, index,
elementData, index + 1,
s - index);
elementData[index] = element;
size = s + 1;
}
/** *Removestheelementatthespecifiedpositioninthislist. *Shiftsanysubsequentelementstotheleft(subtractsonefromtheir *indices). * *@paramindextheindexoftheelementtoberemoved *@returntheelementthatwasremovedfromthelist *@throwsIndexOutOfBoundsException{@inheritDoc}
*/ public E remove(int index) {
Objects.checkIndex(index, size); final Object[] es = elementData;
@SuppressWarnings("unchecked") E oldValue = (E) es[index];
fastRemove(es, index);
return oldValue;
}
/** *{@inheritDoc}
*/ publicboolean equals(Object o) { if (o == this) { returntrue;
}
if (!(o instanceof List)) { returnfalse;
}
finalint expectedModCount = modCount; // ArrayList can be subclassed and given arbitrary behavior, but we can // still deal with the common case where o is ArrayList precisely boolean equal = (o.getClass() == ArrayList.class)
? equalsArrayList((ArrayList<?>) o)
: equalsRange((List<?>) o, 0, size);
boolean equalsRange(List<?> other, int from, int to) { final Object[] es = elementData; if (to > es.length) { thrownew ConcurrentModificationException();
} var oit = other.iterator(); for (; from < to; from++) { if (!oit.hasNext() || !Objects.equals(es[from], oit.next())) { returnfalse;
}
} return !oit.hasNext();
}
privateboolean equalsArrayList(ArrayList<?> other) { finalint otherModCount = other.modCount; finalint s = size; boolean equal; if (equal = (s == other.size)) { final Object[] otherEs = other.elementData; final Object[] es = elementData; if (s > es.length || s > otherEs.length) { thrownew ConcurrentModificationException();
} for (int i = 0; i < s; i++) { if (!Objects.equals(es[i], otherEs[i])) {
equal = false; break;
}
}
}
other.checkForComodification(otherModCount); return equal;
}
/** *{@inheritDoc}
*/ publicint hashCode() { int expectedModCount = modCount; int hash = hashCodeRange(0, size);
checkForComodification(expectedModCount); return hash;
}
int hashCodeRange(int from, int to) { final Object[] es = elementData; if (to > es.length) { thrownew ConcurrentModificationException();
} int hashCode = 1; for (int i = from; i < to; i++) {
Object e = es[i];
hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());
} return hashCode;
}
/** *Removesthefirstoccurrenceofthespecifiedelementfromthislist, *ifitispresent.Ifthelistdoesnotcontaintheelement,itis *unchanged.Moreformally,removestheelementwiththelowestindex *{@codei}suchthat *{@codeObjects.equals(o,get(i))} *(ifsuchanelementexists).Returns{@codetrue}ifthislist *containedthespecifiedelement(orequivalently,ifthislist *changedasaresultofthecall). * *@paramoelementtoberemovedfromthislist,ifpresent *@return{@codetrue}ifthislistcontainedthespecifiedelement
*/ publicboolean remove(Object o) { final Object[] es = elementData; finalint size = this.size; int i = 0;
found: { if (o == null) { for (; i < size; i++) if (es[i] == null) break found;
} else { for (; i < size; i++) if (o.equals(es[i])) break found;
} returnfalse;
}
fastRemove(es, i); returntrue;
}
/** *Privateremovemethodthatskipsboundscheckinganddoesnot *returnthevalueremoved.
*/ privatevoid fastRemove(Object[] es, int i) {
modCount++; finalint newSize; if ((newSize = size - 1) > i)
System.arraycopy(es, i + 1, es, i, newSize - i);
es[size = newSize] = null;
}
/** *Removesalloftheelementsfromthislist.Thelistwill *beemptyafterthiscallreturns.
*/ publicvoid clear() {
modCount++; final Object[] es = elementData; for (int to = size, i = size = 0; i < to; i++)
es[i] = null;
}
/** *Appendsalloftheelementsinthespecifiedcollectiontotheendof *thislist,intheorderthattheyarereturnedbythe *specifiedcollection'sIterator.Thebehaviorofthisoperationis *undefinedifthespecifiedcollectionismodifiedwhiletheoperation *isinprogress.(Thisimpliesthatthebehaviorofthiscallis *undefinedifthespecifiedcollectionisthislist,andthis *listisnonempty.) * *@paramccollectioncontainingelementstobeaddedtothislist *@return{@codetrue}ifthislistchangedasaresultofthecall *@throwsNullPointerExceptionifthespecifiedcollectionisnull
*/ publicboolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
modCount++; int numNew = a.length; if (numNew == 0) returnfalse;
Object[] elementData; finalint s; if (numNew > (elementData = this.elementData).length - (s = size))
elementData = grow(s + numNew);
System.arraycopy(a, 0, elementData, s, numNew);
size = s + numNew; returntrue;
}
/** Erases the gap from lo to hi, by sliding down following elements. */ privatevoid shiftTailOverGap(Object[] es, int lo, int hi) {
System.arraycopy(es, hi, es, lo, size - hi); for (int to = size, i = (size -= hi - lo); i < to; i++)
es[i] = null;
}
/** *AversionofrangeCheckusedbyaddandaddAll.
*/ privatevoid rangeCheckForAdd(int index) { if (index > size || index < 0) thrownew IndexOutOfBoundsException(outOfBoundsMsg(index));
}
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in capacity
s.readInt(); // ignored
if (size > 0) { // like clone(), allocate array based upon size not capacity
SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
Object[] elements = new Object[size];
// Read in all elements in the proper order. for (int i = 0; i < size; i++) {
elements[i] = s.readObject();
}
/** *AnoptimizedversionofAbstractList.Itr
*/ privateclass Itr implements Iterator<E> { int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount;
// prevent creating a synthetic constructor
Itr() {}
@SuppressWarnings("unchecked") public E next() {
checkForComodification(); int i = cursor; if (i >= size) thrownew NoSuchElementException();
Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException();
cursor = i + 1; return (E) elementData[lastRet = i];
}
publicvoid remove() { if (lastRet < 0) thrownew IllegalStateException();
checkForComodification();
@Override publicvoid forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action); finalint size = ArrayList.this.size; int i = cursor; if (i < size) { final Object[] es = elementData; if (i >= es.length) thrownew ConcurrentModificationException(); for (; i < size && modCount == expectedModCount; i++)
action.accept(elementAt(es, i)); // update once at end to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
}
@SuppressWarnings("unchecked") public E previous() {
checkForComodification(); int i = cursor - 1; if (i < 0) thrownew NoSuchElementException();
Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException();
cursor = i; return (E) elementData[lastRet = i];
}
public E remove(int index) {
Objects.checkIndex(index, size);
checkForComodification();
E result = root.remove(offset + index);
updateSizeAndModCount(-1); return result;
}
@SuppressWarnings("unchecked") public E next() {
checkForComodification(); int i = cursor; if (i >= SubList.this.size) thrownew NoSuchElementException();
Object[] elementData = root.elementData; if (offset + i >= elementData.length) thrownew ConcurrentModificationException();
cursor = i + 1; return (E) elementData[offset + (lastRet = i)];
}
@SuppressWarnings("unchecked") public E previous() {
checkForComodification(); int i = cursor - 1; if (i < 0) thrownew NoSuchElementException();
Object[] elementData = root.elementData; if (offset + i >= elementData.length) thrownew ConcurrentModificationException();
cursor = i; return (E) elementData[offset + (lastRet = i)];
}
publicvoid forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action); finalint size = SubList.this.size; int i = cursor; if (i < size) { final Object[] es = root.elementData; if (offset + i >= es.length) thrownew ConcurrentModificationException(); for (; i < size && root.modCount == expectedModCount; i++)
action.accept(elementAt(es, offset + i)); // update once at end to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
}
publicint nextIndex() { return cursor;
}
publicint previousIndex() { return cursor - 1;
}
publicvoid remove() { if (lastRet < 0) thrownew IllegalStateException();
checkForComodification();
public Spliterator<E> spliterator() {
checkForComodification();
// ArrayListSpliterator not used here due to late-binding returnnew Spliterator<E>() { privateint index = offset; // current index, modified on advance/split privateint fence = -1; // -1 until used; then one past last index privateint expectedModCount; // initialized when fence set
privateint getFence() { // initialize fence to size on first use int hi; // (a specialized variant appears in method forEach) if ((hi = fence) < 0) {
expectedModCount = modCount;
hi = fence = offset + size;
} return hi;
}
public ArrayList<E>.ArrayListSpliterator trySplit() { int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; // ArrayListSpliterator can be used here as the source is already bound return (lo >= mid) ? null : // divide range in half unless too small
root.new ArrayListSpliterator(lo, index = mid, expectedModCount);
}
publicboolean tryAdvance(Consumer<? super E> action) {
Objects.requireNonNull(action); int hi = getFence(), i = index; if (i < hi) {
index = i + 1;
@SuppressWarnings("unchecked") E e = (E)root.elementData[i];
action.accept(e); if (root.modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
} returnfalse;
}
publicvoid forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action); int i, hi, mc; // hoist accesses and checks from loop
ArrayList<E> lst = root;
Object[] a; if ((a = lst.elementData) != null) { if ((hi = fence) < 0) {
mc = modCount;
hi = offset + size;
} else
mc = expectedModCount; if ((i = index) >= 0 && (index = hi) <= a.length) { for (; i < hi; ++i) {
@SuppressWarnings("unchecked") E e = (E) a[i];
action.accept(e);
} if (lst.modCount == mc) return;
}
} thrownew ConcurrentModificationException();
}
privateint index; // current index, modified on advance/split privateint fence; // -1 until used; then one past last index privateint expectedModCount; // initialized when fence set
/** Creates new spliterator covering the given range. */
ArrayListSpliterator(int origin, int fence, int expectedModCount) { this.index = origin; this.fence = fence; this.expectedModCount = expectedModCount;
}
privateint getFence() { // initialize fence to size on first use int hi; // (a specialized variant appears in method forEach) if ((hi = fence) < 0) {
expectedModCount = modCount;
hi = fence = size;
} return hi;
}
public ArrayListSpliterator trySplit() { int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; return (lo >= mid) ? null : // divide range in half unless too small new ArrayListSpliterator(lo, index = mid, expectedModCount);
}
publicboolean tryAdvance(Consumer<? super E> action) { if (action == null) thrownew NullPointerException(); int hi = getFence(), i = index; if (i < hi) {
index = i + 1;
@SuppressWarnings("unchecked") E e = (E)elementData[i];
action.accept(e); if (modCount != expectedModCount) thrownew ConcurrentModificationException(); returntrue;
} returnfalse;
}
publicvoid forEachRemaining(Consumer<? super E> action) { int i, hi, mc; // hoist accesses and checks from loop
Object[] a; if (action == null) thrownew NullPointerException(); if ((a = elementData) != null) { if ((hi = fence) < 0) {
mc = modCount;
hi = size;
} else
mc = expectedModCount; if ((i = index) >= 0 && (index = hi) <= a.length) { for (; i < hi; ++i) {
@SuppressWarnings("unchecked") E e = (E) a[i];
action.accept(e);
} if (modCount == mc) return;
}
} thrownew ConcurrentModificationException();
}
/** *Removesallelementssatisfyingthegivenpredicate,fromindex *i(inclusive)toindexend(exclusive).
*/ boolean removeIf(Predicate<? super E> filter, int i, finalint end) {
Objects.requireNonNull(filter); int expectedModCount = modCount; final Object[] es = elementData; // Optimize for initial run of survivors for (; i < end && !filter.test(elementAt(es, i)); i++)
; // Tolerate predicates that reentrantly access the collection for // read (but writers still get CME), so traverse once to find // elements to delete, a second pass to physically expunge. if (i < end) { finalint beg = i; finallong[] deathRow = nBits(end - beg);
deathRow[0] = 1L; // set bit 0 for (i = beg + 1; i < end; i++) if (filter.test(elementAt(es, i)))
setBit(deathRow, i - beg); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
modCount++; int w = beg; for (i = beg; i < end; i++) if (isClear(deathRow, i - beg))
es[w++] = es[i];
shiftTailOverGap(es, w, end); returntrue;
} else { if (modCount != expectedModCount) thrownew ConcurrentModificationException(); returnfalse;
}
}
@Override publicvoid replaceAll(UnaryOperator<E> operator) {
replaceAllRange(operator, 0, size); // TODO(8203662): remove increment of modCount from ...
modCount++;
}
privatevoid replaceAllRange(UnaryOperator<E> operator, int i, int end) {
Objects.requireNonNull(operator); finalint expectedModCount = modCount; final Object[] es = elementData; for (; modCount == expectedModCount && i < end; i++)
es[i] = operator.apply(elementAt(es, i)); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
}
@Override
@SuppressWarnings("unchecked") publicvoid sort(Comparator<? super E> c) { finalint expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size, c); if (modCount != expectedModCount) thrownew ConcurrentModificationException();
modCount++;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.