emul/compact/src/main/java/java/util/Arrays.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 23:18:15 +0100
branchemul
changeset 564 353102c8cf9f
parent 557 5be31d9fa455
child 568 6bcb6b98155d
permissions -rw-r--r--
Use shared System.arraycopy
     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
    23  * questions.
    24  */
    25 
    26 package java.util;
    27 
    28 import java.lang.reflect.*;
    29 import org.apidesign.bck2brwsr.emul.lang.System;
    30 
    31 /**
    32  * This class contains various methods for manipulating arrays (such as
    33  * sorting and searching). This class also contains a static factory
    34  * that allows arrays to be viewed as lists.
    35  *
    36  * <p>The methods in this class all throw a {@code NullPointerException},
    37  * if the specified array reference is null, except where noted.
    38  *
    39  * <p>The documentation for the methods contained in this class includes
    40  * briefs description of the <i>implementations</i>. Such descriptions should
    41  * be regarded as <i>implementation notes</i>, rather than parts of the
    42  * <i>specification</i>. Implementors should feel free to substitute other
    43  * algorithms, so long as the specification itself is adhered to. (For
    44  * example, the algorithm used by {@code sort(Object[])} does not have to be
    45  * a MergeSort, but it does have to be <i>stable</i>.)
    46  *
    47  * <p>This class is a member of the
    48  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    49  * Java Collections Framework</a>.
    50  *
    51  * @author Josh Bloch
    52  * @author Neal Gafter
    53  * @author John Rose
    54  * @since  1.2
    55  */
    56 public class Arrays {
    57 
    58     // Suppresses default constructor, ensuring non-instantiability.
    59     private Arrays() {}
    60 
    61     /*
    62      * Sorting of primitive type arrays.
    63      */
    64 
    65     /**
    66      * Sorts the specified array into ascending numerical order.
    67      *
    68      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
    69      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
    70      * offers O(n log(n)) performance on many data sets that cause other
    71      * quicksorts to degrade to quadratic performance, and is typically
    72      * faster than traditional (one-pivot) Quicksort implementations.
    73      *
    74      * @param a the array to be sorted
    75      */
    76     public static void sort(int[] a) {
    77         DualPivotQuicksort.sort(a);
    78     }
    79 
    80     /**
    81      * Sorts the specified range of the array into ascending order. The range
    82      * to be sorted extends from the index {@code fromIndex}, inclusive, to
    83      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
    84      * the range to be sorted is empty.
    85      *
    86      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
    87      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
    88      * offers O(n log(n)) performance on many data sets that cause other
    89      * quicksorts to degrade to quadratic performance, and is typically
    90      * faster than traditional (one-pivot) Quicksort implementations.
    91      *
    92      * @param a the array to be sorted
    93      * @param fromIndex the index of the first element, inclusive, to be sorted
    94      * @param toIndex the index of the last element, exclusive, to be sorted
    95      *
    96      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
    97      * @throws ArrayIndexOutOfBoundsException
    98      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
    99      */
   100     public static void sort(int[] a, int fromIndex, int toIndex) {
   101         rangeCheck(a.length, fromIndex, toIndex);
   102         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   103     }
   104 
   105     /**
   106      * Sorts the specified array into ascending numerical order.
   107      *
   108      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   109      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   110      * offers O(n log(n)) performance on many data sets that cause other
   111      * quicksorts to degrade to quadratic performance, and is typically
   112      * faster than traditional (one-pivot) Quicksort implementations.
   113      *
   114      * @param a the array to be sorted
   115      */
   116     public static void sort(long[] a) {
   117         DualPivotQuicksort.sort(a);
   118     }
   119 
   120     /**
   121      * Sorts the specified range of the array into ascending order. The range
   122      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   123      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   124      * the range to be sorted is empty.
   125      *
   126      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   127      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   128      * offers O(n log(n)) performance on many data sets that cause other
   129      * quicksorts to degrade to quadratic performance, and is typically
   130      * faster than traditional (one-pivot) Quicksort implementations.
   131      *
   132      * @param a the array to be sorted
   133      * @param fromIndex the index of the first element, inclusive, to be sorted
   134      * @param toIndex the index of the last element, exclusive, to be sorted
   135      *
   136      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   137      * @throws ArrayIndexOutOfBoundsException
   138      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   139      */
   140     public static void sort(long[] a, int fromIndex, int toIndex) {
   141         rangeCheck(a.length, fromIndex, toIndex);
   142         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   143     }
   144 
   145     /**
   146      * Sorts the specified array into ascending numerical order.
   147      *
   148      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   149      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   150      * offers O(n log(n)) performance on many data sets that cause other
   151      * quicksorts to degrade to quadratic performance, and is typically
   152      * faster than traditional (one-pivot) Quicksort implementations.
   153      *
   154      * @param a the array to be sorted
   155      */
   156     public static void sort(short[] a) {
   157         DualPivotQuicksort.sort(a);
   158     }
   159 
   160     /**
   161      * Sorts the specified range of the array into ascending order. The range
   162      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   163      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   164      * the range to be sorted is empty.
   165      *
   166      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   167      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   168      * offers O(n log(n)) performance on many data sets that cause other
   169      * quicksorts to degrade to quadratic performance, and is typically
   170      * faster than traditional (one-pivot) Quicksort implementations.
   171      *
   172      * @param a the array to be sorted
   173      * @param fromIndex the index of the first element, inclusive, to be sorted
   174      * @param toIndex the index of the last element, exclusive, to be sorted
   175      *
   176      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   177      * @throws ArrayIndexOutOfBoundsException
   178      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   179      */
   180     public static void sort(short[] a, int fromIndex, int toIndex) {
   181         rangeCheck(a.length, fromIndex, toIndex);
   182         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   183     }
   184 
   185     /**
   186      * Sorts the specified array into ascending numerical order.
   187      *
   188      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   189      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   190      * offers O(n log(n)) performance on many data sets that cause other
   191      * quicksorts to degrade to quadratic performance, and is typically
   192      * faster than traditional (one-pivot) Quicksort implementations.
   193      *
   194      * @param a the array to be sorted
   195      */
   196     public static void sort(char[] a) {
   197         DualPivotQuicksort.sort(a);
   198     }
   199 
   200     /**
   201      * Sorts the specified range of the array into ascending order. The range
   202      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   203      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   204      * the range to be sorted is empty.
   205      *
   206      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   207      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   208      * offers O(n log(n)) performance on many data sets that cause other
   209      * quicksorts to degrade to quadratic performance, and is typically
   210      * faster than traditional (one-pivot) Quicksort implementations.
   211      *
   212      * @param a the array to be sorted
   213      * @param fromIndex the index of the first element, inclusive, to be sorted
   214      * @param toIndex the index of the last element, exclusive, to be sorted
   215      *
   216      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   217      * @throws ArrayIndexOutOfBoundsException
   218      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   219      */
   220     public static void sort(char[] a, int fromIndex, int toIndex) {
   221         rangeCheck(a.length, fromIndex, toIndex);
   222         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   223     }
   224 
   225     /**
   226      * Sorts the specified array into ascending numerical order.
   227      *
   228      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   229      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   230      * offers O(n log(n)) performance on many data sets that cause other
   231      * quicksorts to degrade to quadratic performance, and is typically
   232      * faster than traditional (one-pivot) Quicksort implementations.
   233      *
   234      * @param a the array to be sorted
   235      */
   236     public static void sort(byte[] a) {
   237         DualPivotQuicksort.sort(a);
   238     }
   239 
   240     /**
   241      * Sorts the specified range of the array into ascending order. The range
   242      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   243      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   244      * the range to be sorted is empty.
   245      *
   246      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   247      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   248      * offers O(n log(n)) performance on many data sets that cause other
   249      * quicksorts to degrade to quadratic performance, and is typically
   250      * faster than traditional (one-pivot) Quicksort implementations.
   251      *
   252      * @param a the array to be sorted
   253      * @param fromIndex the index of the first element, inclusive, to be sorted
   254      * @param toIndex the index of the last element, exclusive, to be sorted
   255      *
   256      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   257      * @throws ArrayIndexOutOfBoundsException
   258      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   259      */
   260     public static void sort(byte[] a, int fromIndex, int toIndex) {
   261         rangeCheck(a.length, fromIndex, toIndex);
   262         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   263     }
   264 
   265     /**
   266      * Sorts the specified array into ascending numerical order.
   267      *
   268      * <p>The {@code <} relation does not provide a total order on all float
   269      * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
   270      * value compares neither less than, greater than, nor equal to any value,
   271      * even itself. This method uses the total order imposed by the method
   272      * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
   273      * {@code 0.0f} and {@code Float.NaN} is considered greater than any
   274      * other value and all {@code Float.NaN} values are considered equal.
   275      *
   276      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   277      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   278      * offers O(n log(n)) performance on many data sets that cause other
   279      * quicksorts to degrade to quadratic performance, and is typically
   280      * faster than traditional (one-pivot) Quicksort implementations.
   281      *
   282      * @param a the array to be sorted
   283      */
   284     public static void sort(float[] a) {
   285         DualPivotQuicksort.sort(a);
   286     }
   287 
   288     /**
   289      * Sorts the specified range of the array into ascending order. The range
   290      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   291      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   292      * the range to be sorted is empty.
   293      *
   294      * <p>The {@code <} relation does not provide a total order on all float
   295      * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
   296      * value compares neither less than, greater than, nor equal to any value,
   297      * even itself. This method uses the total order imposed by the method
   298      * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
   299      * {@code 0.0f} and {@code Float.NaN} is considered greater than any
   300      * other value and all {@code Float.NaN} values are considered equal.
   301      *
   302      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   303      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   304      * offers O(n log(n)) performance on many data sets that cause other
   305      * quicksorts to degrade to quadratic performance, and is typically
   306      * faster than traditional (one-pivot) Quicksort implementations.
   307      *
   308      * @param a the array to be sorted
   309      * @param fromIndex the index of the first element, inclusive, to be sorted
   310      * @param toIndex the index of the last element, exclusive, to be sorted
   311      *
   312      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   313      * @throws ArrayIndexOutOfBoundsException
   314      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   315      */
   316     public static void sort(float[] a, int fromIndex, int toIndex) {
   317         rangeCheck(a.length, fromIndex, toIndex);
   318         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   319     }
   320 
   321     /**
   322      * Sorts the specified array into ascending numerical order.
   323      *
   324      * <p>The {@code <} relation does not provide a total order on all double
   325      * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
   326      * value compares neither less than, greater than, nor equal to any value,
   327      * even itself. This method uses the total order imposed by the method
   328      * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
   329      * {@code 0.0d} and {@code Double.NaN} is considered greater than any
   330      * other value and all {@code Double.NaN} values are considered equal.
   331      *
   332      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   333      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   334      * offers O(n log(n)) performance on many data sets that cause other
   335      * quicksorts to degrade to quadratic performance, and is typically
   336      * faster than traditional (one-pivot) Quicksort implementations.
   337      *
   338      * @param a the array to be sorted
   339      */
   340     public static void sort(double[] a) {
   341         DualPivotQuicksort.sort(a);
   342     }
   343 
   344     /**
   345      * Sorts the specified range of the array into ascending order. The range
   346      * to be sorted extends from the index {@code fromIndex}, inclusive, to
   347      * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
   348      * the range to be sorted is empty.
   349      *
   350      * <p>The {@code <} relation does not provide a total order on all double
   351      * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
   352      * value compares neither less than, greater than, nor equal to any value,
   353      * even itself. This method uses the total order imposed by the method
   354      * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
   355      * {@code 0.0d} and {@code Double.NaN} is considered greater than any
   356      * other value and all {@code Double.NaN} values are considered equal.
   357      *
   358      * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
   359      * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
   360      * offers O(n log(n)) performance on many data sets that cause other
   361      * quicksorts to degrade to quadratic performance, and is typically
   362      * faster than traditional (one-pivot) Quicksort implementations.
   363      *
   364      * @param a the array to be sorted
   365      * @param fromIndex the index of the first element, inclusive, to be sorted
   366      * @param toIndex the index of the last element, exclusive, to be sorted
   367      *
   368      * @throws IllegalArgumentException if {@code fromIndex > toIndex}
   369      * @throws ArrayIndexOutOfBoundsException
   370      *     if {@code fromIndex < 0} or {@code toIndex > a.length}
   371      */
   372     public static void sort(double[] a, int fromIndex, int toIndex) {
   373         rangeCheck(a.length, fromIndex, toIndex);
   374         DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
   375     }
   376 
   377     /*
   378      * Sorting of complex type arrays.
   379      */
   380 
   381     /**
   382      * Old merge sort implementation can be selected (for
   383      * compatibility with broken comparators) using a system property.
   384      * Cannot be a static boolean in the enclosing class due to
   385      * circular dependencies. To be removed in a future release.
   386      */
   387     static final class LegacyMergeSort {
   388         private static final boolean userRequested =
   389             java.security.AccessController.doPrivileged(
   390                 new sun.security.action.GetBooleanAction(
   391                     "java.util.Arrays.useLegacyMergeSort")).booleanValue();
   392     }
   393 
   394     /*
   395      * If this platform has an optimizing VM, check whether ComparableTimSort
   396      * offers any performance benefit over TimSort in conjunction with a
   397      * comparator that returns:
   398      *    {@code ((Comparable)first).compareTo(Second)}.
   399      * If not, you are better off deleting ComparableTimSort to
   400      * eliminate the code duplication.  In other words, the commented
   401      * out code below is the preferable implementation for sorting
   402      * arrays of Comparables if it offers sufficient performance.
   403      */
   404 
   405 //    /**
   406 //     * A comparator that implements the natural ordering of a group of
   407 //     * mutually comparable elements.  Using this comparator saves us
   408 //     * from duplicating most of the code in this file (one version for
   409 //     * Comparables, one for explicit Comparators).
   410 //     */
   411 //    private static final Comparator<Object> NATURAL_ORDER =
   412 //            new Comparator<Object>() {
   413 //        @SuppressWarnings("unchecked")
   414 //        public int compare(Object first, Object second) {
   415 //            return ((Comparable<Object>)first).compareTo(second);
   416 //        }
   417 //    };
   418 //
   419 //    public static void sort(Object[] a) {
   420 //        sort(a, 0, a.length, NATURAL_ORDER);
   421 //    }
   422 //
   423 //    public static void sort(Object[] a, int fromIndex, int toIndex) {
   424 //        sort(a, fromIndex, toIndex, NATURAL_ORDER);
   425 //    }
   426 
   427     /**
   428      * Sorts the specified array of objects into ascending order, according
   429      * to the {@linkplain Comparable natural ordering} of its elements.
   430      * All elements in the array must implement the {@link Comparable}
   431      * interface.  Furthermore, all elements in the array must be
   432      * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
   433      * not throw a {@code ClassCastException} for any elements {@code e1}
   434      * and {@code e2} in the array).
   435      *
   436      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   437      * not be reordered as a result of the sort.
   438      *
   439      * <p>Implementation note: This implementation is a stable, adaptive,
   440      * iterative mergesort that requires far fewer than n lg(n) comparisons
   441      * when the input array is partially sorted, while offering the
   442      * performance of a traditional mergesort when the input array is
   443      * randomly ordered.  If the input array is nearly sorted, the
   444      * implementation requires approximately n comparisons.  Temporary
   445      * storage requirements vary from a small constant for nearly sorted
   446      * input arrays to n/2 object references for randomly ordered input
   447      * arrays.
   448      *
   449      * <p>The implementation takes equal advantage of ascending and
   450      * descending order in its input array, and can take advantage of
   451      * ascending and descending order in different parts of the the same
   452      * input array.  It is well-suited to merging two or more sorted arrays:
   453      * simply concatenate the arrays and sort the resulting array.
   454      *
   455      * <p>The implementation was adapted from Tim Peters's list sort for Python
   456      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
   457      * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
   458      * Sorting and Information Theoretic Complexity", in Proceedings of the
   459      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
   460      * January 1993.
   461      *
   462      * @param a the array to be sorted
   463      * @throws ClassCastException if the array contains elements that are not
   464      *         <i>mutually comparable</i> (for example, strings and integers)
   465      * @throws IllegalArgumentException (optional) if the natural
   466      *         ordering of the array elements is found to violate the
   467      *         {@link Comparable} contract
   468      */
   469     public static void sort(Object[] a) {
   470         if (LegacyMergeSort.userRequested)
   471             legacyMergeSort(a);
   472         else
   473             ComparableTimSort.sort(a);
   474     }
   475 
   476     /** To be removed in a future release. */
   477     private static void legacyMergeSort(Object[] a) {
   478         Object[] aux = a.clone();
   479         mergeSort(aux, a, 0, a.length, 0);
   480     }
   481 
   482     /**
   483      * Sorts the specified range of the specified array of objects into
   484      * ascending order, according to the
   485      * {@linkplain Comparable natural ordering} of its
   486      * elements.  The range to be sorted extends from index
   487      * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
   488      * (If {@code fromIndex==toIndex}, the range to be sorted is empty.)  All
   489      * elements in this range must implement the {@link Comparable}
   490      * interface.  Furthermore, all elements in this range must be <i>mutually
   491      * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
   492      * {@code ClassCastException} for any elements {@code e1} and
   493      * {@code e2} in the array).
   494      *
   495      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   496      * not be reordered as a result of the sort.
   497      *
   498      * <p>Implementation note: This implementation is a stable, adaptive,
   499      * iterative mergesort that requires far fewer than n lg(n) comparisons
   500      * when the input array is partially sorted, while offering the
   501      * performance of a traditional mergesort when the input array is
   502      * randomly ordered.  If the input array is nearly sorted, the
   503      * implementation requires approximately n comparisons.  Temporary
   504      * storage requirements vary from a small constant for nearly sorted
   505      * input arrays to n/2 object references for randomly ordered input
   506      * arrays.
   507      *
   508      * <p>The implementation takes equal advantage of ascending and
   509      * descending order in its input array, and can take advantage of
   510      * ascending and descending order in different parts of the the same
   511      * input array.  It is well-suited to merging two or more sorted arrays:
   512      * simply concatenate the arrays and sort the resulting array.
   513      *
   514      * <p>The implementation was adapted from Tim Peters's list sort for Python
   515      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
   516      * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
   517      * Sorting and Information Theoretic Complexity", in Proceedings of the
   518      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
   519      * January 1993.
   520      *
   521      * @param a the array to be sorted
   522      * @param fromIndex the index of the first element (inclusive) to be
   523      *        sorted
   524      * @param toIndex the index of the last element (exclusive) to be sorted
   525      * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
   526      *         (optional) if the natural ordering of the array elements is
   527      *         found to violate the {@link Comparable} contract
   528      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
   529      *         {@code toIndex > a.length}
   530      * @throws ClassCastException if the array contains elements that are
   531      *         not <i>mutually comparable</i> (for example, strings and
   532      *         integers).
   533      */
   534     public static void sort(Object[] a, int fromIndex, int toIndex) {
   535         if (LegacyMergeSort.userRequested)
   536             legacyMergeSort(a, fromIndex, toIndex);
   537         else
   538             ComparableTimSort.sort(a, fromIndex, toIndex);
   539     }
   540 
   541     /** To be removed in a future release. */
   542     private static void legacyMergeSort(Object[] a,
   543                                         int fromIndex, int toIndex) {
   544         rangeCheck(a.length, fromIndex, toIndex);
   545         Object[] aux = copyOfRange(a, fromIndex, toIndex);
   546         mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
   547     }
   548 
   549     /**
   550      * Tuning parameter: list size at or below which insertion sort will be
   551      * used in preference to mergesort.
   552      * To be removed in a future release.
   553      */
   554     private static final int INSERTIONSORT_THRESHOLD = 7;
   555 
   556     /**
   557      * Src is the source array that starts at index 0
   558      * Dest is the (possibly larger) array destination with a possible offset
   559      * low is the index in dest to start sorting
   560      * high is the end index in dest to end sorting
   561      * off is the offset to generate corresponding low, high in src
   562      * To be removed in a future release.
   563      */
   564     private static void mergeSort(Object[] src,
   565                                   Object[] dest,
   566                                   int low,
   567                                   int high,
   568                                   int off) {
   569         int length = high - low;
   570 
   571         // Insertion sort on smallest arrays
   572         if (length < INSERTIONSORT_THRESHOLD) {
   573             for (int i=low; i<high; i++)
   574                 for (int j=i; j>low &&
   575                          ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
   576                     swap(dest, j, j-1);
   577             return;
   578         }
   579 
   580         // Recursively sort halves of dest into src
   581         int destLow  = low;
   582         int destHigh = high;
   583         low  += off;
   584         high += off;
   585         int mid = (low + high) >>> 1;
   586         mergeSort(dest, src, low, mid, -off);
   587         mergeSort(dest, src, mid, high, -off);
   588 
   589         // If list is already sorted, just copy from src to dest.  This is an
   590         // optimization that results in faster sorts for nearly ordered lists.
   591         if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
   592             System.arraycopy(src, low, dest, destLow, length);
   593             return;
   594         }
   595 
   596         // Merge sorted halves (now in src) into dest
   597         for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
   598             if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
   599                 dest[i] = src[p++];
   600             else
   601                 dest[i] = src[q++];
   602         }
   603     }
   604 
   605     /**
   606      * Swaps x[a] with x[b].
   607      */
   608     private static void swap(Object[] x, int a, int b) {
   609         Object t = x[a];
   610         x[a] = x[b];
   611         x[b] = t;
   612     }
   613 
   614     /**
   615      * Sorts the specified array of objects according to the order induced by
   616      * the specified comparator.  All elements in the array must be
   617      * <i>mutually comparable</i> by the specified comparator (that is,
   618      * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
   619      * for any elements {@code e1} and {@code e2} in the array).
   620      *
   621      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   622      * not be reordered as a result of the sort.
   623      *
   624      * <p>Implementation note: This implementation is a stable, adaptive,
   625      * iterative mergesort that requires far fewer than n lg(n) comparisons
   626      * when the input array is partially sorted, while offering the
   627      * performance of a traditional mergesort when the input array is
   628      * randomly ordered.  If the input array is nearly sorted, the
   629      * implementation requires approximately n comparisons.  Temporary
   630      * storage requirements vary from a small constant for nearly sorted
   631      * input arrays to n/2 object references for randomly ordered input
   632      * arrays.
   633      *
   634      * <p>The implementation takes equal advantage of ascending and
   635      * descending order in its input array, and can take advantage of
   636      * ascending and descending order in different parts of the the same
   637      * input array.  It is well-suited to merging two or more sorted arrays:
   638      * simply concatenate the arrays and sort the resulting array.
   639      *
   640      * <p>The implementation was adapted from Tim Peters's list sort for Python
   641      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
   642      * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
   643      * Sorting and Information Theoretic Complexity", in Proceedings of the
   644      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
   645      * January 1993.
   646      *
   647      * @param a the array to be sorted
   648      * @param c the comparator to determine the order of the array.  A
   649      *        {@code null} value indicates that the elements'
   650      *        {@linkplain Comparable natural ordering} should be used.
   651      * @throws ClassCastException if the array contains elements that are
   652      *         not <i>mutually comparable</i> using the specified comparator
   653      * @throws IllegalArgumentException (optional) if the comparator is
   654      *         found to violate the {@link Comparator} contract
   655      */
   656     public static <T> void sort(T[] a, Comparator<? super T> c) {
   657         if (LegacyMergeSort.userRequested)
   658             legacyMergeSort(a, c);
   659         else
   660             TimSort.sort(a, c);
   661     }
   662 
   663     /** To be removed in a future release. */
   664     private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
   665         T[] aux = a.clone();
   666         if (c==null)
   667             mergeSort(aux, a, 0, a.length, 0);
   668         else
   669             mergeSort(aux, a, 0, a.length, 0, c);
   670     }
   671 
   672     /**
   673      * Sorts the specified range of the specified array of objects according
   674      * to the order induced by the specified comparator.  The range to be
   675      * sorted extends from index {@code fromIndex}, inclusive, to index
   676      * {@code toIndex}, exclusive.  (If {@code fromIndex==toIndex}, the
   677      * range to be sorted is empty.)  All elements in the range must be
   678      * <i>mutually comparable</i> by the specified comparator (that is,
   679      * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
   680      * for any elements {@code e1} and {@code e2} in the range).
   681      *
   682      * <p>This sort is guaranteed to be <i>stable</i>:  equal elements will
   683      * not be reordered as a result of the sort.
   684      *
   685      * <p>Implementation note: This implementation is a stable, adaptive,
   686      * iterative mergesort that requires far fewer than n lg(n) comparisons
   687      * when the input array is partially sorted, while offering the
   688      * performance of a traditional mergesort when the input array is
   689      * randomly ordered.  If the input array is nearly sorted, the
   690      * implementation requires approximately n comparisons.  Temporary
   691      * storage requirements vary from a small constant for nearly sorted
   692      * input arrays to n/2 object references for randomly ordered input
   693      * arrays.
   694      *
   695      * <p>The implementation takes equal advantage of ascending and
   696      * descending order in its input array, and can take advantage of
   697      * ascending and descending order in different parts of the the same
   698      * input array.  It is well-suited to merging two or more sorted arrays:
   699      * simply concatenate the arrays and sort the resulting array.
   700      *
   701      * <p>The implementation was adapted from Tim Peters's list sort for Python
   702      * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
   703      * TimSort</a>).  It uses techiques from Peter McIlroy's "Optimistic
   704      * Sorting and Information Theoretic Complexity", in Proceedings of the
   705      * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
   706      * January 1993.
   707      *
   708      * @param a the array to be sorted
   709      * @param fromIndex the index of the first element (inclusive) to be
   710      *        sorted
   711      * @param toIndex the index of the last element (exclusive) to be sorted
   712      * @param c the comparator to determine the order of the array.  A
   713      *        {@code null} value indicates that the elements'
   714      *        {@linkplain Comparable natural ordering} should be used.
   715      * @throws ClassCastException if the array contains elements that are not
   716      *         <i>mutually comparable</i> using the specified comparator.
   717      * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
   718      *         (optional) if the comparator is found to violate the
   719      *         {@link Comparator} contract
   720      * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
   721      *         {@code toIndex > a.length}
   722      */
   723     public static <T> void sort(T[] a, int fromIndex, int toIndex,
   724                                 Comparator<? super T> c) {
   725         if (LegacyMergeSort.userRequested)
   726             legacyMergeSort(a, fromIndex, toIndex, c);
   727         else
   728             TimSort.sort(a, fromIndex, toIndex, c);
   729     }
   730 
   731     /** To be removed in a future release. */
   732     private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
   733                                             Comparator<? super T> c) {
   734         rangeCheck(a.length, fromIndex, toIndex);
   735         T[] aux = copyOfRange(a, fromIndex, toIndex);
   736         if (c==null)
   737             mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
   738         else
   739             mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
   740     }
   741 
   742     /**
   743      * Src is the source array that starts at index 0
   744      * Dest is the (possibly larger) array destination with a possible offset
   745      * low is the index in dest to start sorting
   746      * high is the end index in dest to end sorting
   747      * off is the offset into src corresponding to low in dest
   748      * To be removed in a future release.
   749      */
   750     private static void mergeSort(Object[] src,
   751                                   Object[] dest,
   752                                   int low, int high, int off,
   753                                   Comparator c) {
   754         int length = high - low;
   755 
   756         // Insertion sort on smallest arrays
   757         if (length < INSERTIONSORT_THRESHOLD) {
   758             for (int i=low; i<high; i++)
   759                 for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
   760                     swap(dest, j, j-1);
   761             return;
   762         }
   763 
   764         // Recursively sort halves of dest into src
   765         int destLow  = low;
   766         int destHigh = high;
   767         low  += off;
   768         high += off;
   769         int mid = (low + high) >>> 1;
   770         mergeSort(dest, src, low, mid, -off, c);
   771         mergeSort(dest, src, mid, high, -off, c);
   772 
   773         // If list is already sorted, just copy from src to dest.  This is an
   774         // optimization that results in faster sorts for nearly ordered lists.
   775         if (c.compare(src[mid-1], src[mid]) <= 0) {
   776            System.arraycopy(src, low, dest, destLow, length);
   777            return;
   778         }
   779 
   780         // Merge sorted halves (now in src) into dest
   781         for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
   782             if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
   783                 dest[i] = src[p++];
   784             else
   785                 dest[i] = src[q++];
   786         }
   787     }
   788 
   789     /**
   790      * Checks that {@code fromIndex} and {@code toIndex} are in
   791      * the range and throws an appropriate exception, if they aren't.
   792      */
   793     private static void rangeCheck(int length, int fromIndex, int toIndex) {
   794         if (fromIndex > toIndex) {
   795             throw new IllegalArgumentException(
   796                 "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
   797         }
   798         if (fromIndex < 0) {
   799             throw new ArrayIndexOutOfBoundsException(fromIndex);
   800         }
   801         if (toIndex > length) {
   802             throw new ArrayIndexOutOfBoundsException(toIndex);
   803         }
   804     }
   805 
   806     // Searching
   807 
   808     /**
   809      * Searches the specified array of longs for the specified value using the
   810      * binary search algorithm.  The array must be sorted (as
   811      * by the {@link #sort(long[])} method) prior to making this call.  If it
   812      * is not sorted, the results are undefined.  If the array contains
   813      * multiple elements with the specified value, there is no guarantee which
   814      * one will be found.
   815      *
   816      * @param a the array to be searched
   817      * @param key the value to be searched for
   818      * @return index of the search key, if it is contained in the array;
   819      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
   820      *         <i>insertion point</i> is defined as the point at which the
   821      *         key would be inserted into the array: the index of the first
   822      *         element greater than the key, or <tt>a.length</tt> if all
   823      *         elements in the array are less than the specified key.  Note
   824      *         that this guarantees that the return value will be &gt;= 0 if
   825      *         and only if the key is found.
   826      */
   827     public static int binarySearch(long[] a, long key) {
   828         return binarySearch0(a, 0, a.length, key);
   829     }
   830 
   831     /**
   832      * Searches a range of
   833      * the specified array of longs for the specified value using the
   834      * binary search algorithm.
   835      * The range must be sorted (as
   836      * by the {@link #sort(long[], int, int)} method)
   837      * prior to making this call.  If it
   838      * is not sorted, the results are undefined.  If the range contains
   839      * multiple elements with the specified value, there is no guarantee which
   840      * one will be found.
   841      *
   842      * @param a the array to be searched
   843      * @param fromIndex the index of the first element (inclusive) to be
   844      *          searched
   845      * @param toIndex the index of the last element (exclusive) to be searched
   846      * @param key the value to be searched for
   847      * @return index of the search key, if it is contained in the array
   848      *         within the specified range;
   849      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
   850      *         <i>insertion point</i> is defined as the point at which the
   851      *         key would be inserted into the array: the index of the first
   852      *         element in the range greater than the key,
   853      *         or <tt>toIndex</tt> if all
   854      *         elements in the range are less than the specified key.  Note
   855      *         that this guarantees that the return value will be &gt;= 0 if
   856      *         and only if the key is found.
   857      * @throws IllegalArgumentException
   858      *         if {@code fromIndex > toIndex}
   859      * @throws ArrayIndexOutOfBoundsException
   860      *         if {@code fromIndex < 0 or toIndex > a.length}
   861      * @since 1.6
   862      */
   863     public static int binarySearch(long[] a, int fromIndex, int toIndex,
   864                                    long key) {
   865         rangeCheck(a.length, fromIndex, toIndex);
   866         return binarySearch0(a, fromIndex, toIndex, key);
   867     }
   868 
   869     // Like public version, but without range checks.
   870     private static int binarySearch0(long[] a, int fromIndex, int toIndex,
   871                                      long key) {
   872         int low = fromIndex;
   873         int high = toIndex - 1;
   874 
   875         while (low <= high) {
   876             int mid = (low + high) >>> 1;
   877             long midVal = a[mid];
   878 
   879             if (midVal < key)
   880                 low = mid + 1;
   881             else if (midVal > key)
   882                 high = mid - 1;
   883             else
   884                 return mid; // key found
   885         }
   886         return -(low + 1);  // key not found.
   887     }
   888 
   889     /**
   890      * Searches the specified array of ints for the specified value using the
   891      * binary search algorithm.  The array must be sorted (as
   892      * by the {@link #sort(int[])} method) prior to making this call.  If it
   893      * is not sorted, the results are undefined.  If the array contains
   894      * multiple elements with the specified value, there is no guarantee which
   895      * one will be found.
   896      *
   897      * @param a the array to be searched
   898      * @param key the value to be searched for
   899      * @return index of the search key, if it is contained in the array;
   900      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
   901      *         <i>insertion point</i> is defined as the point at which the
   902      *         key would be inserted into the array: the index of the first
   903      *         element greater than the key, or <tt>a.length</tt> if all
   904      *         elements in the array are less than the specified key.  Note
   905      *         that this guarantees that the return value will be &gt;= 0 if
   906      *         and only if the key is found.
   907      */
   908     public static int binarySearch(int[] a, int key) {
   909         return binarySearch0(a, 0, a.length, key);
   910     }
   911 
   912     /**
   913      * Searches a range of
   914      * the specified array of ints for the specified value using the
   915      * binary search algorithm.
   916      * The range must be sorted (as
   917      * by the {@link #sort(int[], int, int)} method)
   918      * prior to making this call.  If it
   919      * is not sorted, the results are undefined.  If the range contains
   920      * multiple elements with the specified value, there is no guarantee which
   921      * one will be found.
   922      *
   923      * @param a the array to be searched
   924      * @param fromIndex the index of the first element (inclusive) to be
   925      *          searched
   926      * @param toIndex the index of the last element (exclusive) to be searched
   927      * @param key the value to be searched for
   928      * @return index of the search key, if it is contained in the array
   929      *         within the specified range;
   930      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
   931      *         <i>insertion point</i> is defined as the point at which the
   932      *         key would be inserted into the array: the index of the first
   933      *         element in the range greater than the key,
   934      *         or <tt>toIndex</tt> if all
   935      *         elements in the range are less than the specified key.  Note
   936      *         that this guarantees that the return value will be &gt;= 0 if
   937      *         and only if the key is found.
   938      * @throws IllegalArgumentException
   939      *         if {@code fromIndex > toIndex}
   940      * @throws ArrayIndexOutOfBoundsException
   941      *         if {@code fromIndex < 0 or toIndex > a.length}
   942      * @since 1.6
   943      */
   944     public static int binarySearch(int[] a, int fromIndex, int toIndex,
   945                                    int key) {
   946         rangeCheck(a.length, fromIndex, toIndex);
   947         return binarySearch0(a, fromIndex, toIndex, key);
   948     }
   949 
   950     // Like public version, but without range checks.
   951     private static int binarySearch0(int[] a, int fromIndex, int toIndex,
   952                                      int key) {
   953         int low = fromIndex;
   954         int high = toIndex - 1;
   955 
   956         while (low <= high) {
   957             int mid = (low + high) >>> 1;
   958             int midVal = a[mid];
   959 
   960             if (midVal < key)
   961                 low = mid + 1;
   962             else if (midVal > key)
   963                 high = mid - 1;
   964             else
   965                 return mid; // key found
   966         }
   967         return -(low + 1);  // key not found.
   968     }
   969 
   970     /**
   971      * Searches the specified array of shorts for the specified value using
   972      * the binary search algorithm.  The array must be sorted
   973      * (as by the {@link #sort(short[])} method) prior to making this call.  If
   974      * it is not sorted, the results are undefined.  If the array contains
   975      * multiple elements with the specified value, there is no guarantee which
   976      * one will be found.
   977      *
   978      * @param a the array to be searched
   979      * @param key the value to be searched for
   980      * @return index of the search key, if it is contained in the array;
   981      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
   982      *         <i>insertion point</i> is defined as the point at which the
   983      *         key would be inserted into the array: the index of the first
   984      *         element greater than the key, or <tt>a.length</tt> if all
   985      *         elements in the array are less than the specified key.  Note
   986      *         that this guarantees that the return value will be &gt;= 0 if
   987      *         and only if the key is found.
   988      */
   989     public static int binarySearch(short[] a, short key) {
   990         return binarySearch0(a, 0, a.length, key);
   991     }
   992 
   993     /**
   994      * Searches a range of
   995      * the specified array of shorts for the specified value using
   996      * the binary search algorithm.
   997      * The range must be sorted
   998      * (as by the {@link #sort(short[], int, int)} method)
   999      * prior to making this call.  If
  1000      * it is not sorted, the results are undefined.  If the range contains
  1001      * multiple elements with the specified value, there is no guarantee which
  1002      * one will be found.
  1003      *
  1004      * @param a the array to be searched
  1005      * @param fromIndex the index of the first element (inclusive) to be
  1006      *          searched
  1007      * @param toIndex the index of the last element (exclusive) to be searched
  1008      * @param key the value to be searched for
  1009      * @return index of the search key, if it is contained in the array
  1010      *         within the specified range;
  1011      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1012      *         <i>insertion point</i> is defined as the point at which the
  1013      *         key would be inserted into the array: the index of the first
  1014      *         element in the range greater than the key,
  1015      *         or <tt>toIndex</tt> if all
  1016      *         elements in the range are less than the specified key.  Note
  1017      *         that this guarantees that the return value will be &gt;= 0 if
  1018      *         and only if the key is found.
  1019      * @throws IllegalArgumentException
  1020      *         if {@code fromIndex > toIndex}
  1021      * @throws ArrayIndexOutOfBoundsException
  1022      *         if {@code fromIndex < 0 or toIndex > a.length}
  1023      * @since 1.6
  1024      */
  1025     public static int binarySearch(short[] a, int fromIndex, int toIndex,
  1026                                    short key) {
  1027         rangeCheck(a.length, fromIndex, toIndex);
  1028         return binarySearch0(a, fromIndex, toIndex, key);
  1029     }
  1030 
  1031     // Like public version, but without range checks.
  1032     private static int binarySearch0(short[] a, int fromIndex, int toIndex,
  1033                                      short key) {
  1034         int low = fromIndex;
  1035         int high = toIndex - 1;
  1036 
  1037         while (low <= high) {
  1038             int mid = (low + high) >>> 1;
  1039             short midVal = a[mid];
  1040 
  1041             if (midVal < key)
  1042                 low = mid + 1;
  1043             else if (midVal > key)
  1044                 high = mid - 1;
  1045             else
  1046                 return mid; // key found
  1047         }
  1048         return -(low + 1);  // key not found.
  1049     }
  1050 
  1051     /**
  1052      * Searches the specified array of chars for the specified value using the
  1053      * binary search algorithm.  The array must be sorted (as
  1054      * by the {@link #sort(char[])} method) prior to making this call.  If it
  1055      * is not sorted, the results are undefined.  If the array contains
  1056      * multiple elements with the specified value, there is no guarantee which
  1057      * one will be found.
  1058      *
  1059      * @param a the array to be searched
  1060      * @param key the value to be searched for
  1061      * @return index of the search key, if it is contained in the array;
  1062      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1063      *         <i>insertion point</i> is defined as the point at which the
  1064      *         key would be inserted into the array: the index of the first
  1065      *         element greater than the key, or <tt>a.length</tt> if all
  1066      *         elements in the array are less than the specified key.  Note
  1067      *         that this guarantees that the return value will be &gt;= 0 if
  1068      *         and only if the key is found.
  1069      */
  1070     public static int binarySearch(char[] a, char key) {
  1071         return binarySearch0(a, 0, a.length, key);
  1072     }
  1073 
  1074     /**
  1075      * Searches a range of
  1076      * the specified array of chars for the specified value using the
  1077      * binary search algorithm.
  1078      * The range must be sorted (as
  1079      * by the {@link #sort(char[], int, int)} method)
  1080      * prior to making this call.  If it
  1081      * is not sorted, the results are undefined.  If the range contains
  1082      * multiple elements with the specified value, there is no guarantee which
  1083      * one will be found.
  1084      *
  1085      * @param a the array to be searched
  1086      * @param fromIndex the index of the first element (inclusive) to be
  1087      *          searched
  1088      * @param toIndex the index of the last element (exclusive) to be searched
  1089      * @param key the value to be searched for
  1090      * @return index of the search key, if it is contained in the array
  1091      *         within the specified range;
  1092      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1093      *         <i>insertion point</i> is defined as the point at which the
  1094      *         key would be inserted into the array: the index of the first
  1095      *         element in the range greater than the key,
  1096      *         or <tt>toIndex</tt> if all
  1097      *         elements in the range are less than the specified key.  Note
  1098      *         that this guarantees that the return value will be &gt;= 0 if
  1099      *         and only if the key is found.
  1100      * @throws IllegalArgumentException
  1101      *         if {@code fromIndex > toIndex}
  1102      * @throws ArrayIndexOutOfBoundsException
  1103      *         if {@code fromIndex < 0 or toIndex > a.length}
  1104      * @since 1.6
  1105      */
  1106     public static int binarySearch(char[] a, int fromIndex, int toIndex,
  1107                                    char key) {
  1108         rangeCheck(a.length, fromIndex, toIndex);
  1109         return binarySearch0(a, fromIndex, toIndex, key);
  1110     }
  1111 
  1112     // Like public version, but without range checks.
  1113     private static int binarySearch0(char[] a, int fromIndex, int toIndex,
  1114                                      char key) {
  1115         int low = fromIndex;
  1116         int high = toIndex - 1;
  1117 
  1118         while (low <= high) {
  1119             int mid = (low + high) >>> 1;
  1120             char midVal = a[mid];
  1121 
  1122             if (midVal < key)
  1123                 low = mid + 1;
  1124             else if (midVal > key)
  1125                 high = mid - 1;
  1126             else
  1127                 return mid; // key found
  1128         }
  1129         return -(low + 1);  // key not found.
  1130     }
  1131 
  1132     /**
  1133      * Searches the specified array of bytes for the specified value using the
  1134      * binary search algorithm.  The array must be sorted (as
  1135      * by the {@link #sort(byte[])} method) prior to making this call.  If it
  1136      * is not sorted, the results are undefined.  If the array contains
  1137      * multiple elements with the specified value, there is no guarantee which
  1138      * one will be found.
  1139      *
  1140      * @param a the array to be searched
  1141      * @param key the value to be searched for
  1142      * @return index of the search key, if it is contained in the array;
  1143      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1144      *         <i>insertion point</i> is defined as the point at which the
  1145      *         key would be inserted into the array: the index of the first
  1146      *         element greater than the key, or <tt>a.length</tt> if all
  1147      *         elements in the array are less than the specified key.  Note
  1148      *         that this guarantees that the return value will be &gt;= 0 if
  1149      *         and only if the key is found.
  1150      */
  1151     public static int binarySearch(byte[] a, byte key) {
  1152         return binarySearch0(a, 0, a.length, key);
  1153     }
  1154 
  1155     /**
  1156      * Searches a range of
  1157      * the specified array of bytes for the specified value using the
  1158      * binary search algorithm.
  1159      * The range must be sorted (as
  1160      * by the {@link #sort(byte[], int, int)} method)
  1161      * prior to making this call.  If it
  1162      * is not sorted, the results are undefined.  If the range contains
  1163      * multiple elements with the specified value, there is no guarantee which
  1164      * one will be found.
  1165      *
  1166      * @param a the array to be searched
  1167      * @param fromIndex the index of the first element (inclusive) to be
  1168      *          searched
  1169      * @param toIndex the index of the last element (exclusive) to be searched
  1170      * @param key the value to be searched for
  1171      * @return index of the search key, if it is contained in the array
  1172      *         within the specified range;
  1173      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1174      *         <i>insertion point</i> is defined as the point at which the
  1175      *         key would be inserted into the array: the index of the first
  1176      *         element in the range greater than the key,
  1177      *         or <tt>toIndex</tt> if all
  1178      *         elements in the range are less than the specified key.  Note
  1179      *         that this guarantees that the return value will be &gt;= 0 if
  1180      *         and only if the key is found.
  1181      * @throws IllegalArgumentException
  1182      *         if {@code fromIndex > toIndex}
  1183      * @throws ArrayIndexOutOfBoundsException
  1184      *         if {@code fromIndex < 0 or toIndex > a.length}
  1185      * @since 1.6
  1186      */
  1187     public static int binarySearch(byte[] a, int fromIndex, int toIndex,
  1188                                    byte key) {
  1189         rangeCheck(a.length, fromIndex, toIndex);
  1190         return binarySearch0(a, fromIndex, toIndex, key);
  1191     }
  1192 
  1193     // Like public version, but without range checks.
  1194     private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
  1195                                      byte key) {
  1196         int low = fromIndex;
  1197         int high = toIndex - 1;
  1198 
  1199         while (low <= high) {
  1200             int mid = (low + high) >>> 1;
  1201             byte midVal = a[mid];
  1202 
  1203             if (midVal < key)
  1204                 low = mid + 1;
  1205             else if (midVal > key)
  1206                 high = mid - 1;
  1207             else
  1208                 return mid; // key found
  1209         }
  1210         return -(low + 1);  // key not found.
  1211     }
  1212 
  1213     /**
  1214      * Searches the specified array of doubles for the specified value using
  1215      * the binary search algorithm.  The array must be sorted
  1216      * (as by the {@link #sort(double[])} method) prior to making this call.
  1217      * If it is not sorted, the results are undefined.  If the array contains
  1218      * multiple elements with the specified value, there is no guarantee which
  1219      * one will be found.  This method considers all NaN values to be
  1220      * equivalent and equal.
  1221      *
  1222      * @param a the array to be searched
  1223      * @param key the value to be searched for
  1224      * @return index of the search key, if it is contained in the array;
  1225      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1226      *         <i>insertion point</i> is defined as the point at which the
  1227      *         key would be inserted into the array: the index of the first
  1228      *         element greater than the key, or <tt>a.length</tt> if all
  1229      *         elements in the array are less than the specified key.  Note
  1230      *         that this guarantees that the return value will be &gt;= 0 if
  1231      *         and only if the key is found.
  1232      */
  1233     public static int binarySearch(double[] a, double key) {
  1234         return binarySearch0(a, 0, a.length, key);
  1235     }
  1236 
  1237     /**
  1238      * Searches a range of
  1239      * the specified array of doubles for the specified value using
  1240      * the binary search algorithm.
  1241      * The range must be sorted
  1242      * (as by the {@link #sort(double[], int, int)} method)
  1243      * prior to making this call.
  1244      * If it is not sorted, the results are undefined.  If the range contains
  1245      * multiple elements with the specified value, there is no guarantee which
  1246      * one will be found.  This method considers all NaN values to be
  1247      * equivalent and equal.
  1248      *
  1249      * @param a the array to be searched
  1250      * @param fromIndex the index of the first element (inclusive) to be
  1251      *          searched
  1252      * @param toIndex the index of the last element (exclusive) to be searched
  1253      * @param key the value to be searched for
  1254      * @return index of the search key, if it is contained in the array
  1255      *         within the specified range;
  1256      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1257      *         <i>insertion point</i> is defined as the point at which the
  1258      *         key would be inserted into the array: the index of the first
  1259      *         element in the range greater than the key,
  1260      *         or <tt>toIndex</tt> if all
  1261      *         elements in the range are less than the specified key.  Note
  1262      *         that this guarantees that the return value will be &gt;= 0 if
  1263      *         and only if the key is found.
  1264      * @throws IllegalArgumentException
  1265      *         if {@code fromIndex > toIndex}
  1266      * @throws ArrayIndexOutOfBoundsException
  1267      *         if {@code fromIndex < 0 or toIndex > a.length}
  1268      * @since 1.6
  1269      */
  1270     public static int binarySearch(double[] a, int fromIndex, int toIndex,
  1271                                    double key) {
  1272         rangeCheck(a.length, fromIndex, toIndex);
  1273         return binarySearch0(a, fromIndex, toIndex, key);
  1274     }
  1275 
  1276     // Like public version, but without range checks.
  1277     private static int binarySearch0(double[] a, int fromIndex, int toIndex,
  1278                                      double key) {
  1279         int low = fromIndex;
  1280         int high = toIndex - 1;
  1281 
  1282         while (low <= high) {
  1283             int mid = (low + high) >>> 1;
  1284             double midVal = a[mid];
  1285 
  1286             if (midVal < key)
  1287                 low = mid + 1;  // Neither val is NaN, thisVal is smaller
  1288             else if (midVal > key)
  1289                 high = mid - 1; // Neither val is NaN, thisVal is larger
  1290             else {
  1291                 long midBits = Double.doubleToLongBits(midVal);
  1292                 long keyBits = Double.doubleToLongBits(key);
  1293                 if (midBits == keyBits)     // Values are equal
  1294                     return mid;             // Key found
  1295                 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
  1296                     low = mid + 1;
  1297                 else                        // (0.0, -0.0) or (NaN, !NaN)
  1298                     high = mid - 1;
  1299             }
  1300         }
  1301         return -(low + 1);  // key not found.
  1302     }
  1303 
  1304     /**
  1305      * Searches the specified array of floats for the specified value using
  1306      * the binary search algorithm. The array must be sorted
  1307      * (as by the {@link #sort(float[])} method) prior to making this call. If
  1308      * it is not sorted, the results are undefined. If the array contains
  1309      * multiple elements with the specified value, there is no guarantee which
  1310      * one will be found. This method considers all NaN values to be
  1311      * equivalent and equal.
  1312      *
  1313      * @param a the array to be searched
  1314      * @param key the value to be searched for
  1315      * @return index of the search key, if it is contained in the array;
  1316      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1317      *         <i>insertion point</i> is defined as the point at which the
  1318      *         key would be inserted into the array: the index of the first
  1319      *         element greater than the key, or <tt>a.length</tt> if all
  1320      *         elements in the array are less than the specified key. Note
  1321      *         that this guarantees that the return value will be &gt;= 0 if
  1322      *         and only if the key is found.
  1323      */
  1324     public static int binarySearch(float[] a, float key) {
  1325         return binarySearch0(a, 0, a.length, key);
  1326     }
  1327 
  1328     /**
  1329      * Searches a range of
  1330      * the specified array of floats for the specified value using
  1331      * the binary search algorithm.
  1332      * The range must be sorted
  1333      * (as by the {@link #sort(float[], int, int)} method)
  1334      * prior to making this call. If
  1335      * it is not sorted, the results are undefined. If the range contains
  1336      * multiple elements with the specified value, there is no guarantee which
  1337      * one will be found. This method considers all NaN values to be
  1338      * equivalent and equal.
  1339      *
  1340      * @param a the array to be searched
  1341      * @param fromIndex the index of the first element (inclusive) to be
  1342      *          searched
  1343      * @param toIndex the index of the last element (exclusive) to be searched
  1344      * @param key the value to be searched for
  1345      * @return index of the search key, if it is contained in the array
  1346      *         within the specified range;
  1347      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  1348      *         <i>insertion point</i> is defined as the point at which the
  1349      *         key would be inserted into the array: the index of the first
  1350      *         element in the range greater than the key,
  1351      *         or <tt>toIndex</tt> if all
  1352      *         elements in the range are less than the specified key. Note
  1353      *         that this guarantees that the return value will be &gt;= 0 if
  1354      *         and only if the key is found.
  1355      * @throws IllegalArgumentException
  1356      *         if {@code fromIndex > toIndex}
  1357      * @throws ArrayIndexOutOfBoundsException
  1358      *         if {@code fromIndex < 0 or toIndex > a.length}
  1359      * @since 1.6
  1360      */
  1361     public static int binarySearch(float[] a, int fromIndex, int toIndex,
  1362                                    float key) {
  1363         rangeCheck(a.length, fromIndex, toIndex);
  1364         return binarySearch0(a, fromIndex, toIndex, key);
  1365     }
  1366 
  1367     // Like public version, but without range checks.
  1368     private static int binarySearch0(float[] a, int fromIndex, int toIndex,
  1369                                      float key) {
  1370         int low = fromIndex;
  1371         int high = toIndex - 1;
  1372 
  1373         while (low <= high) {
  1374             int mid = (low + high) >>> 1;
  1375             float midVal = a[mid];
  1376 
  1377             if (midVal < key)
  1378                 low = mid + 1;  // Neither val is NaN, thisVal is smaller
  1379             else if (midVal > key)
  1380                 high = mid - 1; // Neither val is NaN, thisVal is larger
  1381             else {
  1382                 int midBits = Float.floatToIntBits(midVal);
  1383                 int keyBits = Float.floatToIntBits(key);
  1384                 if (midBits == keyBits)     // Values are equal
  1385                     return mid;             // Key found
  1386                 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
  1387                     low = mid + 1;
  1388                 else                        // (0.0, -0.0) or (NaN, !NaN)
  1389                     high = mid - 1;
  1390             }
  1391         }
  1392         return -(low + 1);  // key not found.
  1393     }
  1394 
  1395     /**
  1396      * Searches the specified array for the specified object using the binary
  1397      * search algorithm. The array must be sorted into ascending order
  1398      * according to the
  1399      * {@linkplain Comparable natural ordering}
  1400      * of its elements (as by the
  1401      * {@link #sort(Object[])} method) prior to making this call.
  1402      * If it is not sorted, the results are undefined.
  1403      * (If the array contains elements that are not mutually comparable (for
  1404      * example, strings and integers), it <i>cannot</i> be sorted according
  1405      * to the natural ordering of its elements, hence results are undefined.)
  1406      * If the array contains multiple
  1407      * elements equal to the specified object, there is no guarantee which
  1408      * one will be found.
  1409      *
  1410      * @param a the array to be searched
  1411      * @param key the value to be searched for
  1412      * @return index of the search key, if it is contained in the array;
  1413      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1414      *         <i>insertion point</i> is defined as the point at which the
  1415      *         key would be inserted into the array: the index of the first
  1416      *         element greater than the key, or <tt>a.length</tt> if all
  1417      *         elements in the array are less than the specified key.  Note
  1418      *         that this guarantees that the return value will be &gt;= 0 if
  1419      *         and only if the key is found.
  1420      * @throws ClassCastException if the search key is not comparable to the
  1421      *         elements of the array.
  1422      */
  1423     public static int binarySearch(Object[] a, Object key) {
  1424         return binarySearch0(a, 0, a.length, key);
  1425     }
  1426 
  1427     /**
  1428      * Searches a range of
  1429      * the specified array for the specified object using the binary
  1430      * search algorithm.
  1431      * The range must be sorted into ascending order
  1432      * according to the
  1433      * {@linkplain Comparable natural ordering}
  1434      * of its elements (as by the
  1435      * {@link #sort(Object[], int, int)} method) prior to making this
  1436      * call.  If it is not sorted, the results are undefined.
  1437      * (If the range contains elements that are not mutually comparable (for
  1438      * example, strings and integers), it <i>cannot</i> be sorted according
  1439      * to the natural ordering of its elements, hence results are undefined.)
  1440      * If the range contains multiple
  1441      * elements equal to the specified object, there is no guarantee which
  1442      * one will be found.
  1443      *
  1444      * @param a the array to be searched
  1445      * @param fromIndex the index of the first element (inclusive) to be
  1446      *          searched
  1447      * @param toIndex the index of the last element (exclusive) to be searched
  1448      * @param key the value to be searched for
  1449      * @return index of the search key, if it is contained in the array
  1450      *         within the specified range;
  1451      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1452      *         <i>insertion point</i> is defined as the point at which the
  1453      *         key would be inserted into the array: the index of the first
  1454      *         element in the range greater than the key,
  1455      *         or <tt>toIndex</tt> if all
  1456      *         elements in the range are less than the specified key.  Note
  1457      *         that this guarantees that the return value will be &gt;= 0 if
  1458      *         and only if the key is found.
  1459      * @throws ClassCastException if the search key is not comparable to the
  1460      *         elements of the array within the specified range.
  1461      * @throws IllegalArgumentException
  1462      *         if {@code fromIndex > toIndex}
  1463      * @throws ArrayIndexOutOfBoundsException
  1464      *         if {@code fromIndex < 0 or toIndex > a.length}
  1465      * @since 1.6
  1466      */
  1467     public static int binarySearch(Object[] a, int fromIndex, int toIndex,
  1468                                    Object key) {
  1469         rangeCheck(a.length, fromIndex, toIndex);
  1470         return binarySearch0(a, fromIndex, toIndex, key);
  1471     }
  1472 
  1473     // Like public version, but without range checks.
  1474     private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
  1475                                      Object key) {
  1476         int low = fromIndex;
  1477         int high = toIndex - 1;
  1478 
  1479         while (low <= high) {
  1480             int mid = (low + high) >>> 1;
  1481             Comparable midVal = (Comparable)a[mid];
  1482             int cmp = midVal.compareTo(key);
  1483 
  1484             if (cmp < 0)
  1485                 low = mid + 1;
  1486             else if (cmp > 0)
  1487                 high = mid - 1;
  1488             else
  1489                 return mid; // key found
  1490         }
  1491         return -(low + 1);  // key not found.
  1492     }
  1493 
  1494     /**
  1495      * Searches the specified array for the specified object using the binary
  1496      * search algorithm.  The array must be sorted into ascending order
  1497      * according to the specified comparator (as by the
  1498      * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
  1499      * method) prior to making this call.  If it is
  1500      * not sorted, the results are undefined.
  1501      * If the array contains multiple
  1502      * elements equal to the specified object, there is no guarantee which one
  1503      * will be found.
  1504      *
  1505      * @param a the array to be searched
  1506      * @param key the value to be searched for
  1507      * @param c the comparator by which the array is ordered.  A
  1508      *        <tt>null</tt> value indicates that the elements'
  1509      *        {@linkplain Comparable natural ordering} should be used.
  1510      * @return index of the search key, if it is contained in the array;
  1511      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1512      *         <i>insertion point</i> is defined as the point at which the
  1513      *         key would be inserted into the array: the index of the first
  1514      *         element greater than the key, or <tt>a.length</tt> if all
  1515      *         elements in the array are less than the specified key.  Note
  1516      *         that this guarantees that the return value will be &gt;= 0 if
  1517      *         and only if the key is found.
  1518      * @throws ClassCastException if the array contains elements that are not
  1519      *         <i>mutually comparable</i> using the specified comparator,
  1520      *         or the search key is not comparable to the
  1521      *         elements of the array using this comparator.
  1522      */
  1523     public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
  1524         return binarySearch0(a, 0, a.length, key, c);
  1525     }
  1526 
  1527     /**
  1528      * Searches a range of
  1529      * the specified array for the specified object using the binary
  1530      * search algorithm.
  1531      * The range must be sorted into ascending order
  1532      * according to the specified comparator (as by the
  1533      * {@link #sort(Object[], int, int, Comparator)
  1534      * sort(T[], int, int, Comparator)}
  1535      * method) prior to making this call.
  1536      * If it is not sorted, the results are undefined.
  1537      * If the range contains multiple elements equal to the specified object,
  1538      * there is no guarantee which one will be found.
  1539      *
  1540      * @param a the array to be searched
  1541      * @param fromIndex the index of the first element (inclusive) to be
  1542      *          searched
  1543      * @param toIndex the index of the last element (exclusive) to be searched
  1544      * @param key the value to be searched for
  1545      * @param c the comparator by which the array is ordered.  A
  1546      *        <tt>null</tt> value indicates that the elements'
  1547      *        {@linkplain Comparable natural ordering} should be used.
  1548      * @return index of the search key, if it is contained in the array
  1549      *         within the specified range;
  1550      *         otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
  1551      *         <i>insertion point</i> is defined as the point at which the
  1552      *         key would be inserted into the array: the index of the first
  1553      *         element in the range greater than the key,
  1554      *         or <tt>toIndex</tt> if all
  1555      *         elements in the range are less than the specified key.  Note
  1556      *         that this guarantees that the return value will be &gt;= 0 if
  1557      *         and only if the key is found.
  1558      * @throws ClassCastException if the range contains elements that are not
  1559      *         <i>mutually comparable</i> using the specified comparator,
  1560      *         or the search key is not comparable to the
  1561      *         elements in the range using this comparator.
  1562      * @throws IllegalArgumentException
  1563      *         if {@code fromIndex > toIndex}
  1564      * @throws ArrayIndexOutOfBoundsException
  1565      *         if {@code fromIndex < 0 or toIndex > a.length}
  1566      * @since 1.6
  1567      */
  1568     public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
  1569                                        T key, Comparator<? super T> c) {
  1570         rangeCheck(a.length, fromIndex, toIndex);
  1571         return binarySearch0(a, fromIndex, toIndex, key, c);
  1572     }
  1573 
  1574     // Like public version, but without range checks.
  1575     private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
  1576                                          T key, Comparator<? super T> c) {
  1577         if (c == null) {
  1578             return binarySearch0(a, fromIndex, toIndex, key);
  1579         }
  1580         int low = fromIndex;
  1581         int high = toIndex - 1;
  1582 
  1583         while (low <= high) {
  1584             int mid = (low + high) >>> 1;
  1585             T midVal = a[mid];
  1586             int cmp = c.compare(midVal, key);
  1587             if (cmp < 0)
  1588                 low = mid + 1;
  1589             else if (cmp > 0)
  1590                 high = mid - 1;
  1591             else
  1592                 return mid; // key found
  1593         }
  1594         return -(low + 1);  // key not found.
  1595     }
  1596 
  1597     // Equality Testing
  1598 
  1599     /**
  1600      * Returns <tt>true</tt> if the two specified arrays of longs are
  1601      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1602      * arrays contain the same number of elements, and all corresponding pairs
  1603      * of elements in the two arrays are equal.  In other words, two arrays
  1604      * are equal if they contain the same elements in the same order.  Also,
  1605      * two array references are considered equal if both are <tt>null</tt>.<p>
  1606      *
  1607      * @param a one array to be tested for equality
  1608      * @param a2 the other array to be tested for equality
  1609      * @return <tt>true</tt> if the two arrays are equal
  1610      */
  1611     public static boolean equals(long[] a, long[] a2) {
  1612         if (a==a2)
  1613             return true;
  1614         if (a==null || a2==null)
  1615             return false;
  1616 
  1617         int length = a.length;
  1618         if (a2.length != length)
  1619             return false;
  1620 
  1621         for (int i=0; i<length; i++)
  1622             if (a[i] != a2[i])
  1623                 return false;
  1624 
  1625         return true;
  1626     }
  1627 
  1628     /**
  1629      * Returns <tt>true</tt> if the two specified arrays of ints are
  1630      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1631      * arrays contain the same number of elements, and all corresponding pairs
  1632      * of elements in the two arrays are equal.  In other words, two arrays
  1633      * are equal if they contain the same elements in the same order.  Also,
  1634      * two array references are considered equal if both are <tt>null</tt>.<p>
  1635      *
  1636      * @param a one array to be tested for equality
  1637      * @param a2 the other array to be tested for equality
  1638      * @return <tt>true</tt> if the two arrays are equal
  1639      */
  1640     public static boolean equals(int[] a, int[] a2) {
  1641         if (a==a2)
  1642             return true;
  1643         if (a==null || a2==null)
  1644             return false;
  1645 
  1646         int length = a.length;
  1647         if (a2.length != length)
  1648             return false;
  1649 
  1650         for (int i=0; i<length; i++)
  1651             if (a[i] != a2[i])
  1652                 return false;
  1653 
  1654         return true;
  1655     }
  1656 
  1657     /**
  1658      * Returns <tt>true</tt> if the two specified arrays of shorts are
  1659      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1660      * arrays contain the same number of elements, and all corresponding pairs
  1661      * of elements in the two arrays are equal.  In other words, two arrays
  1662      * are equal if they contain the same elements in the same order.  Also,
  1663      * two array references are considered equal if both are <tt>null</tt>.<p>
  1664      *
  1665      * @param a one array to be tested for equality
  1666      * @param a2 the other array to be tested for equality
  1667      * @return <tt>true</tt> if the two arrays are equal
  1668      */
  1669     public static boolean equals(short[] a, short a2[]) {
  1670         if (a==a2)
  1671             return true;
  1672         if (a==null || a2==null)
  1673             return false;
  1674 
  1675         int length = a.length;
  1676         if (a2.length != length)
  1677             return false;
  1678 
  1679         for (int i=0; i<length; i++)
  1680             if (a[i] != a2[i])
  1681                 return false;
  1682 
  1683         return true;
  1684     }
  1685 
  1686     /**
  1687      * Returns <tt>true</tt> if the two specified arrays of chars are
  1688      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1689      * arrays contain the same number of elements, and all corresponding pairs
  1690      * of elements in the two arrays are equal.  In other words, two arrays
  1691      * are equal if they contain the same elements in the same order.  Also,
  1692      * two array references are considered equal if both are <tt>null</tt>.<p>
  1693      *
  1694      * @param a one array to be tested for equality
  1695      * @param a2 the other array to be tested for equality
  1696      * @return <tt>true</tt> if the two arrays are equal
  1697      */
  1698     public static boolean equals(char[] a, char[] a2) {
  1699         if (a==a2)
  1700             return true;
  1701         if (a==null || a2==null)
  1702             return false;
  1703 
  1704         int length = a.length;
  1705         if (a2.length != length)
  1706             return false;
  1707 
  1708         for (int i=0; i<length; i++)
  1709             if (a[i] != a2[i])
  1710                 return false;
  1711 
  1712         return true;
  1713     }
  1714 
  1715     /**
  1716      * Returns <tt>true</tt> if the two specified arrays of bytes are
  1717      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1718      * arrays contain the same number of elements, and all corresponding pairs
  1719      * of elements in the two arrays are equal.  In other words, two arrays
  1720      * are equal if they contain the same elements in the same order.  Also,
  1721      * two array references are considered equal if both are <tt>null</tt>.<p>
  1722      *
  1723      * @param a one array to be tested for equality
  1724      * @param a2 the other array to be tested for equality
  1725      * @return <tt>true</tt> if the two arrays are equal
  1726      */
  1727     public static boolean equals(byte[] a, byte[] a2) {
  1728         if (a==a2)
  1729             return true;
  1730         if (a==null || a2==null)
  1731             return false;
  1732 
  1733         int length = a.length;
  1734         if (a2.length != length)
  1735             return false;
  1736 
  1737         for (int i=0; i<length; i++)
  1738             if (a[i] != a2[i])
  1739                 return false;
  1740 
  1741         return true;
  1742     }
  1743 
  1744     /**
  1745      * Returns <tt>true</tt> if the two specified arrays of booleans are
  1746      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1747      * arrays contain the same number of elements, and all corresponding pairs
  1748      * of elements in the two arrays are equal.  In other words, two arrays
  1749      * are equal if they contain the same elements in the same order.  Also,
  1750      * two array references are considered equal if both are <tt>null</tt>.<p>
  1751      *
  1752      * @param a one array to be tested for equality
  1753      * @param a2 the other array to be tested for equality
  1754      * @return <tt>true</tt> if the two arrays are equal
  1755      */
  1756     public static boolean equals(boolean[] a, boolean[] a2) {
  1757         if (a==a2)
  1758             return true;
  1759         if (a==null || a2==null)
  1760             return false;
  1761 
  1762         int length = a.length;
  1763         if (a2.length != length)
  1764             return false;
  1765 
  1766         for (int i=0; i<length; i++)
  1767             if (a[i] != a2[i])
  1768                 return false;
  1769 
  1770         return true;
  1771     }
  1772 
  1773     /**
  1774      * Returns <tt>true</tt> if the two specified arrays of doubles are
  1775      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1776      * arrays contain the same number of elements, and all corresponding pairs
  1777      * of elements in the two arrays are equal.  In other words, two arrays
  1778      * are equal if they contain the same elements in the same order.  Also,
  1779      * two array references are considered equal if both are <tt>null</tt>.<p>
  1780      *
  1781      * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
  1782      * <pre>    <tt>new Double(d1).equals(new Double(d2))</tt></pre>
  1783      * (Unlike the <tt>==</tt> operator, this method considers
  1784      * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
  1785      *
  1786      * @param a one array to be tested for equality
  1787      * @param a2 the other array to be tested for equality
  1788      * @return <tt>true</tt> if the two arrays are equal
  1789      * @see Double#equals(Object)
  1790      */
  1791     public static boolean equals(double[] a, double[] a2) {
  1792         if (a==a2)
  1793             return true;
  1794         if (a==null || a2==null)
  1795             return false;
  1796 
  1797         int length = a.length;
  1798         if (a2.length != length)
  1799             return false;
  1800 
  1801         for (int i=0; i<length; i++)
  1802             if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
  1803                 return false;
  1804 
  1805         return true;
  1806     }
  1807 
  1808     /**
  1809      * Returns <tt>true</tt> if the two specified arrays of floats are
  1810      * <i>equal</i> to one another.  Two arrays are considered equal if both
  1811      * arrays contain the same number of elements, and all corresponding pairs
  1812      * of elements in the two arrays are equal.  In other words, two arrays
  1813      * are equal if they contain the same elements in the same order.  Also,
  1814      * two array references are considered equal if both are <tt>null</tt>.<p>
  1815      *
  1816      * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
  1817      * <pre>    <tt>new Float(f1).equals(new Float(f2))</tt></pre>
  1818      * (Unlike the <tt>==</tt> operator, this method considers
  1819      * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
  1820      *
  1821      * @param a one array to be tested for equality
  1822      * @param a2 the other array to be tested for equality
  1823      * @return <tt>true</tt> if the two arrays are equal
  1824      * @see Float#equals(Object)
  1825      */
  1826     public static boolean equals(float[] a, float[] a2) {
  1827         if (a==a2)
  1828             return true;
  1829         if (a==null || a2==null)
  1830             return false;
  1831 
  1832         int length = a.length;
  1833         if (a2.length != length)
  1834             return false;
  1835 
  1836         for (int i=0; i<length; i++)
  1837             if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
  1838                 return false;
  1839 
  1840         return true;
  1841     }
  1842 
  1843     /**
  1844      * Returns <tt>true</tt> if the two specified arrays of Objects are
  1845      * <i>equal</i> to one another.  The two arrays are considered equal if
  1846      * both arrays contain the same number of elements, and all corresponding
  1847      * pairs of elements in the two arrays are equal.  Two objects <tt>e1</tt>
  1848      * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
  1849      * : e1.equals(e2))</tt>.  In other words, the two arrays are equal if
  1850      * they contain the same elements in the same order.  Also, two array
  1851      * references are considered equal if both are <tt>null</tt>.<p>
  1852      *
  1853      * @param a one array to be tested for equality
  1854      * @param a2 the other array to be tested for equality
  1855      * @return <tt>true</tt> if the two arrays are equal
  1856      */
  1857     public static boolean equals(Object[] a, Object[] a2) {
  1858         if (a==a2)
  1859             return true;
  1860         if (a==null || a2==null)
  1861             return false;
  1862 
  1863         int length = a.length;
  1864         if (a2.length != length)
  1865             return false;
  1866 
  1867         for (int i=0; i<length; i++) {
  1868             Object o1 = a[i];
  1869             Object o2 = a2[i];
  1870             if (!(o1==null ? o2==null : o1.equals(o2)))
  1871                 return false;
  1872         }
  1873 
  1874         return true;
  1875     }
  1876 
  1877     // Filling
  1878 
  1879     /**
  1880      * Assigns the specified long value to each element of the specified array
  1881      * of longs.
  1882      *
  1883      * @param a the array to be filled
  1884      * @param val the value to be stored in all elements of the array
  1885      */
  1886     public static void fill(long[] a, long val) {
  1887         for (int i = 0, len = a.length; i < len; i++)
  1888             a[i] = val;
  1889     }
  1890 
  1891     /**
  1892      * Assigns the specified long value to each element of the specified
  1893      * range of the specified array of longs.  The range to be filled
  1894      * extends from index <tt>fromIndex</tt>, inclusive, to index
  1895      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  1896      * range to be filled is empty.)
  1897      *
  1898      * @param a the array to be filled
  1899      * @param fromIndex the index of the first element (inclusive) to be
  1900      *        filled with the specified value
  1901      * @param toIndex the index of the last element (exclusive) to be
  1902      *        filled with the specified value
  1903      * @param val the value to be stored in all elements of the array
  1904      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  1905      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  1906      *         <tt>toIndex &gt; a.length</tt>
  1907      */
  1908     public static void fill(long[] a, int fromIndex, int toIndex, long val) {
  1909         rangeCheck(a.length, fromIndex, toIndex);
  1910         for (int i = fromIndex; i < toIndex; i++)
  1911             a[i] = val;
  1912     }
  1913 
  1914     /**
  1915      * Assigns the specified int value to each element of the specified array
  1916      * of ints.
  1917      *
  1918      * @param a the array to be filled
  1919      * @param val the value to be stored in all elements of the array
  1920      */
  1921     public static void fill(int[] a, int val) {
  1922         for (int i = 0, len = a.length; i < len; i++)
  1923             a[i] = val;
  1924     }
  1925 
  1926     /**
  1927      * Assigns the specified int value to each element of the specified
  1928      * range of the specified array of ints.  The range to be filled
  1929      * extends from index <tt>fromIndex</tt>, inclusive, to index
  1930      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  1931      * range to be filled is empty.)
  1932      *
  1933      * @param a the array to be filled
  1934      * @param fromIndex the index of the first element (inclusive) to be
  1935      *        filled with the specified value
  1936      * @param toIndex the index of the last element (exclusive) to be
  1937      *        filled with the specified value
  1938      * @param val the value to be stored in all elements of the array
  1939      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  1940      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  1941      *         <tt>toIndex &gt; a.length</tt>
  1942      */
  1943     public static void fill(int[] a, int fromIndex, int toIndex, int val) {
  1944         rangeCheck(a.length, fromIndex, toIndex);
  1945         for (int i = fromIndex; i < toIndex; i++)
  1946             a[i] = val;
  1947     }
  1948 
  1949     /**
  1950      * Assigns the specified short value to each element of the specified array
  1951      * of shorts.
  1952      *
  1953      * @param a the array to be filled
  1954      * @param val the value to be stored in all elements of the array
  1955      */
  1956     public static void fill(short[] a, short val) {
  1957         for (int i = 0, len = a.length; i < len; i++)
  1958             a[i] = val;
  1959     }
  1960 
  1961     /**
  1962      * Assigns the specified short value to each element of the specified
  1963      * range of the specified array of shorts.  The range to be filled
  1964      * extends from index <tt>fromIndex</tt>, inclusive, to index
  1965      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  1966      * range to be filled is empty.)
  1967      *
  1968      * @param a the array to be filled
  1969      * @param fromIndex the index of the first element (inclusive) to be
  1970      *        filled with the specified value
  1971      * @param toIndex the index of the last element (exclusive) to be
  1972      *        filled with the specified value
  1973      * @param val the value to be stored in all elements of the array
  1974      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  1975      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  1976      *         <tt>toIndex &gt; a.length</tt>
  1977      */
  1978     public static void fill(short[] a, int fromIndex, int toIndex, short val) {
  1979         rangeCheck(a.length, fromIndex, toIndex);
  1980         for (int i = fromIndex; i < toIndex; i++)
  1981             a[i] = val;
  1982     }
  1983 
  1984     /**
  1985      * Assigns the specified char value to each element of the specified array
  1986      * of chars.
  1987      *
  1988      * @param a the array to be filled
  1989      * @param val the value to be stored in all elements of the array
  1990      */
  1991     public static void fill(char[] a, char val) {
  1992         for (int i = 0, len = a.length; i < len; i++)
  1993             a[i] = val;
  1994     }
  1995 
  1996     /**
  1997      * Assigns the specified char value to each element of the specified
  1998      * range of the specified array of chars.  The range to be filled
  1999      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2000      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2001      * range to be filled is empty.)
  2002      *
  2003      * @param a the array to be filled
  2004      * @param fromIndex the index of the first element (inclusive) to be
  2005      *        filled with the specified value
  2006      * @param toIndex the index of the last element (exclusive) to be
  2007      *        filled with the specified value
  2008      * @param val the value to be stored in all elements of the array
  2009      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2010      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2011      *         <tt>toIndex &gt; a.length</tt>
  2012      */
  2013     public static void fill(char[] a, int fromIndex, int toIndex, char val) {
  2014         rangeCheck(a.length, fromIndex, toIndex);
  2015         for (int i = fromIndex; i < toIndex; i++)
  2016             a[i] = val;
  2017     }
  2018 
  2019     /**
  2020      * Assigns the specified byte value to each element of the specified array
  2021      * of bytes.
  2022      *
  2023      * @param a the array to be filled
  2024      * @param val the value to be stored in all elements of the array
  2025      */
  2026     public static void fill(byte[] a, byte val) {
  2027         for (int i = 0, len = a.length; i < len; i++)
  2028             a[i] = val;
  2029     }
  2030 
  2031     /**
  2032      * Assigns the specified byte value to each element of the specified
  2033      * range of the specified array of bytes.  The range to be filled
  2034      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2035      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2036      * range to be filled is empty.)
  2037      *
  2038      * @param a the array to be filled
  2039      * @param fromIndex the index of the first element (inclusive) to be
  2040      *        filled with the specified value
  2041      * @param toIndex the index of the last element (exclusive) to be
  2042      *        filled with the specified value
  2043      * @param val the value to be stored in all elements of the array
  2044      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2045      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2046      *         <tt>toIndex &gt; a.length</tt>
  2047      */
  2048     public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
  2049         rangeCheck(a.length, fromIndex, toIndex);
  2050         for (int i = fromIndex; i < toIndex; i++)
  2051             a[i] = val;
  2052     }
  2053 
  2054     /**
  2055      * Assigns the specified boolean value to each element of the specified
  2056      * array of booleans.
  2057      *
  2058      * @param a the array to be filled
  2059      * @param val the value to be stored in all elements of the array
  2060      */
  2061     public static void fill(boolean[] a, boolean val) {
  2062         for (int i = 0, len = a.length; i < len; i++)
  2063             a[i] = val;
  2064     }
  2065 
  2066     /**
  2067      * Assigns the specified boolean value to each element of the specified
  2068      * range of the specified array of booleans.  The range to be filled
  2069      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2070      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2071      * range to be filled is empty.)
  2072      *
  2073      * @param a the array to be filled
  2074      * @param fromIndex the index of the first element (inclusive) to be
  2075      *        filled with the specified value
  2076      * @param toIndex the index of the last element (exclusive) to be
  2077      *        filled with the specified value
  2078      * @param val the value to be stored in all elements of the array
  2079      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2080      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2081      *         <tt>toIndex &gt; a.length</tt>
  2082      */
  2083     public static void fill(boolean[] a, int fromIndex, int toIndex,
  2084                             boolean val) {
  2085         rangeCheck(a.length, fromIndex, toIndex);
  2086         for (int i = fromIndex; i < toIndex; i++)
  2087             a[i] = val;
  2088     }
  2089 
  2090     /**
  2091      * Assigns the specified double value to each element of the specified
  2092      * array of doubles.
  2093      *
  2094      * @param a the array to be filled
  2095      * @param val the value to be stored in all elements of the array
  2096      */
  2097     public static void fill(double[] a, double val) {
  2098         for (int i = 0, len = a.length; i < len; i++)
  2099             a[i] = val;
  2100     }
  2101 
  2102     /**
  2103      * Assigns the specified double value to each element of the specified
  2104      * range of the specified array of doubles.  The range to be filled
  2105      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2106      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2107      * range to be filled is empty.)
  2108      *
  2109      * @param a the array to be filled
  2110      * @param fromIndex the index of the first element (inclusive) to be
  2111      *        filled with the specified value
  2112      * @param toIndex the index of the last element (exclusive) to be
  2113      *        filled with the specified value
  2114      * @param val the value to be stored in all elements of the array
  2115      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2116      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2117      *         <tt>toIndex &gt; a.length</tt>
  2118      */
  2119     public static void fill(double[] a, int fromIndex, int toIndex,double val){
  2120         rangeCheck(a.length, fromIndex, toIndex);
  2121         for (int i = fromIndex; i < toIndex; i++)
  2122             a[i] = val;
  2123     }
  2124 
  2125     /**
  2126      * Assigns the specified float value to each element of the specified array
  2127      * of floats.
  2128      *
  2129      * @param a the array to be filled
  2130      * @param val the value to be stored in all elements of the array
  2131      */
  2132     public static void fill(float[] a, float val) {
  2133         for (int i = 0, len = a.length; i < len; i++)
  2134             a[i] = val;
  2135     }
  2136 
  2137     /**
  2138      * Assigns the specified float value to each element of the specified
  2139      * range of the specified array of floats.  The range to be filled
  2140      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2141      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2142      * range to be filled is empty.)
  2143      *
  2144      * @param a the array to be filled
  2145      * @param fromIndex the index of the first element (inclusive) to be
  2146      *        filled with the specified value
  2147      * @param toIndex the index of the last element (exclusive) to be
  2148      *        filled with the specified value
  2149      * @param val the value to be stored in all elements of the array
  2150      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2151      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2152      *         <tt>toIndex &gt; a.length</tt>
  2153      */
  2154     public static void fill(float[] a, int fromIndex, int toIndex, float val) {
  2155         rangeCheck(a.length, fromIndex, toIndex);
  2156         for (int i = fromIndex; i < toIndex; i++)
  2157             a[i] = val;
  2158     }
  2159 
  2160     /**
  2161      * Assigns the specified Object reference to each element of the specified
  2162      * array of Objects.
  2163      *
  2164      * @param a the array to be filled
  2165      * @param val the value to be stored in all elements of the array
  2166      * @throws ArrayStoreException if the specified value is not of a
  2167      *         runtime type that can be stored in the specified array
  2168      */
  2169     public static void fill(Object[] a, Object val) {
  2170         for (int i = 0, len = a.length; i < len; i++)
  2171             a[i] = val;
  2172     }
  2173 
  2174     /**
  2175      * Assigns the specified Object reference to each element of the specified
  2176      * range of the specified array of Objects.  The range to be filled
  2177      * extends from index <tt>fromIndex</tt>, inclusive, to index
  2178      * <tt>toIndex</tt>, exclusive.  (If <tt>fromIndex==toIndex</tt>, the
  2179      * range to be filled is empty.)
  2180      *
  2181      * @param a the array to be filled
  2182      * @param fromIndex the index of the first element (inclusive) to be
  2183      *        filled with the specified value
  2184      * @param toIndex the index of the last element (exclusive) to be
  2185      *        filled with the specified value
  2186      * @param val the value to be stored in all elements of the array
  2187      * @throws IllegalArgumentException if <tt>fromIndex &gt; toIndex</tt>
  2188      * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex &lt; 0</tt> or
  2189      *         <tt>toIndex &gt; a.length</tt>
  2190      * @throws ArrayStoreException if the specified value is not of a
  2191      *         runtime type that can be stored in the specified array
  2192      */
  2193     public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
  2194         rangeCheck(a.length, fromIndex, toIndex);
  2195         for (int i = fromIndex; i < toIndex; i++)
  2196             a[i] = val;
  2197     }
  2198 
  2199     // Cloning
  2200 
  2201     /**
  2202      * Copies the specified array, truncating or padding with nulls (if necessary)
  2203      * so the copy has the specified length.  For all indices that are
  2204      * valid in both the original array and the copy, the two arrays will
  2205      * contain identical values.  For any indices that are valid in the
  2206      * copy but not the original, the copy will contain <tt>null</tt>.
  2207      * Such indices will exist if and only if the specified length
  2208      * is greater than that of the original array.
  2209      * The resulting array is of exactly the same class as the original array.
  2210      *
  2211      * @param original the array to be copied
  2212      * @param newLength the length of the copy to be returned
  2213      * @return a copy of the original array, truncated or padded with nulls
  2214      *     to obtain the specified length
  2215      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2216      * @throws NullPointerException if <tt>original</tt> is null
  2217      * @since 1.6
  2218      */
  2219     public static <T> T[] copyOf(T[] original, int newLength) {
  2220         return (T[]) copyOf(original, newLength, original.getClass());
  2221     }
  2222 
  2223     /**
  2224      * Copies the specified array, truncating or padding with nulls (if necessary)
  2225      * so the copy has the specified length.  For all indices that are
  2226      * valid in both the original array and the copy, the two arrays will
  2227      * contain identical values.  For any indices that are valid in the
  2228      * copy but not the original, the copy will contain <tt>null</tt>.
  2229      * Such indices will exist if and only if the specified length
  2230      * is greater than that of the original array.
  2231      * The resulting array is of the class <tt>newType</tt>.
  2232      *
  2233      * @param original the array to be copied
  2234      * @param newLength the length of the copy to be returned
  2235      * @param newType the class of the copy to be returned
  2236      * @return a copy of the original array, truncated or padded with nulls
  2237      *     to obtain the specified length
  2238      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2239      * @throws NullPointerException if <tt>original</tt> is null
  2240      * @throws ArrayStoreException if an element copied from
  2241      *     <tt>original</tt> is not of a runtime type that can be stored in
  2242      *     an array of class <tt>newType</tt>
  2243      * @since 1.6
  2244      */
  2245     public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
  2246         T[] copy = ((Object)newType == (Object)Object[].class)
  2247             ? (T[]) new Object[newLength]
  2248             : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  2249         System.arraycopy(original, 0, copy, 0,
  2250                          Math.min(original.length, newLength));
  2251         return copy;
  2252     }
  2253 
  2254     /**
  2255      * Copies the specified array, truncating or padding with zeros (if necessary)
  2256      * so the copy has the specified length.  For all indices that are
  2257      * valid in both the original array and the copy, the two arrays will
  2258      * contain identical values.  For any indices that are valid in the
  2259      * copy but not the original, the copy will contain <tt>(byte)0</tt>.
  2260      * Such indices will exist if and only if the specified length
  2261      * is greater than that of the original array.
  2262      *
  2263      * @param original the array to be copied
  2264      * @param newLength the length of the copy to be returned
  2265      * @return a copy of the original array, truncated or padded with zeros
  2266      *     to obtain the specified length
  2267      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2268      * @throws NullPointerException if <tt>original</tt> is null
  2269      * @since 1.6
  2270      */
  2271     public static byte[] copyOf(byte[] original, int newLength) {
  2272         byte[] copy = new byte[newLength];
  2273         System.arraycopy(original, 0, copy, 0,
  2274                          Math.min(original.length, newLength));
  2275         return copy;
  2276     }
  2277 
  2278     /**
  2279      * Copies the specified array, truncating or padding with zeros (if necessary)
  2280      * so the copy has the specified length.  For all indices that are
  2281      * valid in both the original array and the copy, the two arrays will
  2282      * contain identical values.  For any indices that are valid in the
  2283      * copy but not the original, the copy will contain <tt>(short)0</tt>.
  2284      * Such indices will exist if and only if the specified length
  2285      * is greater than that of the original array.
  2286      *
  2287      * @param original the array to be copied
  2288      * @param newLength the length of the copy to be returned
  2289      * @return a copy of the original array, truncated or padded with zeros
  2290      *     to obtain the specified length
  2291      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2292      * @throws NullPointerException if <tt>original</tt> is null
  2293      * @since 1.6
  2294      */
  2295     public static short[] copyOf(short[] original, int newLength) {
  2296         short[] copy = new short[newLength];
  2297         System.arraycopy(original, 0, copy, 0,
  2298                          Math.min(original.length, newLength));
  2299         return copy;
  2300     }
  2301 
  2302     /**
  2303      * Copies the specified array, truncating or padding with zeros (if necessary)
  2304      * so the copy has the specified length.  For all indices that are
  2305      * valid in both the original array and the copy, the two arrays will
  2306      * contain identical values.  For any indices that are valid in the
  2307      * copy but not the original, the copy will contain <tt>0</tt>.
  2308      * Such indices will exist if and only if the specified length
  2309      * is greater than that of the original array.
  2310      *
  2311      * @param original the array to be copied
  2312      * @param newLength the length of the copy to be returned
  2313      * @return a copy of the original array, truncated or padded with zeros
  2314      *     to obtain the specified length
  2315      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2316      * @throws NullPointerException if <tt>original</tt> is null
  2317      * @since 1.6
  2318      */
  2319     public static int[] copyOf(int[] original, int newLength) {
  2320         int[] copy = new int[newLength];
  2321         System.arraycopy(original, 0, copy, 0,
  2322                          Math.min(original.length, newLength));
  2323         return copy;
  2324     }
  2325 
  2326     /**
  2327      * Copies the specified array, truncating or padding with zeros (if necessary)
  2328      * so the copy has the specified length.  For all indices that are
  2329      * valid in both the original array and the copy, the two arrays will
  2330      * contain identical values.  For any indices that are valid in the
  2331      * copy but not the original, the copy will contain <tt>0L</tt>.
  2332      * Such indices will exist if and only if the specified length
  2333      * is greater than that of the original array.
  2334      *
  2335      * @param original the array to be copied
  2336      * @param newLength the length of the copy to be returned
  2337      * @return a copy of the original array, truncated or padded with zeros
  2338      *     to obtain the specified length
  2339      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2340      * @throws NullPointerException if <tt>original</tt> is null
  2341      * @since 1.6
  2342      */
  2343     public static long[] copyOf(long[] original, int newLength) {
  2344         long[] copy = new long[newLength];
  2345         System.arraycopy(original, 0, copy, 0,
  2346                          Math.min(original.length, newLength));
  2347         return copy;
  2348     }
  2349 
  2350     /**
  2351      * Copies the specified array, truncating or padding with null characters (if necessary)
  2352      * so the copy has the specified length.  For all indices that are valid
  2353      * in both the original array and the copy, the two arrays will contain
  2354      * identical values.  For any indices that are valid in the copy but not
  2355      * the original, the copy will contain <tt>'\\u000'</tt>.  Such indices
  2356      * will exist if and only if the specified length is greater than that of
  2357      * the original array.
  2358      *
  2359      * @param original the array to be copied
  2360      * @param newLength the length of the copy to be returned
  2361      * @return a copy of the original array, truncated or padded with null characters
  2362      *     to obtain the specified length
  2363      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2364      * @throws NullPointerException if <tt>original</tt> is null
  2365      * @since 1.6
  2366      */
  2367     public static char[] copyOf(char[] original, int newLength) {
  2368         char[] copy = new char[newLength];
  2369         System.arraycopy(original, 0, copy, 0,
  2370                          Math.min(original.length, newLength));
  2371         return copy;
  2372     }
  2373 
  2374     /**
  2375      * Copies the specified array, truncating or padding with zeros (if necessary)
  2376      * so the copy has the specified length.  For all indices that are
  2377      * valid in both the original array and the copy, the two arrays will
  2378      * contain identical values.  For any indices that are valid in the
  2379      * copy but not the original, the copy will contain <tt>0f</tt>.
  2380      * Such indices will exist if and only if the specified length
  2381      * is greater than that of the original array.
  2382      *
  2383      * @param original the array to be copied
  2384      * @param newLength the length of the copy to be returned
  2385      * @return a copy of the original array, truncated or padded with zeros
  2386      *     to obtain the specified length
  2387      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2388      * @throws NullPointerException if <tt>original</tt> is null
  2389      * @since 1.6
  2390      */
  2391     public static float[] copyOf(float[] original, int newLength) {
  2392         float[] copy = new float[newLength];
  2393         System.arraycopy(original, 0, copy, 0,
  2394                          Math.min(original.length, newLength));
  2395         return copy;
  2396     }
  2397 
  2398     /**
  2399      * Copies the specified array, truncating or padding with zeros (if necessary)
  2400      * so the copy has the specified length.  For all indices that are
  2401      * valid in both the original array and the copy, the two arrays will
  2402      * contain identical values.  For any indices that are valid in the
  2403      * copy but not the original, the copy will contain <tt>0d</tt>.
  2404      * Such indices will exist if and only if the specified length
  2405      * is greater than that of the original array.
  2406      *
  2407      * @param original the array to be copied
  2408      * @param newLength the length of the copy to be returned
  2409      * @return a copy of the original array, truncated or padded with zeros
  2410      *     to obtain the specified length
  2411      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2412      * @throws NullPointerException if <tt>original</tt> is null
  2413      * @since 1.6
  2414      */
  2415     public static double[] copyOf(double[] original, int newLength) {
  2416         double[] copy = new double[newLength];
  2417         System.arraycopy(original, 0, copy, 0,
  2418                          Math.min(original.length, newLength));
  2419         return copy;
  2420     }
  2421 
  2422     /**
  2423      * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
  2424      * so the copy has the specified length.  For all indices that are
  2425      * valid in both the original array and the copy, the two arrays will
  2426      * contain identical values.  For any indices that are valid in the
  2427      * copy but not the original, the copy will contain <tt>false</tt>.
  2428      * Such indices will exist if and only if the specified length
  2429      * is greater than that of the original array.
  2430      *
  2431      * @param original the array to be copied
  2432      * @param newLength the length of the copy to be returned
  2433      * @return a copy of the original array, truncated or padded with false elements
  2434      *     to obtain the specified length
  2435      * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
  2436      * @throws NullPointerException if <tt>original</tt> is null
  2437      * @since 1.6
  2438      */
  2439     public static boolean[] copyOf(boolean[] original, int newLength) {
  2440         boolean[] copy = new boolean[newLength];
  2441         System.arraycopy(original, 0, copy, 0,
  2442                          Math.min(original.length, newLength));
  2443         return copy;
  2444     }
  2445 
  2446     /**
  2447      * Copies the specified range of the specified array into a new array.
  2448      * The initial index of the range (<tt>from</tt>) must lie between zero
  2449      * and <tt>original.length</tt>, inclusive.  The value at
  2450      * <tt>original[from]</tt> is placed into the initial element of the copy
  2451      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2452      * Values from subsequent elements in the original array are placed into
  2453      * subsequent elements in the copy.  The final index of the range
  2454      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2455      * may be greater than <tt>original.length</tt>, in which case
  2456      * <tt>null</tt> is placed in all elements of the copy whose index is
  2457      * greater than or equal to <tt>original.length - from</tt>.  The length
  2458      * of the returned array will be <tt>to - from</tt>.
  2459      * <p>
  2460      * The resulting array is of exactly the same class as the original array.
  2461      *
  2462      * @param original the array from which a range is to be copied
  2463      * @param from the initial index of the range to be copied, inclusive
  2464      * @param to the final index of the range to be copied, exclusive.
  2465      *     (This index may lie outside the array.)
  2466      * @return a new array containing the specified range from the original array,
  2467      *     truncated or padded with nulls to obtain the required length
  2468      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2469      *     or {@code from > original.length}
  2470      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2471      * @throws NullPointerException if <tt>original</tt> is null
  2472      * @since 1.6
  2473      */
  2474     public static <T> T[] copyOfRange(T[] original, int from, int to) {
  2475         return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
  2476     }
  2477 
  2478     /**
  2479      * Copies the specified range of the specified array into a new array.
  2480      * The initial index of the range (<tt>from</tt>) must lie between zero
  2481      * and <tt>original.length</tt>, inclusive.  The value at
  2482      * <tt>original[from]</tt> is placed into the initial element of the copy
  2483      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2484      * Values from subsequent elements in the original array are placed into
  2485      * subsequent elements in the copy.  The final index of the range
  2486      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2487      * may be greater than <tt>original.length</tt>, in which case
  2488      * <tt>null</tt> is placed in all elements of the copy whose index is
  2489      * greater than or equal to <tt>original.length - from</tt>.  The length
  2490      * of the returned array will be <tt>to - from</tt>.
  2491      * The resulting array is of the class <tt>newType</tt>.
  2492      *
  2493      * @param original the array from which a range is to be copied
  2494      * @param from the initial index of the range to be copied, inclusive
  2495      * @param to the final index of the range to be copied, exclusive.
  2496      *     (This index may lie outside the array.)
  2497      * @param newType the class of the copy to be returned
  2498      * @return a new array containing the specified range from the original array,
  2499      *     truncated or padded with nulls to obtain the required length
  2500      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2501      *     or {@code from > original.length}
  2502      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2503      * @throws NullPointerException if <tt>original</tt> is null
  2504      * @throws ArrayStoreException if an element copied from
  2505      *     <tt>original</tt> is not of a runtime type that can be stored in
  2506      *     an array of class <tt>newType</tt>.
  2507      * @since 1.6
  2508      */
  2509     public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
  2510         int newLength = to - from;
  2511         if (newLength < 0)
  2512             throw new IllegalArgumentException(from + " > " + to);
  2513         T[] copy = ((Object)newType == (Object)Object[].class)
  2514             ? (T[]) new Object[newLength]
  2515             : (T[]) Array.newInstance(newType.getComponentType(), newLength);
  2516         System.arraycopy(original, from, copy, 0,
  2517                          Math.min(original.length - from, newLength));
  2518         return copy;
  2519     }
  2520 
  2521     /**
  2522      * Copies the specified range of the specified array into a new array.
  2523      * The initial index of the range (<tt>from</tt>) must lie between zero
  2524      * and <tt>original.length</tt>, inclusive.  The value at
  2525      * <tt>original[from]</tt> is placed into the initial element of the copy
  2526      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2527      * Values from subsequent elements in the original array are placed into
  2528      * subsequent elements in the copy.  The final index of the range
  2529      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2530      * may be greater than <tt>original.length</tt>, in which case
  2531      * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
  2532      * greater than or equal to <tt>original.length - from</tt>.  The length
  2533      * of the returned array will be <tt>to - from</tt>.
  2534      *
  2535      * @param original the array from which a range is to be copied
  2536      * @param from the initial index of the range to be copied, inclusive
  2537      * @param to the final index of the range to be copied, exclusive.
  2538      *     (This index may lie outside the array.)
  2539      * @return a new array containing the specified range from the original array,
  2540      *     truncated or padded with zeros to obtain the required length
  2541      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2542      *     or {@code from > original.length}
  2543      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2544      * @throws NullPointerException if <tt>original</tt> is null
  2545      * @since 1.6
  2546      */
  2547     public static byte[] copyOfRange(byte[] original, int from, int to) {
  2548         int newLength = to - from;
  2549         if (newLength < 0)
  2550             throw new IllegalArgumentException(from + " > " + to);
  2551         byte[] copy = new byte[newLength];
  2552         System.arraycopy(original, from, copy, 0,
  2553                          Math.min(original.length - from, newLength));
  2554         return copy;
  2555     }
  2556 
  2557     /**
  2558      * Copies the specified range of the specified array into a new array.
  2559      * The initial index of the range (<tt>from</tt>) must lie between zero
  2560      * and <tt>original.length</tt>, inclusive.  The value at
  2561      * <tt>original[from]</tt> is placed into the initial element of the copy
  2562      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2563      * Values from subsequent elements in the original array are placed into
  2564      * subsequent elements in the copy.  The final index of the range
  2565      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2566      * may be greater than <tt>original.length</tt>, in which case
  2567      * <tt>(short)0</tt> is placed in all elements of the copy whose index is
  2568      * greater than or equal to <tt>original.length - from</tt>.  The length
  2569      * of the returned array will be <tt>to - from</tt>.
  2570      *
  2571      * @param original the array from which a range is to be copied
  2572      * @param from the initial index of the range to be copied, inclusive
  2573      * @param to the final index of the range to be copied, exclusive.
  2574      *     (This index may lie outside the array.)
  2575      * @return a new array containing the specified range from the original array,
  2576      *     truncated or padded with zeros to obtain the required length
  2577      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2578      *     or {@code from > original.length}
  2579      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2580      * @throws NullPointerException if <tt>original</tt> is null
  2581      * @since 1.6
  2582      */
  2583     public static short[] copyOfRange(short[] original, int from, int to) {
  2584         int newLength = to - from;
  2585         if (newLength < 0)
  2586             throw new IllegalArgumentException(from + " > " + to);
  2587         short[] copy = new short[newLength];
  2588         System.arraycopy(original, from, copy, 0,
  2589                          Math.min(original.length - from, newLength));
  2590         return copy;
  2591     }
  2592 
  2593     /**
  2594      * Copies the specified range of the specified array into a new array.
  2595      * The initial index of the range (<tt>from</tt>) must lie between zero
  2596      * and <tt>original.length</tt>, inclusive.  The value at
  2597      * <tt>original[from]</tt> is placed into the initial element of the copy
  2598      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2599      * Values from subsequent elements in the original array are placed into
  2600      * subsequent elements in the copy.  The final index of the range
  2601      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2602      * may be greater than <tt>original.length</tt>, in which case
  2603      * <tt>0</tt> is placed in all elements of the copy whose index is
  2604      * greater than or equal to <tt>original.length - from</tt>.  The length
  2605      * of the returned array will be <tt>to - from</tt>.
  2606      *
  2607      * @param original the array from which a range is to be copied
  2608      * @param from the initial index of the range to be copied, inclusive
  2609      * @param to the final index of the range to be copied, exclusive.
  2610      *     (This index may lie outside the array.)
  2611      * @return a new array containing the specified range from the original array,
  2612      *     truncated or padded with zeros to obtain the required length
  2613      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2614      *     or {@code from > original.length}
  2615      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2616      * @throws NullPointerException if <tt>original</tt> is null
  2617      * @since 1.6
  2618      */
  2619     public static int[] copyOfRange(int[] original, int from, int to) {
  2620         int newLength = to - from;
  2621         if (newLength < 0)
  2622             throw new IllegalArgumentException(from + " > " + to);
  2623         int[] copy = new int[newLength];
  2624         System.arraycopy(original, from, copy, 0,
  2625                          Math.min(original.length - from, newLength));
  2626         return copy;
  2627     }
  2628 
  2629     /**
  2630      * Copies the specified range of the specified array into a new array.
  2631      * The initial index of the range (<tt>from</tt>) must lie between zero
  2632      * and <tt>original.length</tt>, inclusive.  The value at
  2633      * <tt>original[from]</tt> is placed into the initial element of the copy
  2634      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2635      * Values from subsequent elements in the original array are placed into
  2636      * subsequent elements in the copy.  The final index of the range
  2637      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2638      * may be greater than <tt>original.length</tt>, in which case
  2639      * <tt>0L</tt> is placed in all elements of the copy whose index is
  2640      * greater than or equal to <tt>original.length - from</tt>.  The length
  2641      * of the returned array will be <tt>to - from</tt>.
  2642      *
  2643      * @param original the array from which a range is to be copied
  2644      * @param from the initial index of the range to be copied, inclusive
  2645      * @param to the final index of the range to be copied, exclusive.
  2646      *     (This index may lie outside the array.)
  2647      * @return a new array containing the specified range from the original array,
  2648      *     truncated or padded with zeros to obtain the required length
  2649      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2650      *     or {@code from > original.length}
  2651      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2652      * @throws NullPointerException if <tt>original</tt> is null
  2653      * @since 1.6
  2654      */
  2655     public static long[] copyOfRange(long[] original, int from, int to) {
  2656         int newLength = to - from;
  2657         if (newLength < 0)
  2658             throw new IllegalArgumentException(from + " > " + to);
  2659         long[] copy = new long[newLength];
  2660         System.arraycopy(original, from, copy, 0,
  2661                          Math.min(original.length - from, newLength));
  2662         return copy;
  2663     }
  2664 
  2665     /**
  2666      * Copies the specified range of the specified array into a new array.
  2667      * The initial index of the range (<tt>from</tt>) must lie between zero
  2668      * and <tt>original.length</tt>, inclusive.  The value at
  2669      * <tt>original[from]</tt> is placed into the initial element of the copy
  2670      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2671      * Values from subsequent elements in the original array are placed into
  2672      * subsequent elements in the copy.  The final index of the range
  2673      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2674      * may be greater than <tt>original.length</tt>, in which case
  2675      * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
  2676      * greater than or equal to <tt>original.length - from</tt>.  The length
  2677      * of the returned array will be <tt>to - from</tt>.
  2678      *
  2679      * @param original the array from which a range is to be copied
  2680      * @param from the initial index of the range to be copied, inclusive
  2681      * @param to the final index of the range to be copied, exclusive.
  2682      *     (This index may lie outside the array.)
  2683      * @return a new array containing the specified range from the original array,
  2684      *     truncated or padded with null characters to obtain the required length
  2685      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2686      *     or {@code from > original.length}
  2687      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2688      * @throws NullPointerException if <tt>original</tt> is null
  2689      * @since 1.6
  2690      */
  2691     public static char[] copyOfRange(char[] original, int from, int to) {
  2692         int newLength = to - from;
  2693         if (newLength < 0)
  2694             throw new IllegalArgumentException(from + " > " + to);
  2695         char[] copy = new char[newLength];
  2696         System.arraycopy(original, from, copy, 0,
  2697                          Math.min(original.length - from, newLength));
  2698         return copy;
  2699     }
  2700 
  2701     /**
  2702      * Copies the specified range of the specified array into a new array.
  2703      * The initial index of the range (<tt>from</tt>) must lie between zero
  2704      * and <tt>original.length</tt>, inclusive.  The value at
  2705      * <tt>original[from]</tt> is placed into the initial element of the copy
  2706      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2707      * Values from subsequent elements in the original array are placed into
  2708      * subsequent elements in the copy.  The final index of the range
  2709      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2710      * may be greater than <tt>original.length</tt>, in which case
  2711      * <tt>0f</tt> is placed in all elements of the copy whose index is
  2712      * greater than or equal to <tt>original.length - from</tt>.  The length
  2713      * of the returned array will be <tt>to - from</tt>.
  2714      *
  2715      * @param original the array from which a range is to be copied
  2716      * @param from the initial index of the range to be copied, inclusive
  2717      * @param to the final index of the range to be copied, exclusive.
  2718      *     (This index may lie outside the array.)
  2719      * @return a new array containing the specified range from the original array,
  2720      *     truncated or padded with zeros to obtain the required length
  2721      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2722      *     or {@code from > original.length}
  2723      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2724      * @throws NullPointerException if <tt>original</tt> is null
  2725      * @since 1.6
  2726      */
  2727     public static float[] copyOfRange(float[] original, int from, int to) {
  2728         int newLength = to - from;
  2729         if (newLength < 0)
  2730             throw new IllegalArgumentException(from + " > " + to);
  2731         float[] copy = new float[newLength];
  2732         System.arraycopy(original, from, copy, 0,
  2733                          Math.min(original.length - from, newLength));
  2734         return copy;
  2735     }
  2736 
  2737     /**
  2738      * Copies the specified range of the specified array into a new array.
  2739      * The initial index of the range (<tt>from</tt>) must lie between zero
  2740      * and <tt>original.length</tt>, inclusive.  The value at
  2741      * <tt>original[from]</tt> is placed into the initial element of the copy
  2742      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2743      * Values from subsequent elements in the original array are placed into
  2744      * subsequent elements in the copy.  The final index of the range
  2745      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2746      * may be greater than <tt>original.length</tt>, in which case
  2747      * <tt>0d</tt> is placed in all elements of the copy whose index is
  2748      * greater than or equal to <tt>original.length - from</tt>.  The length
  2749      * of the returned array will be <tt>to - from</tt>.
  2750      *
  2751      * @param original the array from which a range is to be copied
  2752      * @param from the initial index of the range to be copied, inclusive
  2753      * @param to the final index of the range to be copied, exclusive.
  2754      *     (This index may lie outside the array.)
  2755      * @return a new array containing the specified range from the original array,
  2756      *     truncated or padded with zeros to obtain the required length
  2757      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2758      *     or {@code from > original.length}
  2759      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2760      * @throws NullPointerException if <tt>original</tt> is null
  2761      * @since 1.6
  2762      */
  2763     public static double[] copyOfRange(double[] original, int from, int to) {
  2764         int newLength = to - from;
  2765         if (newLength < 0)
  2766             throw new IllegalArgumentException(from + " > " + to);
  2767         double[] copy = new double[newLength];
  2768         System.arraycopy(original, from, copy, 0,
  2769                          Math.min(original.length - from, newLength));
  2770         return copy;
  2771     }
  2772 
  2773     /**
  2774      * Copies the specified range of the specified array into a new array.
  2775      * The initial index of the range (<tt>from</tt>) must lie between zero
  2776      * and <tt>original.length</tt>, inclusive.  The value at
  2777      * <tt>original[from]</tt> is placed into the initial element of the copy
  2778      * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
  2779      * Values from subsequent elements in the original array are placed into
  2780      * subsequent elements in the copy.  The final index of the range
  2781      * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
  2782      * may be greater than <tt>original.length</tt>, in which case
  2783      * <tt>false</tt> is placed in all elements of the copy whose index is
  2784      * greater than or equal to <tt>original.length - from</tt>.  The length
  2785      * of the returned array will be <tt>to - from</tt>.
  2786      *
  2787      * @param original the array from which a range is to be copied
  2788      * @param from the initial index of the range to be copied, inclusive
  2789      * @param to the final index of the range to be copied, exclusive.
  2790      *     (This index may lie outside the array.)
  2791      * @return a new array containing the specified range from the original array,
  2792      *     truncated or padded with false elements to obtain the required length
  2793      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
  2794      *     or {@code from > original.length}
  2795      * @throws IllegalArgumentException if <tt>from &gt; to</tt>
  2796      * @throws NullPointerException if <tt>original</tt> is null
  2797      * @since 1.6
  2798      */
  2799     public static boolean[] copyOfRange(boolean[] original, int from, int to) {
  2800         int newLength = to - from;
  2801         if (newLength < 0)
  2802             throw new IllegalArgumentException(from + " > " + to);
  2803         boolean[] copy = new boolean[newLength];
  2804         System.arraycopy(original, from, copy, 0,
  2805                          Math.min(original.length - from, newLength));
  2806         return copy;
  2807     }
  2808 
  2809     // Misc
  2810 
  2811     /**
  2812      * Returns a fixed-size list backed by the specified array.  (Changes to
  2813      * the returned list "write through" to the array.)  This method acts
  2814      * as bridge between array-based and collection-based APIs, in
  2815      * combination with {@link Collection#toArray}.  The returned list is
  2816      * serializable and implements {@link RandomAccess}.
  2817      *
  2818      * <p>This method also provides a convenient way to create a fixed-size
  2819      * list initialized to contain several elements:
  2820      * <pre>
  2821      *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
  2822      * </pre>
  2823      *
  2824      * @param a the array by which the list will be backed
  2825      * @return a list view of the specified array
  2826      */
  2827     @SafeVarargs
  2828     public static <T> List<T> asList(T... a) {
  2829         return new ArrayList<>(a);
  2830     }
  2831 
  2832     /**
  2833      * @serial include
  2834      */
  2835     private static class ArrayList<E> extends AbstractList<E>
  2836         implements RandomAccess, java.io.Serializable
  2837     {
  2838         private static final long serialVersionUID = -2764017481108945198L;
  2839         private final E[] a;
  2840 
  2841         ArrayList(E[] array) {
  2842             if (array==null)
  2843                 throw new NullPointerException();
  2844             a = array;
  2845         }
  2846 
  2847         public int size() {
  2848             return a.length;
  2849         }
  2850 
  2851         public Object[] toArray() {
  2852             return a.clone();
  2853         }
  2854 
  2855         public <T> T[] toArray(T[] a) {
  2856             int size = size();
  2857             if (a.length < size)
  2858                 return Arrays.copyOf(this.a, size,
  2859                                      (Class<? extends T[]>) a.getClass());
  2860             System.arraycopy(this.a, 0, a, 0, size);
  2861             if (a.length > size)
  2862                 a[size] = null;
  2863             return a;
  2864         }
  2865 
  2866         public E get(int index) {
  2867             return a[index];
  2868         }
  2869 
  2870         public E set(int index, E element) {
  2871             E oldValue = a[index];
  2872             a[index] = element;
  2873             return oldValue;
  2874         }
  2875 
  2876         public int indexOf(Object o) {
  2877             if (o==null) {
  2878                 for (int i=0; i<a.length; i++)
  2879                     if (a[i]==null)
  2880                         return i;
  2881             } else {
  2882                 for (int i=0; i<a.length; i++)
  2883                     if (o.equals(a[i]))
  2884                         return i;
  2885             }
  2886             return -1;
  2887         }
  2888 
  2889         public boolean contains(Object o) {
  2890             return indexOf(o) != -1;
  2891         }
  2892     }
  2893 
  2894     /**
  2895      * Returns a hash code based on the contents of the specified array.
  2896      * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
  2897      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  2898      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  2899      *
  2900      * <p>The value returned by this method is the same value that would be
  2901      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  2902      * method on a {@link List} containing a sequence of {@link Long}
  2903      * instances representing the elements of <tt>a</tt> in the same order.
  2904      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  2905      *
  2906      * @param a the array whose hash value to compute
  2907      * @return a content-based hash code for <tt>a</tt>
  2908      * @since 1.5
  2909      */
  2910     public static int hashCode(long a[]) {
  2911         if (a == null)
  2912             return 0;
  2913 
  2914         int result = 1;
  2915         for (long element : a) {
  2916             int elementHash = (int)(element ^ (element >>> 32));
  2917             result = 31 * result + elementHash;
  2918         }
  2919 
  2920         return result;
  2921     }
  2922 
  2923     /**
  2924      * Returns a hash code based on the contents of the specified array.
  2925      * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
  2926      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  2927      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  2928      *
  2929      * <p>The value returned by this method is the same value that would be
  2930      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  2931      * method on a {@link List} containing a sequence of {@link Integer}
  2932      * instances representing the elements of <tt>a</tt> in the same order.
  2933      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  2934      *
  2935      * @param a the array whose hash value to compute
  2936      * @return a content-based hash code for <tt>a</tt>
  2937      * @since 1.5
  2938      */
  2939     public static int hashCode(int a[]) {
  2940         if (a == null)
  2941             return 0;
  2942 
  2943         int result = 1;
  2944         for (int element : a)
  2945             result = 31 * result + element;
  2946 
  2947         return result;
  2948     }
  2949 
  2950     /**
  2951      * Returns a hash code based on the contents of the specified array.
  2952      * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
  2953      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  2954      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  2955      *
  2956      * <p>The value returned by this method is the same value that would be
  2957      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  2958      * method on a {@link List} containing a sequence of {@link Short}
  2959      * instances representing the elements of <tt>a</tt> in the same order.
  2960      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  2961      *
  2962      * @param a the array whose hash value to compute
  2963      * @return a content-based hash code for <tt>a</tt>
  2964      * @since 1.5
  2965      */
  2966     public static int hashCode(short a[]) {
  2967         if (a == null)
  2968             return 0;
  2969 
  2970         int result = 1;
  2971         for (short element : a)
  2972             result = 31 * result + element;
  2973 
  2974         return result;
  2975     }
  2976 
  2977     /**
  2978      * Returns a hash code based on the contents of the specified array.
  2979      * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
  2980      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  2981      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  2982      *
  2983      * <p>The value returned by this method is the same value that would be
  2984      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  2985      * method on a {@link List} containing a sequence of {@link Character}
  2986      * instances representing the elements of <tt>a</tt> in the same order.
  2987      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  2988      *
  2989      * @param a the array whose hash value to compute
  2990      * @return a content-based hash code for <tt>a</tt>
  2991      * @since 1.5
  2992      */
  2993     public static int hashCode(char a[]) {
  2994         if (a == null)
  2995             return 0;
  2996 
  2997         int result = 1;
  2998         for (char element : a)
  2999             result = 31 * result + element;
  3000 
  3001         return result;
  3002     }
  3003 
  3004     /**
  3005      * Returns a hash code based on the contents of the specified array.
  3006      * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
  3007      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  3008      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  3009      *
  3010      * <p>The value returned by this method is the same value that would be
  3011      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  3012      * method on a {@link List} containing a sequence of {@link Byte}
  3013      * instances representing the elements of <tt>a</tt> in the same order.
  3014      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  3015      *
  3016      * @param a the array whose hash value to compute
  3017      * @return a content-based hash code for <tt>a</tt>
  3018      * @since 1.5
  3019      */
  3020     public static int hashCode(byte a[]) {
  3021         if (a == null)
  3022             return 0;
  3023 
  3024         int result = 1;
  3025         for (byte element : a)
  3026             result = 31 * result + element;
  3027 
  3028         return result;
  3029     }
  3030 
  3031     /**
  3032      * Returns a hash code based on the contents of the specified array.
  3033      * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
  3034      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  3035      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  3036      *
  3037      * <p>The value returned by this method is the same value that would be
  3038      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  3039      * method on a {@link List} containing a sequence of {@link Boolean}
  3040      * instances representing the elements of <tt>a</tt> in the same order.
  3041      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  3042      *
  3043      * @param a the array whose hash value to compute
  3044      * @return a content-based hash code for <tt>a</tt>
  3045      * @since 1.5
  3046      */
  3047     public static int hashCode(boolean a[]) {
  3048         if (a == null)
  3049             return 0;
  3050 
  3051         int result = 1;
  3052         for (boolean element : a)
  3053             result = 31 * result + (element ? 1231 : 1237);
  3054 
  3055         return result;
  3056     }
  3057 
  3058     /**
  3059      * Returns a hash code based on the contents of the specified array.
  3060      * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
  3061      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  3062      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  3063      *
  3064      * <p>The value returned by this method is the same value that would be
  3065      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  3066      * method on a {@link List} containing a sequence of {@link Float}
  3067      * instances representing the elements of <tt>a</tt> in the same order.
  3068      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  3069      *
  3070      * @param a the array whose hash value to compute
  3071      * @return a content-based hash code for <tt>a</tt>
  3072      * @since 1.5
  3073      */
  3074     public static int hashCode(float a[]) {
  3075         if (a == null)
  3076             return 0;
  3077 
  3078         int result = 1;
  3079         for (float element : a)
  3080             result = 31 * result + Float.floatToIntBits(element);
  3081 
  3082         return result;
  3083     }
  3084 
  3085     /**
  3086      * Returns a hash code based on the contents of the specified array.
  3087      * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
  3088      * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
  3089      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  3090      *
  3091      * <p>The value returned by this method is the same value that would be
  3092      * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
  3093      * method on a {@link List} containing a sequence of {@link Double}
  3094      * instances representing the elements of <tt>a</tt> in the same order.
  3095      * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
  3096      *
  3097      * @param a the array whose hash value to compute
  3098      * @return a content-based hash code for <tt>a</tt>
  3099      * @since 1.5
  3100      */
  3101     public static int hashCode(double a[]) {
  3102         if (a == null)
  3103             return 0;
  3104 
  3105         int result = 1;
  3106         for (double element : a) {
  3107             long bits = Double.doubleToLongBits(element);
  3108             result = 31 * result + (int)(bits ^ (bits >>> 32));
  3109         }
  3110         return result;
  3111     }
  3112 
  3113     /**
  3114      * Returns a hash code based on the contents of the specified array.  If
  3115      * the array contains other arrays as elements, the hash code is based on
  3116      * their identities rather than their contents.  It is therefore
  3117      * acceptable to invoke this method on an array that contains itself as an
  3118      * element,  either directly or indirectly through one or more levels of
  3119      * arrays.
  3120      *
  3121      * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
  3122      * <tt>Arrays.equals(a, b)</tt>, it is also the case that
  3123      * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
  3124      *
  3125      * <p>The value returned by this method is equal to the value that would
  3126      * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
  3127      * is <tt>null</tt>, in which case <tt>0</tt> is returned.
  3128      *
  3129      * @param a the array whose content-based hash code to compute
  3130      * @return a content-based hash code for <tt>a</tt>
  3131      * @see #deepHashCode(Object[])
  3132      * @since 1.5
  3133      */
  3134     public static int hashCode(Object a[]) {
  3135         if (a == null)
  3136             return 0;
  3137 
  3138         int result = 1;
  3139 
  3140         for (Object element : a)
  3141             result = 31 * result + (element == null ? 0 : element.hashCode());
  3142 
  3143         return result;
  3144     }
  3145 
  3146     /**
  3147      * Returns a hash code based on the "deep contents" of the specified
  3148      * array.  If the array contains other arrays as elements, the
  3149      * hash code is based on their contents and so on, ad infinitum.
  3150      * It is therefore unacceptable to invoke this method on an array that
  3151      * contains itself as an element, either directly or indirectly through
  3152      * one or more levels of arrays.  The behavior of such an invocation is
  3153      * undefined.
  3154      *
  3155      * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
  3156      * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
  3157      * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
  3158      *
  3159      * <p>The computation of the value returned by this method is similar to
  3160      * that of the value returned by {@link List#hashCode()} on a list
  3161      * containing the same elements as <tt>a</tt> in the same order, with one
  3162      * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
  3163      * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
  3164      * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
  3165      * if <tt>e</tt> is an array of a primitive type, or as by calling
  3166      * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
  3167      * of a reference type.  If <tt>a</tt> is <tt>null</tt>, this method
  3168      * returns 0.
  3169      *
  3170      * @param a the array whose deep-content-based hash code to compute
  3171      * @return a deep-content-based hash code for <tt>a</tt>
  3172      * @see #hashCode(Object[])
  3173      * @since 1.5
  3174      */
  3175     public static int deepHashCode(Object a[]) {
  3176         if (a == null)
  3177             return 0;
  3178 
  3179         int result = 1;
  3180 
  3181         for (Object element : a) {
  3182             int elementHash = 0;
  3183             if (element instanceof Object[])
  3184                 elementHash = deepHashCode((Object[]) element);
  3185             else if (element instanceof byte[])
  3186                 elementHash = hashCode((byte[]) element);
  3187             else if (element instanceof short[])
  3188                 elementHash = hashCode((short[]) element);
  3189             else if (element instanceof int[])
  3190                 elementHash = hashCode((int[]) element);
  3191             else if (element instanceof long[])
  3192                 elementHash = hashCode((long[]) element);
  3193             else if (element instanceof char[])
  3194                 elementHash = hashCode((char[]) element);
  3195             else if (element instanceof float[])
  3196                 elementHash = hashCode((float[]) element);
  3197             else if (element instanceof double[])
  3198                 elementHash = hashCode((double[]) element);
  3199             else if (element instanceof boolean[])
  3200                 elementHash = hashCode((boolean[]) element);
  3201             else if (element != null)
  3202                 elementHash = element.hashCode();
  3203 
  3204             result = 31 * result + elementHash;
  3205         }
  3206 
  3207         return result;
  3208     }
  3209 
  3210     /**
  3211      * Returns <tt>true</tt> if the two specified arrays are <i>deeply
  3212      * equal</i> to one another.  Unlike the {@link #equals(Object[],Object[])}
  3213      * method, this method is appropriate for use with nested arrays of
  3214      * arbitrary depth.
  3215      *
  3216      * <p>Two array references are considered deeply equal if both
  3217      * are <tt>null</tt>, or if they refer to arrays that contain the same
  3218      * number of elements and all corresponding pairs of elements in the two
  3219      * arrays are deeply equal.
  3220      *
  3221      * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
  3222      * deeply equal if any of the following conditions hold:
  3223      * <ul>
  3224      *    <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
  3225      *         types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
  3226      *    <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
  3227      *         type, and the appropriate overloading of
  3228      *         <tt>Arrays.equals(e1, e2)</tt> would return true.
  3229      *    <li> <tt>e1 == e2</tt>
  3230      *    <li> <tt>e1.equals(e2)</tt> would return true.
  3231      * </ul>
  3232      * Note that this definition permits <tt>null</tt> elements at any depth.
  3233      *
  3234      * <p>If either of the specified arrays contain themselves as elements
  3235      * either directly or indirectly through one or more levels of arrays,
  3236      * the behavior of this method is undefined.
  3237      *
  3238      * @param a1 one array to be tested for equality
  3239      * @param a2 the other array to be tested for equality
  3240      * @return <tt>true</tt> if the two arrays are equal
  3241      * @see #equals(Object[],Object[])
  3242      * @see Objects#deepEquals(Object, Object)
  3243      * @since 1.5
  3244      */
  3245     public static boolean deepEquals(Object[] a1, Object[] a2) {
  3246         if (a1 == a2)
  3247             return true;
  3248         if (a1 == null || a2==null)
  3249             return false;
  3250         int length = a1.length;
  3251         if (a2.length != length)
  3252             return false;
  3253 
  3254         for (int i = 0; i < length; i++) {
  3255             Object e1 = a1[i];
  3256             Object e2 = a2[i];
  3257 
  3258             if (e1 == e2)
  3259                 continue;
  3260             if (e1 == null)
  3261                 return false;
  3262 
  3263             // Figure out whether the two elements are equal
  3264             boolean eq = deepEquals0(e1, e2);
  3265 
  3266             if (!eq)
  3267                 return false;
  3268         }
  3269         return true;
  3270     }
  3271 
  3272     static boolean deepEquals0(Object e1, Object e2) {
  3273         assert e1 != null;
  3274         boolean eq;
  3275         if (e1 instanceof Object[] && e2 instanceof Object[])
  3276             eq = deepEquals ((Object[]) e1, (Object[]) e2);
  3277         else if (e1 instanceof byte[] && e2 instanceof byte[])
  3278             eq = equals((byte[]) e1, (byte[]) e2);
  3279         else if (e1 instanceof short[] && e2 instanceof short[])
  3280             eq = equals((short[]) e1, (short[]) e2);
  3281         else if (e1 instanceof int[] && e2 instanceof int[])
  3282             eq = equals((int[]) e1, (int[]) e2);
  3283         else if (e1 instanceof long[] && e2 instanceof long[])
  3284             eq = equals((long[]) e1, (long[]) e2);
  3285         else if (e1 instanceof char[] && e2 instanceof char[])
  3286             eq = equals((char[]) e1, (char[]) e2);
  3287         else if (e1 instanceof float[] && e2 instanceof float[])
  3288             eq = equals((float[]) e1, (float[]) e2);
  3289         else if (e1 instanceof double[] && e2 instanceof double[])
  3290             eq = equals((double[]) e1, (double[]) e2);
  3291         else if (e1 instanceof boolean[] && e2 instanceof boolean[])
  3292             eq = equals((boolean[]) e1, (boolean[]) e2);
  3293         else
  3294             eq = e1.equals(e2);
  3295         return eq;
  3296     }
  3297 
  3298     /**
  3299      * Returns a string representation of the contents of the specified array.
  3300      * The string representation consists of a list of the array's elements,
  3301      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3302      * separated by the characters <tt>", "</tt> (a comma followed by a
  3303      * space).  Elements are converted to strings as by
  3304      * <tt>String.valueOf(long)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
  3305      * is <tt>null</tt>.
  3306      *
  3307      * @param a the array whose string representation to return
  3308      * @return a string representation of <tt>a</tt>
  3309      * @since 1.5
  3310      */
  3311     public static String toString(long[] a) {
  3312         if (a == null)
  3313             return "null";
  3314         int iMax = a.length - 1;
  3315         if (iMax == -1)
  3316             return "[]";
  3317 
  3318         StringBuilder b = new StringBuilder();
  3319         b.append('[');
  3320         for (int i = 0; ; i++) {
  3321             b.append(a[i]);
  3322             if (i == iMax)
  3323                 return b.append(']').toString();
  3324             b.append(", ");
  3325         }
  3326     }
  3327 
  3328     /**
  3329      * Returns a string representation of the contents of the specified array.
  3330      * The string representation consists of a list of the array's elements,
  3331      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3332      * separated by the characters <tt>", "</tt> (a comma followed by a
  3333      * space).  Elements are converted to strings as by
  3334      * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
  3335      * <tt>null</tt>.
  3336      *
  3337      * @param a the array whose string representation to return
  3338      * @return a string representation of <tt>a</tt>
  3339      * @since 1.5
  3340      */
  3341     public static String toString(int[] a) {
  3342         if (a == null)
  3343             return "null";
  3344         int iMax = a.length - 1;
  3345         if (iMax == -1)
  3346             return "[]";
  3347 
  3348         StringBuilder b = new StringBuilder();
  3349         b.append('[');
  3350         for (int i = 0; ; i++) {
  3351             b.append(a[i]);
  3352             if (i == iMax)
  3353                 return b.append(']').toString();
  3354             b.append(", ");
  3355         }
  3356     }
  3357 
  3358     /**
  3359      * Returns a string representation of the contents of the specified array.
  3360      * The string representation consists of a list of the array's elements,
  3361      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3362      * separated by the characters <tt>", "</tt> (a comma followed by a
  3363      * space).  Elements are converted to strings as by
  3364      * <tt>String.valueOf(short)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
  3365      * is <tt>null</tt>.
  3366      *
  3367      * @param a the array whose string representation to return
  3368      * @return a string representation of <tt>a</tt>
  3369      * @since 1.5
  3370      */
  3371     public static String toString(short[] a) {
  3372         if (a == null)
  3373             return "null";
  3374         int iMax = a.length - 1;
  3375         if (iMax == -1)
  3376             return "[]";
  3377 
  3378         StringBuilder b = new StringBuilder();
  3379         b.append('[');
  3380         for (int i = 0; ; i++) {
  3381             b.append(a[i]);
  3382             if (i == iMax)
  3383                 return b.append(']').toString();
  3384             b.append(", ");
  3385         }
  3386     }
  3387 
  3388     /**
  3389      * Returns a string representation of the contents of the specified array.
  3390      * The string representation consists of a list of the array's elements,
  3391      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3392      * separated by the characters <tt>", "</tt> (a comma followed by a
  3393      * space).  Elements are converted to strings as by
  3394      * <tt>String.valueOf(char)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
  3395      * is <tt>null</tt>.
  3396      *
  3397      * @param a the array whose string representation to return
  3398      * @return a string representation of <tt>a</tt>
  3399      * @since 1.5
  3400      */
  3401     public static String toString(char[] a) {
  3402         if (a == null)
  3403             return "null";
  3404         int iMax = a.length - 1;
  3405         if (iMax == -1)
  3406             return "[]";
  3407 
  3408         StringBuilder b = new StringBuilder();
  3409         b.append('[');
  3410         for (int i = 0; ; i++) {
  3411             b.append(a[i]);
  3412             if (i == iMax)
  3413                 return b.append(']').toString();
  3414             b.append(", ");
  3415         }
  3416     }
  3417 
  3418     /**
  3419      * Returns a string representation of the contents of the specified array.
  3420      * The string representation consists of a list of the array's elements,
  3421      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements
  3422      * are separated by the characters <tt>", "</tt> (a comma followed
  3423      * by a space).  Elements are converted to strings as by
  3424      * <tt>String.valueOf(byte)</tt>.  Returns <tt>"null"</tt> if
  3425      * <tt>a</tt> is <tt>null</tt>.
  3426      *
  3427      * @param a the array whose string representation to return
  3428      * @return a string representation of <tt>a</tt>
  3429      * @since 1.5
  3430      */
  3431     public static String toString(byte[] a) {
  3432         if (a == null)
  3433             return "null";
  3434         int iMax = a.length - 1;
  3435         if (iMax == -1)
  3436             return "[]";
  3437 
  3438         StringBuilder b = new StringBuilder();
  3439         b.append('[');
  3440         for (int i = 0; ; i++) {
  3441             b.append(a[i]);
  3442             if (i == iMax)
  3443                 return b.append(']').toString();
  3444             b.append(", ");
  3445         }
  3446     }
  3447 
  3448     /**
  3449      * Returns a string representation of the contents of the specified array.
  3450      * The string representation consists of a list of the array's elements,
  3451      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3452      * separated by the characters <tt>", "</tt> (a comma followed by a
  3453      * space).  Elements are converted to strings as by
  3454      * <tt>String.valueOf(boolean)</tt>.  Returns <tt>"null"</tt> if
  3455      * <tt>a</tt> is <tt>null</tt>.
  3456      *
  3457      * @param a the array whose string representation to return
  3458      * @return a string representation of <tt>a</tt>
  3459      * @since 1.5
  3460      */
  3461     public static String toString(boolean[] a) {
  3462         if (a == null)
  3463             return "null";
  3464         int iMax = a.length - 1;
  3465         if (iMax == -1)
  3466             return "[]";
  3467 
  3468         StringBuilder b = new StringBuilder();
  3469         b.append('[');
  3470         for (int i = 0; ; i++) {
  3471             b.append(a[i]);
  3472             if (i == iMax)
  3473                 return b.append(']').toString();
  3474             b.append(", ");
  3475         }
  3476     }
  3477 
  3478     /**
  3479      * Returns a string representation of the contents of the specified array.
  3480      * The string representation consists of a list of the array's elements,
  3481      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3482      * separated by the characters <tt>", "</tt> (a comma followed by a
  3483      * space).  Elements are converted to strings as by
  3484      * <tt>String.valueOf(float)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
  3485      * is <tt>null</tt>.
  3486      *
  3487      * @param a the array whose string representation to return
  3488      * @return a string representation of <tt>a</tt>
  3489      * @since 1.5
  3490      */
  3491     public static String toString(float[] a) {
  3492         if (a == null)
  3493             return "null";
  3494 
  3495         int iMax = a.length - 1;
  3496         if (iMax == -1)
  3497             return "[]";
  3498 
  3499         StringBuilder b = new StringBuilder();
  3500         b.append('[');
  3501         for (int i = 0; ; i++) {
  3502             b.append(a[i]);
  3503             if (i == iMax)
  3504                 return b.append(']').toString();
  3505             b.append(", ");
  3506         }
  3507     }
  3508 
  3509     /**
  3510      * Returns a string representation of the contents of the specified array.
  3511      * The string representation consists of a list of the array's elements,
  3512      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  3513      * separated by the characters <tt>", "</tt> (a comma followed by a
  3514      * space).  Elements are converted to strings as by
  3515      * <tt>String.valueOf(double)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt>
  3516      * is <tt>null</tt>.
  3517      *
  3518      * @param a the array whose string representation to return
  3519      * @return a string representation of <tt>a</tt>
  3520      * @since 1.5
  3521      */
  3522     public static String toString(double[] a) {
  3523         if (a == null)
  3524             return "null";
  3525         int iMax = a.length - 1;
  3526         if (iMax == -1)
  3527             return "[]";
  3528 
  3529         StringBuilder b = new StringBuilder();
  3530         b.append('[');
  3531         for (int i = 0; ; i++) {
  3532             b.append(a[i]);
  3533             if (i == iMax)
  3534                 return b.append(']').toString();
  3535             b.append(", ");
  3536         }
  3537     }
  3538 
  3539     /**
  3540      * Returns a string representation of the contents of the specified array.
  3541      * If the array contains other arrays as elements, they are converted to
  3542      * strings by the {@link Object#toString} method inherited from
  3543      * <tt>Object</tt>, which describes their <i>identities</i> rather than
  3544      * their contents.
  3545      *
  3546      * <p>The value returned by this method is equal to the value that would
  3547      * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
  3548      * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
  3549      *
  3550      * @param a the array whose string representation to return
  3551      * @return a string representation of <tt>a</tt>
  3552      * @see #deepToString(Object[])
  3553      * @since 1.5
  3554      */
  3555     public static String toString(Object[] a) {
  3556         if (a == null)
  3557             return "null";
  3558 
  3559         int iMax = a.length - 1;
  3560         if (iMax == -1)
  3561             return "[]";
  3562 
  3563         StringBuilder b = new StringBuilder();
  3564         b.append('[');
  3565         for (int i = 0; ; i++) {
  3566             b.append(String.valueOf(a[i]));
  3567             if (i == iMax)
  3568                 return b.append(']').toString();
  3569             b.append(", ");
  3570         }
  3571     }
  3572 
  3573     /**
  3574      * Returns a string representation of the "deep contents" of the specified
  3575      * array.  If the array contains other arrays as elements, the string
  3576      * representation contains their contents and so on.  This method is
  3577      * designed for converting multidimensional arrays to strings.
  3578      *
  3579      * <p>The string representation consists of a list of the array's
  3580      * elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent
  3581      * elements are separated by the characters <tt>", "</tt> (a comma
  3582      * followed by a space).  Elements are converted to strings as by
  3583      * <tt>String.valueOf(Object)</tt>, unless they are themselves
  3584      * arrays.
  3585      *
  3586      * <p>If an element <tt>e</tt> is an array of a primitive type, it is
  3587      * converted to a string as by invoking the appropriate overloading of
  3588      * <tt>Arrays.toString(e)</tt>.  If an element <tt>e</tt> is an array of a
  3589      * reference type, it is converted to a string as by invoking
  3590      * this method recursively.
  3591      *
  3592      * <p>To avoid infinite recursion, if the specified array contains itself
  3593      * as an element, or contains an indirect reference to itself through one
  3594      * or more levels of arrays, the self-reference is converted to the string
  3595      * <tt>"[...]"</tt>.  For example, an array containing only a reference
  3596      * to itself would be rendered as <tt>"[[...]]"</tt>.
  3597      *
  3598      * <p>This method returns <tt>"null"</tt> if the specified array
  3599      * is <tt>null</tt>.
  3600      *
  3601      * @param a the array whose string representation to return
  3602      * @return a string representation of <tt>a</tt>
  3603      * @see #toString(Object[])
  3604      * @since 1.5
  3605      */
  3606     public static String deepToString(Object[] a) {
  3607         if (a == null)
  3608             return "null";
  3609 
  3610         int bufLen = 20 * a.length;
  3611         if (a.length != 0 && bufLen <= 0)
  3612             bufLen = Integer.MAX_VALUE;
  3613         StringBuilder buf = new StringBuilder(bufLen);
  3614         deepToString(a, buf, new HashSet<Object[]>());
  3615         return buf.toString();
  3616     }
  3617 
  3618     private static void deepToString(Object[] a, StringBuilder buf,
  3619                                      Set<Object[]> dejaVu) {
  3620         if (a == null) {
  3621             buf.append("null");
  3622             return;
  3623         }
  3624         int iMax = a.length - 1;
  3625         if (iMax == -1) {
  3626             buf.append("[]");
  3627             return;
  3628         }
  3629 
  3630         dejaVu.add(a);
  3631         buf.append('[');
  3632         for (int i = 0; ; i++) {
  3633 
  3634             Object element = a[i];
  3635             if (element == null) {
  3636                 buf.append("null");
  3637             } else {
  3638                 Class eClass = element.getClass();
  3639 
  3640                 if (eClass.isArray()) {
  3641                     if (eClass == byte[].class)
  3642                         buf.append(toString((byte[]) element));
  3643                     else if (eClass == short[].class)
  3644                         buf.append(toString((short[]) element));
  3645                     else if (eClass == int[].class)
  3646                         buf.append(toString((int[]) element));
  3647                     else if (eClass == long[].class)
  3648                         buf.append(toString((long[]) element));
  3649                     else if (eClass == char[].class)
  3650                         buf.append(toString((char[]) element));
  3651                     else if (eClass == float[].class)
  3652                         buf.append(toString((float[]) element));
  3653                     else if (eClass == double[].class)
  3654                         buf.append(toString((double[]) element));
  3655                     else if (eClass == boolean[].class)
  3656                         buf.append(toString((boolean[]) element));
  3657                     else { // element is an array of object references
  3658                         if (dejaVu.contains(element))
  3659                             buf.append("[...]");
  3660                         else
  3661                             deepToString((Object[])element, buf, dejaVu);
  3662                     }
  3663                 } else {  // element is non-null and not an array
  3664                     buf.append(element.toString());
  3665                 }
  3666             }
  3667             if (i == iMax)
  3668                 break;
  3669             buf.append(", ");
  3670         }
  3671         buf.append(']');
  3672         dejaVu.remove(a);
  3673     }
  3674 }