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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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


 281      */
 282     public int lastIndexOf(Object o) {
 283         if (o == null) {
 284             for (int i = size-1; i >= 0; i--)
 285                 if (elementData[i]==null)
 286                     return i;
 287         } else {
 288             for (int i = size-1; i >= 0; i--)
 289                 if (o.equals(elementData[i]))
 290                     return i;
 291         }
 292         return -1;
 293     }
 294 
 295     /**
 296      * Returns a shallow copy of this <tt>ArrayList</tt> instance.  (The
 297      * elements themselves are not copied.)
 298      *
 299      * @return a clone of this <tt>ArrayList</tt> instance
 300      */
 301     public Object clone() {

 302         try {
 303             @SuppressWarnings("unchecked")
 304                 ArrayList<E> v = (ArrayList<E>) super.clone();
 305             v.elementData = Arrays.copyOf(elementData, size);
 306             v.modCount = 0;
 307             return v;
 308         } catch (CloneNotSupportedException e) {
 309             // this shouldn't happen, since we are Cloneable
 310             throw new InternalError(e);
 311         }
 312     }
 313 
 314     /**
 315      * Returns an array containing all of the elements in this list
 316      * in proper sequence (from first to last element).
 317      *
 318      * <p>The returned array will be "safe" in that no references to it are
 319      * maintained by this list.  (In other words, this method must allocate
 320      * a new array).  The caller is thus free to modify the returned array.
 321      *


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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


 281      */
 282     public int lastIndexOf(Object o) {
 283         if (o == null) {
 284             for (int i = size-1; i >= 0; i--)
 285                 if (elementData[i]==null)
 286                     return i;
 287         } else {
 288             for (int i = size-1; i >= 0; i--)
 289                 if (o.equals(elementData[i]))
 290                     return i;
 291         }
 292         return -1;
 293     }
 294 
 295     /**
 296      * Returns a shallow copy of this <tt>ArrayList</tt> instance.  (The
 297      * elements themselves are not copied.)
 298      *
 299      * @return a clone of this <tt>ArrayList</tt> instance
 300      */
 301     @Override
 302     public ArrayList<E> clone() {
 303         try {
 304             @SuppressWarnings("unchecked")
 305                 ArrayList<E> v = (ArrayList<E>) super.clone();
 306             v.elementData = Arrays.copyOf(elementData, size);
 307             v.modCount = 0;
 308             return v;
 309         } catch (CloneNotSupportedException e) {
 310             // this shouldn't happen, since we are Cloneable
 311             throw new InternalError(e);
 312         }
 313     }
 314 
 315     /**
 316      * Returns an array containing all of the elements in this list
 317      * in proper sequence (from first to last element).
 318      *
 319      * <p>The returned array will be "safe" in that no references to it are
 320      * maintained by this list.  (In other words, this method must allocate
 321      * a new array).  The caller is thus free to modify the returned array.
 322      *