Could you explain in detail about your comments?
1) Could the two level loops be simplified to one level? Which two levels?
The while loop and the for loop? These levels have been introduced for
clearly demarcating the array resizing and the copy portions.
2) I had the same thought about making the trim method generic, but was also
having a thought on why we should be repeating the code in toArray() to
toArray(T). Moreover, the docs say,
"If this collection fits in the specified array with room to spare (i.e.,
the array has more elements than this collection), the element in the array
immediately following the end of the collection is set to null. (This is
useful in determining the length of this collection only if the caller
knows that this collection does not contain any null elements.) "
If I employ the array resizing technique, then I have to again add logic to
implement the above, because the arrays are resized perfectly as per the
above technique. The patch that I have provided takes care of this, though
it takes an extra copy. I will try to work this out and reduce the copy.
We can commit this patch for now and probably discuss the improvements of
these methods in a separate mail chain.