src/share/classes/java/util/Arrays.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2011, 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


2815 
2816     // Misc
2817 
2818     /**
2819      * Returns a fixed-size list backed by the specified array.  (Changes to
2820      * the returned list "write through" to the array.)  This method acts
2821      * as bridge between array-based and collection-based APIs, in
2822      * combination with {@link Collection#toArray}.  The returned list is
2823      * serializable and implements {@link RandomAccess}.
2824      *
2825      * <p>This method also provides a convenient way to create a fixed-size
2826      * list initialized to contain several elements:
2827      * <pre>
2828      *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
2829      * </pre>
2830      *
2831      * @param a the array by which the list will be backed
2832      * @return a list view of the specified array
2833      */
2834     @SafeVarargs

2835     public static <T> List<T> asList(T... a) {
2836         return new ArrayList<>(a);
2837     }
2838 
2839     /**
2840      * @serial include
2841      */
2842     private static class ArrayList<E> extends AbstractList<E>
2843         implements RandomAccess, java.io.Serializable
2844     {
2845         private static final long serialVersionUID = -2764017481108945198L;
2846         private final E[] a;
2847 
2848         ArrayList(E[] array) {
2849             if (array==null)
2850                 throw new NullPointerException();
2851             a = array;
2852         }
2853 
2854         public int size() {


   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


2815 
2816     // Misc
2817 
2818     /**
2819      * Returns a fixed-size list backed by the specified array.  (Changes to
2820      * the returned list "write through" to the array.)  This method acts
2821      * as bridge between array-based and collection-based APIs, in
2822      * combination with {@link Collection#toArray}.  The returned list is
2823      * serializable and implements {@link RandomAccess}.
2824      *
2825      * <p>This method also provides a convenient way to create a fixed-size
2826      * list initialized to contain several elements:
2827      * <pre>
2828      *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
2829      * </pre>
2830      *
2831      * @param a the array by which the list will be backed
2832      * @return a list view of the specified array
2833      */
2834     @SafeVarargs
2835     @SuppressWarnings("varargs")
2836     public static <T> List<T> asList(T... a) {
2837         return new ArrayList<>(a);
2838     }
2839 
2840     /**
2841      * @serial include
2842      */
2843     private static class ArrayList<E> extends AbstractList<E>
2844         implements RandomAccess, java.io.Serializable
2845     {
2846         private static final long serialVersionUID = -2764017481108945198L;
2847         private final E[] a;
2848 
2849         ArrayList(E[] array) {
2850             if (array==null)
2851                 throw new NullPointerException();
2852             a = array;
2853         }
2854 
2855         public int size() {