Java. util source code: AbstractCollection (based on jdk1.7), java. util. collection
Public Object [] toArray () {// converts the Object to an array, when other threads Delete elements in the Collection and insert data into the Collection, Object [] r = new Object [size ()]; Iterator <E> it = iterator (); for (int I = 0; I <r. length; I ++) {if (! It. hasNext () // when the current thread calls this method, another thread calls this. remove or iterator. remove return Arrays. copyOf (r, I); // The returned array length is I, that is, the actual size r [I] = it. next ();}
// When the current thread calls this method and the other thread calls this. add or iterator. add, it. hasNext is true and finishToArray is executed.
// If no other thread calls add (iterator. add) and remove (iterator. remove), return r
return it.hasNext() ? finishToArray(r, it) : r;
}
// Write the result of toArray execution to the passed Parameter
Public <T> T [] toArray (T [] ){
Int size = size ();
// If the length of the parameter Array is greater than or equal to the Collection capacity, the parameter Array is returned. Otherwise, the new java. lang. reflect. Array is returned, and the size is the Collection capacity.
T [] r = a. length> = size? A:
(T []) java. lang. reflect. Array
. NewInstance (a. getClass (). getComponentType (), size );
Iterator <E> it = iterator ();
For (int I = 0; I <r. length; I ++ ){
If (! It. hasNext () {// when the current thread calls this method, another thread calls this. remove or iterator. remove
If (a = r) {// when the parameter array length> = Collection capacity
R [I] = null; // After meaningful data is written to an array, the followed data is set to null, and the subsequent data remains unchanged. I don't know why this is done, please explain ????? Does it end with a valid array as an identifier?
} Else if (a. length <I ){
Return Arrays. copyOf (r, I );
} Else {
System. arraycopy (r, 0, a, 0, I );
If (a. length> I ){
A [I] = null; // Same principle as assigning a [I] to Null
}
}
return a;
}
r[i] = (T)it.next();
}
// When the current thread calls this method and the other thread calls this. add or iterator. add, it. hasNext is true and finishToArray is executed.
// If no other thread calls add (iterator. add) and remove (iterator. remove), return r
return it.hasNext() ? finishToArray(r, it) : r;
}
private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
// Each time when the index value points to the last element of the array plus 1, the index is scaled at the speed of 1-bit + 1 shifted to the right, for example, 11 to 11 + 11/2 + 1 = 17.
// When the ArrayList add method is executed, when the array length is not enough, it is scaled at the speed of one-bit shift to the right, for example, 11 to 11 + 11/2 = 16
// Why do I add 1 when running toArray ??????
Int newCap = cap + (cap> 1) + 1;
// Overflow-conscious code
If (newCap-MAX_ARRAY_SIZE> 0)
NewCap = hugeCapacity (cap + 1 );
R = Arrays. copyOf (r, newCap );
}
R [I ++] = (T) it. next (); // because the initial I value is r. length, I ++ instead of ++ I
}
// To improve efficiency, when the while execution is complete, if the index value is located at the end of the array + 1, r is directly returned.
// Actually return (I! = R. length? Arrays. copyOf (r, I): r) is more efficient, because I = r. length is not normal, especially when the array is large.
Return (I = r. length )? R: Arrays. copyOf (r, I );
}