8196207: Inefficient ArrayList.subList().toArray()

Reviewed-by: martin, psandoz, jrose, redestad
This commit is contained in:
Sergei Tsypanov 2018-01-30 11:08:50 -08:00 committed by Doug Lea
parent 35a7cc10b7
commit bbb4bcd69d
3 changed files with 124 additions and 34 deletions

View file

@ -1143,6 +1143,23 @@ public class ArrayList<E> extends AbstractList<E>
return modified;
}
public Object[] toArray() {
checkForComodification();
return Arrays.copyOfRange(root.elementData, offset, offset + size);
}
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
checkForComodification();
if (a.length < size)
return (T[]) Arrays.copyOfRange(
root.elementData, offset, offset + size, a.getClass());
System.arraycopy(root.elementData, offset, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
public Iterator<E> iterator() {
return listIterator();
}