< prev index next >
src/java.base/share/classes/java/util/ArrayList.java
Print this page
@@ -174,10 +174,15 @@
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
+ if (c instanceof ArrayList) {
+ elementData = new Object[((ArrayList)c).elementData.length];
+ System.arraycopy(((ArrayList)c).elementData, 0, elementData, 0, ((ArrayList)c).elementData.length);
+ size = ((ArrayList)c).size;
+ } else {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// defend against c.toArray (incorrectly) not returning Object[]
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
if (elementData.getClass() != Object[].class)
@@ -185,10 +190,11 @@
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
+ }
/**
* Trims the capacity of this {@code ArrayList} instance to be the
* list's current size. An application can use this operation to minimize
* the storage of an {@code ArrayList} instance.
< prev index next >