< prev index next >

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

Print this page




 159                                                initialCapacity);
 160         }
 161     }
 162 
 163     /**
 164      * Constructs an empty list with an initial capacity of ten.
 165      */
 166     public ArrayList() {
 167         this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
 168     }
 169 
 170     /**
 171      * Constructs a list containing the elements of the specified
 172      * collection, in the order they are returned by the collection's
 173      * iterator.
 174      *
 175      * @param c the collection whose elements are to be placed into this list
 176      * @throws NullPointerException if the specified collection is null
 177      */
 178     public ArrayList(Collection<? extends E> c) {





 179         elementData = c.toArray();
 180         if ((size = elementData.length) != 0) {
 181             // defend against c.toArray (incorrectly) not returning Object[]
 182             // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
 183             if (elementData.getClass() != Object[].class)
 184                 elementData = Arrays.copyOf(elementData, size, Object[].class);
 185         } else {
 186             // replace with empty array.
 187             this.elementData = EMPTY_ELEMENTDATA;

 188         }
 189     }
 190 
 191     /**
 192      * Trims the capacity of this {@code ArrayList} instance to be the
 193      * list's current size.  An application can use this operation to minimize
 194      * the storage of an {@code ArrayList} instance.
 195      */
 196     public void trimToSize() {
 197         modCount++;
 198         if (size < elementData.length) {
 199             elementData = (size == 0)
 200               ? EMPTY_ELEMENTDATA
 201               : Arrays.copyOf(elementData, size);
 202         }
 203     }
 204 
 205     /**
 206      * Increases the capacity of this {@code ArrayList} instance, if
 207      * necessary, to ensure that it can hold at least the number of elements




 159                                                initialCapacity);
 160         }
 161     }
 162 
 163     /**
 164      * Constructs an empty list with an initial capacity of ten.
 165      */
 166     public ArrayList() {
 167         this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
 168     }
 169 
 170     /**
 171      * Constructs a list containing the elements of the specified
 172      * collection, in the order they are returned by the collection's
 173      * iterator.
 174      *
 175      * @param c the collection whose elements are to be placed into this list
 176      * @throws NullPointerException if the specified collection is null
 177      */
 178     public ArrayList(Collection<? extends E> c) {
 179         if (c instanceof ArrayList) {
 180             elementData = new Object[((ArrayList)c).elementData.length];
 181             System.arraycopy(((ArrayList)c).elementData, 0, elementData, 0, ((ArrayList)c).elementData.length);
 182             size = ((ArrayList)c).size;
 183         } else {
 184             elementData = c.toArray();
 185             if ((size = elementData.length) != 0) {
 186                 // defend against c.toArray (incorrectly) not returning Object[]
 187                 // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
 188                 if (elementData.getClass() != Object[].class)
 189                     elementData = Arrays.copyOf(elementData, size, Object[].class);
 190             } else {
 191                 // replace with empty array.
 192                 this.elementData = EMPTY_ELEMENTDATA;
 193             }
 194         }
 195     }
 196 
 197     /**
 198      * Trims the capacity of this {@code ArrayList} instance to be the
 199      * list's current size.  An application can use this operation to minimize
 200      * the storage of an {@code ArrayList} instance.
 201      */
 202     public void trimToSize() {
 203         modCount++;
 204         if (size < elementData.length) {
 205             elementData = (size == 0)
 206               ? EMPTY_ELEMENTDATA
 207               : Arrays.copyOf(elementData, size);
 208         }
 209     }
 210 
 211     /**
 212      * Increases the capacity of this {@code ArrayList} instance, if
 213      * necessary, to ensure that it can hold at least the number of elements


< prev index next >