< prev index next >

src/java.base/share/classes/java/util/ArrayList.java

Print this page
rev 48060 : 8060192: Add default method <A> A[] Collection.toArray(IntFunction<A[]> generator)
Reviewed-by: XXX


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Consumer;

  29 import java.util.function.Predicate;
  30 import java.util.function.UnaryOperator;
  31 import jdk.internal.misc.SharedSecrets;
  32 
  33 /**
  34  * Resizable-array implementation of the {@code List} interface.  Implements
  35  * all optional list operations, and permits all elements, including
  36  * {@code null}.  In addition to implementing the {@code List} interface,
  37  * this class provides methods to manipulate the size of the array that is
  38  * used internally to store the list.  (This class is roughly equivalent to
  39  * {@code Vector}, except that it is unsynchronized.)
  40  *
  41  * <p>The {@code size}, {@code isEmpty}, {@code get}, {@code set},
  42  * {@code iterator}, and {@code listIterator} operations run in constant
  43  * time.  The {@code add} operation runs in <i>amortized constant time</i>,
  44  * that is, adding n elements requires O(n) time.  All of the other operations
  45  * run in linear time (roughly speaking).  The constant factor is low compared
  46  * to that for the {@code LinkedList} implementation.
  47  *
  48  * <p>Each {@code ArrayList} instance has a <i>capacity</i>.  The capacity is


 400      * @param a the array into which the elements of the list are to
 401      *          be stored, if it is big enough; otherwise, a new array of the
 402      *          same runtime type is allocated for this purpose.
 403      * @return an array containing the elements of the list
 404      * @throws ArrayStoreException if the runtime type of the specified array
 405      *         is not a supertype of the runtime type of every element in
 406      *         this list
 407      * @throws NullPointerException if the specified array is null
 408      */
 409     @SuppressWarnings("unchecked")
 410     public <T> T[] toArray(T[] a) {
 411         if (a.length < size)
 412             // Make a new array of a's runtime type, but my contents:
 413             return (T[]) Arrays.copyOf(elementData, size, a.getClass());
 414         System.arraycopy(elementData, 0, a, 0, size);
 415         if (a.length > size)
 416             a[size] = null;
 417         return a;
 418     }
 419 







 420     // Positional Access Operations
 421 
 422     @SuppressWarnings("unchecked")
 423     E elementData(int index) {
 424         return (E) elementData[index];
 425     }
 426 
 427     @SuppressWarnings("unchecked")
 428     static <E> E elementAt(Object[] es, int index) {
 429         return (E) es[index];
 430     }
 431 
 432     /**
 433      * Returns the element at the specified position in this list.
 434      *
 435      * @param  index index of the element to return
 436      * @return the element at the specified position in this list
 437      * @throws IndexOutOfBoundsException {@inheritDoc}
 438      */
 439     public E get(int index) {




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.function.Consumer;
  29 import java.util.function.IntFunction;
  30 import java.util.function.Predicate;
  31 import java.util.function.UnaryOperator;
  32 import jdk.internal.misc.SharedSecrets;
  33 
  34 /**
  35  * Resizable-array implementation of the {@code List} interface.  Implements
  36  * all optional list operations, and permits all elements, including
  37  * {@code null}.  In addition to implementing the {@code List} interface,
  38  * this class provides methods to manipulate the size of the array that is
  39  * used internally to store the list.  (This class is roughly equivalent to
  40  * {@code Vector}, except that it is unsynchronized.)
  41  *
  42  * <p>The {@code size}, {@code isEmpty}, {@code get}, {@code set},
  43  * {@code iterator}, and {@code listIterator} operations run in constant
  44  * time.  The {@code add} operation runs in <i>amortized constant time</i>,
  45  * that is, adding n elements requires O(n) time.  All of the other operations
  46  * run in linear time (roughly speaking).  The constant factor is low compared
  47  * to that for the {@code LinkedList} implementation.
  48  *
  49  * <p>Each {@code ArrayList} instance has a <i>capacity</i>.  The capacity is


 401      * @param a the array into which the elements of the list are to
 402      *          be stored, if it is big enough; otherwise, a new array of the
 403      *          same runtime type is allocated for this purpose.
 404      * @return an array containing the elements of the list
 405      * @throws ArrayStoreException if the runtime type of the specified array
 406      *         is not a supertype of the runtime type of every element in
 407      *         this list
 408      * @throws NullPointerException if the specified array is null
 409      */
 410     @SuppressWarnings("unchecked")
 411     public <T> T[] toArray(T[] a) {
 412         if (a.length < size)
 413             // Make a new array of a's runtime type, but my contents:
 414             return (T[]) Arrays.copyOf(elementData, size, a.getClass());
 415         System.arraycopy(elementData, 0, a, 0, size);
 416         if (a.length > size)
 417             a[size] = null;
 418         return a;
 419     }
 420 
 421     // no spec changes relative to supertype
 422     public <T> T[] toArray(IntFunction<T[]> generator) {
 423         T[] a = generator.apply(size);
 424         System.arraycopy(elementData, 0, a, 0, size);
 425         return a;
 426     }
 427 
 428     // Positional Access Operations
 429 
 430     @SuppressWarnings("unchecked")
 431     E elementData(int index) {
 432         return (E) elementData[index];
 433     }
 434 
 435     @SuppressWarnings("unchecked")
 436     static <E> E elementAt(Object[] es, int index) {
 437         return (E) es[index];
 438     }
 439 
 440     /**
 441      * Returns the element at the specified position in this list.
 442      *
 443      * @param  index index of the element to return
 444      * @return the element at the specified position in this list
 445      * @throws IndexOutOfBoundsException {@inheritDoc}
 446      */
 447     public E get(int index) {


< prev index next >