< prev index next >

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

Print this page




   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.Array;
  29 import java.util.concurrent.ForkJoinPool;
  30 import java.util.function.BinaryOperator;
  31 import java.util.function.Consumer;
  32 import java.util.function.DoubleBinaryOperator;
  33 import java.util.function.IntBinaryOperator;
  34 import java.util.function.IntFunction;
  35 import java.util.function.IntToDoubleFunction;
  36 import java.util.function.IntToLongFunction;
  37 import java.util.function.IntUnaryOperator;
  38 import java.util.function.LongBinaryOperator;
  39 import java.util.function.UnaryOperator;
  40 import java.util.stream.DoubleStream;
  41 import java.util.stream.IntStream;
  42 import java.util.stream.LongStream;
  43 import java.util.stream.Stream;
  44 import java.util.stream.StreamSupport;
  45 import jdk.internal.HotSpotIntrinsicCandidate;
  46 
  47 /**
  48  * This class contains various methods for manipulating arrays (such as
  49  * sorting and searching). This class also contains a static factory
  50  * that allows arrays to be viewed as lists.
  51  *
  52  * <p>The methods in this class all throw a {@code NullPointerException},
  53  * if the specified array reference is null, except where noted.
  54  *
  55  * <p>The documentation for the methods contained in this class includes
  56  * brief descriptions of the <i>implementations</i>. Such descriptions should
  57  * be regarded as <i>implementation notes</i>, rather than parts of the
  58  * <i>specification</i>. Implementors should feel free to substitute other
  59  * algorithms, so long as the specification itself is adhered to. (For
  60  * example, the algorithm used by {@code sort(Object[])} does not have to be
  61  * a MergeSort, but it does have to be <i>stable</i>.)
  62  *
  63  * <p>This class is a member of the
  64  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  65  * Java Collections Framework</a>.


2569      * @return {@code true} if the two arrays are equal
2570      */
2571     public static boolean equals(long[] a, long[] a2) {
2572         if (a==a2)
2573             return true;
2574         if (a==null || a2==null)
2575             return false;
2576 
2577         int length = a.length;
2578         if (a2.length != length)
2579             return false;
2580 
2581         for (int i=0; i<length; i++)
2582             if (a[i] != a2[i])
2583                 return false;
2584 
2585         return true;
2586     }
2587 
2588     /**

















































2589      * Returns {@code true} if the two specified arrays of ints are
2590      * <i>equal</i> to one another.  Two arrays are considered equal if both
2591      * arrays contain the same number of elements, and all corresponding pairs
2592      * of elements in the two arrays are equal.  In other words, two arrays
2593      * are equal if they contain the same elements in the same order.  Also,
2594      * two array references are considered equal if both are {@code null}.
2595      *
2596      * @param a one array to be tested for equality
2597      * @param a2 the other array to be tested for equality
2598      * @return {@code true} if the two arrays are equal
2599      */
2600     public static boolean equals(int[] a, int[] a2) {
2601         if (a==a2)
2602             return true;
2603         if (a==null || a2==null)
2604             return false;
2605 
2606         int length = a.length;
2607         if (a2.length != length)
2608             return false;
2609 
2610         for (int i=0; i<length; i++)
2611             if (a[i] != a2[i])
2612                 return false;
2613 
2614         return true;
2615     }
2616 
2617     /**

















































2618      * Returns {@code true} if the two specified arrays of shorts are
2619      * <i>equal</i> to one another.  Two arrays are considered equal if both
2620      * arrays contain the same number of elements, and all corresponding pairs
2621      * of elements in the two arrays are equal.  In other words, two arrays
2622      * are equal if they contain the same elements in the same order.  Also,
2623      * two array references are considered equal if both are {@code null}.
2624      *
2625      * @param a one array to be tested for equality
2626      * @param a2 the other array to be tested for equality
2627      * @return {@code true} if the two arrays are equal
2628      */
2629     public static boolean equals(short[] a, short a2[]) {
2630         if (a==a2)
2631             return true;
2632         if (a==null || a2==null)
2633             return false;
2634 
2635         int length = a.length;
2636         if (a2.length != length)
2637             return false;
2638 
2639         for (int i=0; i<length; i++)
2640             if (a[i] != a2[i])
2641                 return false;
2642 
2643         return true;
2644     }
2645 
2646     /**

















































2647      * Returns {@code true} if the two specified arrays of chars are
2648      * <i>equal</i> to one another.  Two arrays are considered equal if both
2649      * arrays contain the same number of elements, and all corresponding pairs
2650      * of elements in the two arrays are equal.  In other words, two arrays
2651      * are equal if they contain the same elements in the same order.  Also,
2652      * two array references are considered equal if both are {@code null}.
2653      *
2654      * @param a one array to be tested for equality
2655      * @param a2 the other array to be tested for equality
2656      * @return {@code true} if the two arrays are equal
2657      */
2658     @HotSpotIntrinsicCandidate
2659     public static boolean equals(char[] a, char[] a2) {
2660         if (a==a2)
2661             return true;
2662         if (a==null || a2==null)
2663             return false;
2664 
2665         int length = a.length;
2666         if (a2.length != length)
2667             return false;
2668 
2669         for (int i=0; i<length; i++)
2670             if (a[i] != a2[i])
2671                 return false;
2672 
2673         return true;
2674     }
2675 
2676     /**

















































2677      * Returns {@code true} if the two specified arrays of bytes are
2678      * <i>equal</i> to one another.  Two arrays are considered equal if both
2679      * arrays contain the same number of elements, and all corresponding pairs
2680      * of elements in the two arrays are equal.  In other words, two arrays
2681      * are equal if they contain the same elements in the same order.  Also,
2682      * two array references are considered equal if both are {@code null}.
2683      *
2684      * @param a one array to be tested for equality
2685      * @param a2 the other array to be tested for equality
2686      * @return {@code true} if the two arrays are equal
2687      */
2688     public static boolean equals(byte[] a, byte[] a2) {
2689         if (a==a2)
2690             return true;
2691         if (a==null || a2==null)
2692             return false;
2693 
2694         int length = a.length;
2695         if (a2.length != length)
2696             return false;
2697 
2698         for (int i=0; i<length; i++)
2699             if (a[i] != a2[i])
2700                 return false;
2701 
2702         return true;
2703     }
2704 
2705     /**

















































2706      * Returns {@code true} if the two specified arrays of booleans are
2707      * <i>equal</i> to one another.  Two arrays are considered equal if both
2708      * arrays contain the same number of elements, and all corresponding pairs
2709      * of elements in the two arrays are equal.  In other words, two arrays
2710      * are equal if they contain the same elements in the same order.  Also,
2711      * two array references are considered equal if both are {@code null}.
2712      *
2713      * @param a one array to be tested for equality
2714      * @param a2 the other array to be tested for equality
2715      * @return {@code true} if the two arrays are equal
2716      */
2717     public static boolean equals(boolean[] a, boolean[] a2) {
2718         if (a==a2)
2719             return true;
2720         if (a==null || a2==null)
2721             return false;
2722 
2723         int length = a.length;
2724         if (a2.length != length)
2725             return false;
2726 
2727         for (int i=0; i<length; i++)
2728             if (a[i] != a2[i])
2729                 return false;
2730 
2731         return true;
2732     }
2733 
2734     /**

















































2735      * Returns {@code true} if the two specified arrays of doubles are
2736      * <i>equal</i> to one another.  Two arrays are considered equal if both
2737      * arrays contain the same number of elements, and all corresponding pairs
2738      * of elements in the two arrays are equal.  In other words, two arrays
2739      * are equal if they contain the same elements in the same order.  Also,
2740      * two array references are considered equal if both are {@code null}.
2741      *
2742      * Two doubles {@code d1} and {@code d2} are considered equal if:
2743      * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
2744      * (Unlike the {@code ==} operator, this method considers
2745      * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
2746      *
2747      * @param a one array to be tested for equality
2748      * @param a2 the other array to be tested for equality
2749      * @return {@code true} if the two arrays are equal
2750      * @see Double#equals(Object)
2751      */
2752     public static boolean equals(double[] a, double[] a2) {
2753         if (a==a2)
2754             return true;
2755         if (a==null || a2==null)
2756             return false;
2757 
2758         int length = a.length;
2759         if (a2.length != length)
2760             return false;
2761 
2762         for (int i=0; i<length; i++)
2763             if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))






















































2764                 return false;
2765 







2766         return true;
2767     }
2768 
2769     /**
2770      * Returns {@code true} if the two specified arrays of floats are
2771      * <i>equal</i> to one another.  Two arrays are considered equal if both
2772      * arrays contain the same number of elements, and all corresponding pairs
2773      * of elements in the two arrays are equal.  In other words, two arrays
2774      * are equal if they contain the same elements in the same order.  Also,
2775      * two array references are considered equal if both are {@code null}.
2776      *
2777      * Two floats {@code f1} and {@code f2} are considered equal if:
2778      * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
2779      * (Unlike the {@code ==} operator, this method considers
2780      * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
2781      *
2782      * @param a one array to be tested for equality
2783      * @param a2 the other array to be tested for equality
2784      * @return {@code true} if the two arrays are equal
2785      * @see Float#equals(Object)
2786      */
2787     public static boolean equals(float[] a, float[] a2) {
2788         if (a==a2)
2789             return true;
2790         if (a==null || a2==null)
2791             return false;
2792 
2793         int length = a.length;
2794         if (a2.length != length)
2795             return false;
2796 
2797         for (int i=0; i<length; i++)
2798             if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))




























































2799                 return false;

2800 
2801         return true;
2802     }
2803 
2804     /**
2805      * Returns {@code true} if the two specified arrays of Objects are
2806      * <i>equal</i> to one another.  The two arrays are considered equal if
2807      * both arrays contain the same number of elements, and all corresponding
2808      * pairs of elements in the two arrays are equal.  Two objects {@code e1}
2809      * and {@code e2} are considered <i>equal</i> if
2810      * {@code Objects.equals(e1, e2)}.
2811      * In other words, the two arrays are equal if
2812      * they contain the same elements in the same order.  Also, two array
2813      * references are considered equal if both are {@code null}.
2814      *
2815      * @param a one array to be tested for equality
2816      * @param a2 the other array to be tested for equality
2817      * @return {@code true} if the two arrays are equal
2818      */
2819     public static boolean equals(Object[] a, Object[] a2) {
2820         if (a==a2)
2821             return true;
2822         if (a==null || a2==null)
2823             return false;
2824 
2825         int length = a.length;
2826         if (a2.length != length)
2827             return false;
2828 
2829         for (int i=0; i<length; i++) {
2830             Object o1 = a[i];
2831             Object o2 = a2[i];
2832             if (!(o1==null ? o2==null : o1.equals(o2)))



















































2833                 return false;
2834         }
2835 
2836         return true;
2837     }
2838 
2839     // Filling
2840 
2841     /**
2842      * Assigns the specified long value to each element of the specified array
2843      * of longs.
2844      *
2845      * @param a the array to be filled
2846      * @param val the value to be stored in all elements of the array
2847      */
2848     public static void fill(long[] a, long val) {
2849         for (int i = 0, len = a.length; i < len; i++)
2850             a[i] = val;
2851     }
2852 


5168         return stream(array, 0, array.length);
5169     }
5170 
5171     /**
5172      * Returns a sequential {@link DoubleStream} with the specified range of the
5173      * specified array as its source.
5174      *
5175      * @param array the array, assumed to be unmodified during use
5176      * @param startInclusive the first index to cover, inclusive
5177      * @param endExclusive index immediately past the last index to cover
5178      * @return a {@code DoubleStream} for the array range
5179      * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5180      *         negative, {@code endExclusive} is less than
5181      *         {@code startInclusive}, or {@code endExclusive} is greater than
5182      *         the array size
5183      * @since 1.8
5184      */
5185     public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
5186         return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
5187     }





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































5188 }


   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 jdk.internal.HotSpotIntrinsicCandidate;
  29 
  30 import java.lang.reflect.Array;
  31 import java.util.concurrent.ForkJoinPool;
  32 import java.util.function.BinaryOperator;
  33 import java.util.function.Consumer;
  34 import java.util.function.DoubleBinaryOperator;
  35 import java.util.function.IntBinaryOperator;
  36 import java.util.function.IntFunction;
  37 import java.util.function.IntToDoubleFunction;
  38 import java.util.function.IntToLongFunction;
  39 import java.util.function.IntUnaryOperator;
  40 import java.util.function.LongBinaryOperator;
  41 import java.util.function.UnaryOperator;
  42 import java.util.stream.DoubleStream;
  43 import java.util.stream.IntStream;
  44 import java.util.stream.LongStream;
  45 import java.util.stream.Stream;
  46 import java.util.stream.StreamSupport;

  47 
  48 /**
  49  * This class contains various methods for manipulating arrays (such as
  50  * sorting and searching). This class also contains a static factory
  51  * that allows arrays to be viewed as lists.
  52  *
  53  * <p>The methods in this class all throw a {@code NullPointerException},
  54  * if the specified array reference is null, except where noted.
  55  *
  56  * <p>The documentation for the methods contained in this class includes
  57  * brief descriptions of the <i>implementations</i>. Such descriptions should
  58  * be regarded as <i>implementation notes</i>, rather than parts of the
  59  * <i>specification</i>. Implementors should feel free to substitute other
  60  * algorithms, so long as the specification itself is adhered to. (For
  61  * example, the algorithm used by {@code sort(Object[])} does not have to be
  62  * a MergeSort, but it does have to be <i>stable</i>.)
  63  *
  64  * <p>This class is a member of the
  65  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  66  * Java Collections Framework</a>.


2570      * @return {@code true} if the two arrays are equal
2571      */
2572     public static boolean equals(long[] a, long[] a2) {
2573         if (a==a2)
2574             return true;
2575         if (a==null || a2==null)
2576             return false;
2577 
2578         int length = a.length;
2579         if (a2.length != length)
2580             return false;
2581 
2582         for (int i=0; i<length; i++)
2583             if (a[i] != a2[i])
2584                 return false;
2585 
2586         return true;
2587     }
2588 
2589     /**
2590      * Returns true if the two specified arrays of longs, over the specified
2591      * ranges, are <i>equal</i> to one another.
2592      *
2593      * <p>Two arrays are considered equal if the number of elements covered by
2594      * each range is the same, and all corresponding pairs of elements over the
2595      * specified ranges in the two arrays are equal.  In other words, two arrays
2596      * are equal if they contain, over the specified ranges, the same elements
2597      * in the same order.
2598      *
2599      * @param a the first array to be tested for equality
2600      * @param aFromIndex the index (inclusive) of the first element in the
2601      *                   first array to be tested
2602      * @param aToIndex the index (exclusive) of the last element in the
2603      *                 first array to be tested
2604      * @param b the second array to be tested fro equality
2605      * @param bFromIndex the index (inclusive) of the first element in the
2606      *                   second array to be tested
2607      * @param bToIndex the index (exclusive) of the last element in the
2608      *                 second array to be tested
2609      * @return {@code true} if the two arrays, over the specified ranges, are
2610      *         equal
2611      * @throws IllegalArgumentException
2612      *         if {@code aFromIndex > aToIndex} or
2613      *         if {@code bFromIndex > bToIndex}
2614      * @throws ArrayIndexOutOfBoundsException
2615      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
2616      *         if {@code bFromIndex < 0 or bToIndex > b.length}
2617      * @throws NullPointerException
2618      *         if either array is {@code null}
2619      * @since 9
2620      */
2621     public static boolean equals(long[] a, int aFromIndex, int aToIndex,
2622                                  long[] b, int bFromIndex, int bToIndex) {
2623         rangeCheck(a.length, aFromIndex, aToIndex);
2624         rangeCheck(b.length, bFromIndex, bToIndex);
2625 
2626         int aLength = aToIndex - aFromIndex;
2627         int bLength = bToIndex - bFromIndex;
2628         if (aLength != bLength)
2629             return false;
2630 
2631         for (int i = 0; i < aLength; i++)
2632             if (a[aFromIndex++] != b[bFromIndex++])
2633                 return false;
2634 
2635         return true;
2636     }
2637 
2638     /**
2639      * Returns {@code true} if the two specified arrays of ints are
2640      * <i>equal</i> to one another.  Two arrays are considered equal if both
2641      * arrays contain the same number of elements, and all corresponding pairs
2642      * of elements in the two arrays are equal.  In other words, two arrays
2643      * are equal if they contain the same elements in the same order.  Also,
2644      * two array references are considered equal if both are {@code null}.
2645      *
2646      * @param a one array to be tested for equality
2647      * @param a2 the other array to be tested for equality
2648      * @return {@code true} if the two arrays are equal
2649      */
2650     public static boolean equals(int[] a, int[] a2) {
2651         if (a==a2)
2652             return true;
2653         if (a==null || a2==null)
2654             return false;
2655 
2656         int length = a.length;
2657         if (a2.length != length)
2658             return false;
2659 
2660         for (int i=0; i<length; i++)
2661             if (a[i] != a2[i])
2662                 return false;
2663 
2664         return true;
2665     }
2666 
2667     /**
2668      * Returns true if the two specified arrays of ints, over the specified
2669      * ranges, are <i>equal</i> to one another.
2670      *
2671      * <p>Two arrays are considered equal if the number of elements covered by
2672      * each range is the same, and all corresponding pairs of elements over the
2673      * specified ranges in the two arrays are equal.  In other words, two arrays
2674      * are equal if they contain, over the specified ranges, the same elements
2675      * in the same order.
2676      *
2677      * @param a the first array to be tested for equality
2678      * @param aFromIndex the index (inclusive) of the first element in the
2679      *                   first array to be tested
2680      * @param aToIndex the index (exclusive) of the last element in the
2681      *                 first array to be tested
2682      * @param b the second array to be tested fro equality
2683      * @param bFromIndex the index (inclusive) of the first element in the
2684      *                   second array to be tested
2685      * @param bToIndex the index (exclusive) of the last element in the
2686      *                 second array to be tested
2687      * @return {@code true} if the two arrays, over the specified ranges, are
2688      *         equal
2689      * @throws IllegalArgumentException
2690      *         if {@code aFromIndex > aToIndex} or
2691      *         if {@code bFromIndex > bToIndex}
2692      * @throws ArrayIndexOutOfBoundsException
2693      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
2694      *         if {@code bFromIndex < 0 or bToIndex > b.length}
2695      * @throws NullPointerException
2696      *         if either array is {@code null}
2697      * @since 9
2698      */
2699     public static boolean equals(int[] a, int aFromIndex, int aToIndex,
2700                                  int[] b, int bFromIndex, int bToIndex) {
2701         rangeCheck(a.length, aFromIndex, aToIndex);
2702         rangeCheck(b.length, bFromIndex, bToIndex);
2703 
2704         int aLength = aToIndex - aFromIndex;
2705         int bLength = bToIndex - bFromIndex;
2706         if (aLength != bLength)
2707             return false;
2708 
2709         for (int i = 0; i < aLength; i++)
2710             if (a[aFromIndex++] != b[bFromIndex++])
2711                 return false;
2712 
2713         return true;
2714     }
2715 
2716     /**
2717      * Returns {@code true} if the two specified arrays of shorts are
2718      * <i>equal</i> to one another.  Two arrays are considered equal if both
2719      * arrays contain the same number of elements, and all corresponding pairs
2720      * of elements in the two arrays are equal.  In other words, two arrays
2721      * are equal if they contain the same elements in the same order.  Also,
2722      * two array references are considered equal if both are {@code null}.
2723      *
2724      * @param a one array to be tested for equality
2725      * @param a2 the other array to be tested for equality
2726      * @return {@code true} if the two arrays are equal
2727      */
2728     public static boolean equals(short[] a, short a2[]) {
2729         if (a==a2)
2730             return true;
2731         if (a==null || a2==null)
2732             return false;
2733 
2734         int length = a.length;
2735         if (a2.length != length)
2736             return false;
2737 
2738         for (int i=0; i<length; i++)
2739             if (a[i] != a2[i])
2740                 return false;
2741 
2742         return true;
2743     }
2744 
2745     /**
2746      * Returns true if the two specified arrays of shorts, over the specified
2747      * ranges, are <i>equal</i> to one another.
2748      *
2749      * <p>Two arrays are considered equal if the number of elements covered by
2750      * each range is the same, and all corresponding pairs of elements over the
2751      * specified ranges in the two arrays are equal.  In other words, two arrays
2752      * are equal if they contain, over the specified ranges, the same elements
2753      * in the same order.
2754      *
2755      * @param a the first array to be tested for equality
2756      * @param aFromIndex the index (inclusive) of the first element in the
2757      *                   first array to be tested
2758      * @param aToIndex the index (exclusive) of the last element in the
2759      *                 first array to be tested
2760      * @param b the second array to be tested fro equality
2761      * @param bFromIndex the index (inclusive) of the first element in the
2762      *                   second array to be tested
2763      * @param bToIndex the index (exclusive) of the last element in the
2764      *                 second array to be tested
2765      * @return {@code true} if the two arrays, over the specified ranges, are
2766      *         equal
2767      * @throws IllegalArgumentException
2768      *         if {@code aFromIndex > aToIndex} or
2769      *         if {@code bFromIndex > bToIndex}
2770      * @throws ArrayIndexOutOfBoundsException
2771      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
2772      *         if {@code bFromIndex < 0 or bToIndex > b.length}
2773      * @throws NullPointerException
2774      *         if either array is {@code null}
2775      * @since 9
2776      */
2777     public static boolean equals(short[] a, int aFromIndex, int aToIndex,
2778                                  short[] b, int bFromIndex, int bToIndex) {
2779         rangeCheck(a.length, aFromIndex, aToIndex);
2780         rangeCheck(b.length, bFromIndex, bToIndex);
2781 
2782         int aLength = aToIndex - aFromIndex;
2783         int bLength = bToIndex - bFromIndex;
2784         if (aLength != bLength)
2785             return false;
2786 
2787         for (int i = 0; i < aLength; i++)
2788             if (a[aFromIndex++] != b[bFromIndex++])
2789                 return false;
2790 
2791         return true;
2792     }
2793 
2794     /**
2795      * Returns {@code true} if the two specified arrays of chars are
2796      * <i>equal</i> to one another.  Two arrays are considered equal if both
2797      * arrays contain the same number of elements, and all corresponding pairs
2798      * of elements in the two arrays are equal.  In other words, two arrays
2799      * are equal if they contain the same elements in the same order.  Also,
2800      * two array references are considered equal if both are {@code null}.
2801      *
2802      * @param a one array to be tested for equality
2803      * @param a2 the other array to be tested for equality
2804      * @return {@code true} if the two arrays are equal
2805      */
2806     @HotSpotIntrinsicCandidate
2807     public static boolean equals(char[] a, char[] a2) {
2808         if (a==a2)
2809             return true;
2810         if (a==null || a2==null)
2811             return false;
2812 
2813         int length = a.length;
2814         if (a2.length != length)
2815             return false;
2816 
2817         for (int i=0; i<length; i++)
2818             if (a[i] != a2[i])
2819                 return false;
2820 
2821         return true;
2822     }
2823 
2824     /**
2825      * Returns true if the two specified arrays of chars, over the specified
2826      * ranges, are <i>equal</i> to one another.
2827      *
2828      * <p>Two arrays are considered equal if the number of elements covered by
2829      * each range is the same, and all corresponding pairs of elements over the
2830      * specified ranges in the two arrays are equal.  In other words, two arrays
2831      * are equal if they contain, over the specified ranges, the same elements
2832      * in the same order.
2833      *
2834      * @param a the first array to be tested for equality
2835      * @param aFromIndex the index (inclusive) of the first element in the
2836      *                   first array to be tested
2837      * @param aToIndex the index (exclusive) of the last element in the
2838      *                 first array to be tested
2839      * @param b the second array to be tested fro equality
2840      * @param bFromIndex the index (inclusive) of the first element in the
2841      *                   second array to be tested
2842      * @param bToIndex the index (exclusive) of the last element in the
2843      *                 second array to be tested
2844      * @return {@code true} if the two arrays, over the specified ranges, are
2845      *         equal
2846      * @throws IllegalArgumentException
2847      *         if {@code aFromIndex > aToIndex} or
2848      *         if {@code bFromIndex > bToIndex}
2849      * @throws ArrayIndexOutOfBoundsException
2850      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
2851      *         if {@code bFromIndex < 0 or bToIndex > b.length}
2852      * @throws NullPointerException
2853      *         if either array is {@code null}
2854      * @since 9
2855      */
2856     public static boolean equals(char[] a, int aFromIndex, int aToIndex,
2857                                  char[] b, int bFromIndex, int bToIndex) {
2858         rangeCheck(a.length, aFromIndex, aToIndex);
2859         rangeCheck(b.length, bFromIndex, bToIndex);
2860 
2861         int aLength = aToIndex - aFromIndex;
2862         int bLength = bToIndex - bFromIndex;
2863         if (aLength != bLength)
2864             return false;
2865 
2866         for (int i = 0; i < aLength; i++)
2867             if (a[aFromIndex++] != b[bFromIndex++])
2868                 return false;
2869 
2870         return true;
2871     }
2872 
2873     /**
2874      * Returns {@code true} if the two specified arrays of bytes are
2875      * <i>equal</i> to one another.  Two arrays are considered equal if both
2876      * arrays contain the same number of elements, and all corresponding pairs
2877      * of elements in the two arrays are equal.  In other words, two arrays
2878      * are equal if they contain the same elements in the same order.  Also,
2879      * two array references are considered equal if both are {@code null}.
2880      *
2881      * @param a one array to be tested for equality
2882      * @param a2 the other array to be tested for equality
2883      * @return {@code true} if the two arrays are equal
2884      */
2885     public static boolean equals(byte[] a, byte[] a2) {
2886         if (a==a2)
2887             return true;
2888         if (a==null || a2==null)
2889             return false;
2890 
2891         int length = a.length;
2892         if (a2.length != length)
2893             return false;
2894 
2895         for (int i=0; i<length; i++)
2896             if (a[i] != a2[i])
2897                 return false;
2898 
2899         return true;
2900     }
2901 
2902     /**
2903      * Returns true if the two specified arrays of bytes, over the specified
2904      * ranges, are <i>equal</i> to one another.
2905      *
2906      * <p>Two arrays are considered equal if the number of elements covered by
2907      * each range is the same, and all corresponding pairs of elements over the
2908      * specified ranges in the two arrays are equal.  In other words, two arrays
2909      * are equal if they contain, over the specified ranges, the same elements
2910      * in the same order.
2911      *
2912      * @param a the first array to be tested for equality
2913      * @param aFromIndex the index (inclusive) of the first element in the
2914      *                   first array to be tested
2915      * @param aToIndex the index (exclusive) of the last element in the
2916      *                 first array to be tested
2917      * @param b the second array to be tested fro equality
2918      * @param bFromIndex the index (inclusive) of the first element in the
2919      *                   second array to be tested
2920      * @param bToIndex the index (exclusive) of the last element in the
2921      *                 second array to be tested
2922      * @return {@code true} if the two arrays, over the specified ranges, are
2923      *         equal
2924      * @throws IllegalArgumentException
2925      *         if {@code aFromIndex > aToIndex} or
2926      *         if {@code bFromIndex > bToIndex}
2927      * @throws ArrayIndexOutOfBoundsException
2928      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
2929      *         if {@code bFromIndex < 0 or bToIndex > b.length}
2930      * @throws NullPointerException
2931      *         if either array is {@code null}
2932      * @since 9
2933      */
2934     public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
2935                                  byte[] b, int bFromIndex, int bToIndex) {
2936         rangeCheck(a.length, aFromIndex, aToIndex);
2937         rangeCheck(b.length, bFromIndex, bToIndex);
2938 
2939         int aLength = aToIndex - aFromIndex;
2940         int bLength = bToIndex - bFromIndex;
2941         if (aLength != bLength)
2942             return false;
2943 
2944         for (int i = 0; i < aLength; i++)
2945             if (a[aFromIndex++] != b[bFromIndex++])
2946                 return false;
2947 
2948         return true;
2949     }
2950 
2951     /**
2952      * Returns {@code true} if the two specified arrays of booleans are
2953      * <i>equal</i> to one another.  Two arrays are considered equal if both
2954      * arrays contain the same number of elements, and all corresponding pairs
2955      * of elements in the two arrays are equal.  In other words, two arrays
2956      * are equal if they contain the same elements in the same order.  Also,
2957      * two array references are considered equal if both are {@code null}.
2958      *
2959      * @param a one array to be tested for equality
2960      * @param a2 the other array to be tested for equality
2961      * @return {@code true} if the two arrays are equal
2962      */
2963     public static boolean equals(boolean[] a, boolean[] a2) {
2964         if (a==a2)
2965             return true;
2966         if (a==null || a2==null)
2967             return false;
2968 
2969         int length = a.length;
2970         if (a2.length != length)
2971             return false;
2972 
2973         for (int i=0; i<length; i++)
2974             if (a[i] != a2[i])
2975                 return false;
2976 
2977         return true;
2978     }
2979 
2980     /**
2981      * Returns true if the two specified arrays of booleans, over the specified
2982      * ranges, are <i>equal</i> to one another.
2983      *
2984      * <p>Two arrays are considered equal if the number of elements covered by
2985      * each range is the same, and all corresponding pairs of elements over the
2986      * specified ranges in the two arrays are equal.  In other words, two arrays
2987      * are equal if they contain, over the specified ranges, the same elements
2988      * in the same order.
2989      *
2990      * @param a the first array to be tested for equality
2991      * @param aFromIndex the index (inclusive) of the first element in the
2992      *                   first array to be tested
2993      * @param aToIndex the index (exclusive) of the last element in the
2994      *                 first array to be tested
2995      * @param b the second array to be tested fro equality
2996      * @param bFromIndex the index (inclusive) of the first element in the
2997      *                   second array to be tested
2998      * @param bToIndex the index (exclusive) of the last element in the
2999      *                 second array to be tested
3000      * @return {@code true} if the two arrays, over the specified ranges, are
3001      *         equal
3002      * @throws IllegalArgumentException
3003      *         if {@code aFromIndex > aToIndex} or
3004      *         if {@code bFromIndex > bToIndex}
3005      * @throws ArrayIndexOutOfBoundsException
3006      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
3007      *         if {@code bFromIndex < 0 or bToIndex > b.length}
3008      * @throws NullPointerException
3009      *         if either array is {@code null}
3010      * @since 9
3011      */
3012     public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
3013                                  boolean[] b, int bFromIndex, int bToIndex) {
3014         rangeCheck(a.length, aFromIndex, aToIndex);
3015         rangeCheck(b.length, bFromIndex, bToIndex);
3016 
3017         int aLength = aToIndex - aFromIndex;
3018         int bLength = bToIndex - bFromIndex;
3019         if (aLength != bLength)
3020             return false;
3021 
3022         for (int i = 0; i < aLength; i++)
3023             if (a[aFromIndex++] != b[bFromIndex++])
3024                 return false;
3025 
3026         return true;
3027     }
3028 
3029     /**
3030      * Returns {@code true} if the two specified arrays of doubles are
3031      * <i>equal</i> to one another.  Two arrays are considered equal if both
3032      * arrays contain the same number of elements, and all corresponding pairs
3033      * of elements in the two arrays are equal.  In other words, two arrays
3034      * are equal if they contain the same elements in the same order.  Also,
3035      * two array references are considered equal if both are {@code null}.
3036      *
3037      * Two doubles {@code d1} and {@code d2} are considered equal if:
3038      * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
3039      * (Unlike the {@code ==} operator, this method considers
3040      * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
3041      *
3042      * @param a one array to be tested for equality
3043      * @param a2 the other array to be tested for equality
3044      * @return {@code true} if the two arrays are equal
3045      * @see Double#equals(Object)
3046      */
3047     public static boolean equals(double[] a, double[] a2) {
3048         if (a==a2)
3049             return true;
3050         if (a==null || a2==null)
3051             return false;
3052 
3053         int length = a.length;
3054         if (a2.length != length)
3055             return false;
3056 
3057         for (int i=0; i<length; i++) {
3058             double v1 = a[i], v2 = a2[i];
3059             if (Double.doubleToRawLongBits(v1) != Double.doubleToRawLongBits(v2))
3060                 if (!Double.isNaN(v1) || !Double.isNaN(v2))
3061                     return false;
3062         }
3063 
3064         return true;
3065     }
3066 
3067     /**
3068      * Returns true if the two specified arrays of doubles, over the specified
3069      * ranges, are <i>equal</i> to one another.
3070      *
3071      * <p>Two arrays are considered equal if the number of elements covered by
3072      * each range is the same, and all corresponding pairs of elements over the
3073      * specified ranges in the two arrays are equal.  In other words, two arrays
3074      * are equal if they contain, over the specified ranges, the same elements
3075      * in the same order.
3076      *
3077      * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
3078      * <pre>    {@code new Double(d1).equals(new Double(d2))}</pre>
3079      * (Unlike the {@code ==} operator, this method considers
3080      * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
3081      *
3082      * @param a the first array to be tested for equality
3083      * @param aFromIndex the index (inclusive) of the first element in the
3084      *                   first array to be tested
3085      * @param aToIndex the index (exclusive) of the last element in the
3086      *                 first array to be tested
3087      * @param b the second array to be tested fro equality
3088      * @param bFromIndex the index (inclusive) of the first element in the
3089      *                   second array to be tested
3090      * @param bToIndex the index (exclusive) of the last element in the
3091      *                 second array to be tested
3092      * @return {@code true} if the two arrays, over the specified ranges, are
3093      *         equal
3094      * @throws IllegalArgumentException
3095      *         if {@code aFromIndex > aToIndex} or
3096      *         if {@code bFromIndex > bToIndex}
3097      * @throws ArrayIndexOutOfBoundsException
3098      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
3099      *         if {@code bFromIndex < 0 or bToIndex > b.length}
3100      * @throws NullPointerException
3101      *         if either array is {@code null}
3102      * @see Double#equals(Object)
3103      * @since 9
3104      */
3105     public static boolean equals(double[] a, int aFromIndex, int aToIndex,
3106                                  double[] b, int bFromIndex, int bToIndex) {
3107         rangeCheck(a.length, aFromIndex, aToIndex);
3108         rangeCheck(b.length, bFromIndex, bToIndex);
3109 
3110         int aLength = aToIndex - aFromIndex;
3111         int bLength = bToIndex - bFromIndex;
3112         if (aLength != bLength)
3113             return false;
3114 
3115         for (int i = 0; i < aLength; i++) {
3116             Double va = a[aFromIndex++], vb = b[bFromIndex++];
3117             if (Double.doubleToRawLongBits(va) != Double.doubleToRawLongBits(vb))
3118                 if (!Double.isNaN(va) || !Double.isNaN(vb))
3119                     return false;
3120         }
3121 
3122         return true;
3123     }
3124 
3125     /**
3126      * Returns {@code true} if the two specified arrays of floats are
3127      * <i>equal</i> to one another.  Two arrays are considered equal if both
3128      * arrays contain the same number of elements, and all corresponding pairs
3129      * of elements in the two arrays are equal.  In other words, two arrays
3130      * are equal if they contain the same elements in the same order.  Also,
3131      * two array references are considered equal if both are {@code null}.
3132      *
3133      * Two floats {@code f1} and {@code f2} are considered equal if:
3134      * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
3135      * (Unlike the {@code ==} operator, this method considers
3136      * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
3137      *
3138      * @param a one array to be tested for equality
3139      * @param a2 the other array to be tested for equality
3140      * @return {@code true} if the two arrays are equal
3141      * @see Float#equals(Object)
3142      */
3143     public static boolean equals(float[] a, float[] a2) {
3144         if (a==a2)
3145             return true;
3146         if (a==null || a2==null)
3147             return false;
3148 
3149         int length = a.length;
3150         if (a2.length != length)
3151             return false;
3152 
3153         for (int i=0; i<length; i++) {
3154             float v1 = a[i], v2 = a2[i];
3155             if (Float.floatToRawIntBits(v1) != Float.floatToRawIntBits(v2))
3156                 if (!Float.isNaN(v1) || !Float.isNaN(v2))
3157                     return false;
3158         }
3159 
3160         return true;
3161     }
3162 
3163     /**
3164      * Returns true if the two specified arrays of floats, over the specified
3165      * ranges, are <i>equal</i> to one another.
3166      *
3167      * <p>Two arrays are considered equal if the number of elements covered by
3168      * each range is the same, and all corresponding pairs of elements over the
3169      * specified ranges in the two arrays are equal.  In other words, two arrays
3170      * are equal if they contain, over the specified ranges, the same elements
3171      * in the same order.
3172      *
3173      * <p>Two floats {@code f1} and {@code f2} are considered equal if:
3174      * <pre>    {@code new Float(f1).equals(new Float(f2))}</pre>
3175      * (Unlike the {@code ==} operator, this method considers
3176      * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
3177      *
3178      * @param a the first array to be tested for equality
3179      * @param aFromIndex the index (inclusive) of the first element in the
3180      *                   first array to be tested
3181      * @param aToIndex the index (exclusive) of the last element in the
3182      *                 first array to be tested
3183      * @param b the second array to be tested fro equality
3184      * @param bFromIndex the index (inclusive) of the first element in the
3185      *                   second array to be tested
3186      * @param bToIndex the index (exclusive) of the last element in the
3187      *                 second array to be tested
3188      * @return {@code true} if the two arrays, over the specified ranges, are
3189      *         equal
3190      * @throws IllegalArgumentException
3191      *         if {@code aFromIndex > aToIndex} or
3192      *         if {@code bFromIndex > bToIndex}
3193      * @throws ArrayIndexOutOfBoundsException
3194      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
3195      *         if {@code bFromIndex < 0 or bToIndex > b.length}
3196      * @throws NullPointerException
3197      *         if either array is {@code null}
3198      * @see Float#equals(Object)
3199      * @since 9
3200      */
3201     public static boolean equals(float[] a, int aFromIndex, int aToIndex,
3202                                  float[] b, int bFromIndex, int bToIndex) {
3203         rangeCheck(a.length, aFromIndex, aToIndex);
3204         rangeCheck(b.length, bFromIndex, bToIndex);
3205 
3206         int aLength = aToIndex - aFromIndex;
3207         int bLength = bToIndex - bFromIndex;
3208         if (aLength != bLength)
3209             return false;
3210 
3211         for (int i = 0; i < aLength; i++) {
3212             float va = a[aFromIndex++], vb = b[bFromIndex++];
3213             if (Float.floatToRawIntBits(va) != Float.floatToRawIntBits(vb))
3214                 if (!Float.isNaN(va) || !Float.isNaN(vb))
3215                     return false;
3216         }
3217 
3218         return true;
3219     }
3220 
3221     /**
3222      * Returns {@code true} if the two specified arrays of Objects are
3223      * <i>equal</i> to one another.  The two arrays are considered equal if
3224      * both arrays contain the same number of elements, and all corresponding
3225      * pairs of elements in the two arrays are equal.  Two objects {@code e1}
3226      * and {@code e2} are considered <i>equal</i> if
3227      * {@code Objects.equals(e1, e2)}.
3228      * In other words, the two arrays are equal if
3229      * they contain the same elements in the same order.  Also, two array
3230      * references are considered equal if both are {@code null}.
3231      *
3232      * @param a one array to be tested for equality
3233      * @param a2 the other array to be tested for equality
3234      * @return {@code true} if the two arrays are equal
3235      */
3236     public static boolean equals(Object[] a, Object[] a2) {
3237         if (a==a2)
3238             return true;
3239         if (a==null || a2==null)
3240             return false;
3241 
3242         int length = a.length;
3243         if (a2.length != length)
3244             return false;
3245 
3246         for (int i=0; i<length; i++) {
3247             if (!Objects.equals(a[i], a2[i]))
3248                 return false;
3249         }
3250 
3251         return true;
3252     }
3253 
3254     /**
3255      * Returns true if the two specified arrays of Objects, over the specified
3256      * ranges, are <i>equal</i> to one another.
3257      *
3258      * <p>Two arrays are considered equal if the number of elements covered by
3259      * each range is the same, and all corresponding pairs of elements over the
3260      * specified ranges in the two arrays are equal.  In other words, two arrays
3261      * are equal if they contain, over the specified ranges, the same elements
3262      * in the same order.
3263      *
3264      * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if
3265      * {@code Objects.equals(e1, e2)}.
3266      *
3267      * @param a the first array to be tested for equality
3268      * @param aFromIndex the index (inclusive) of the first element in the
3269      *                   first array to be tested
3270      * @param aToIndex the index (exclusive) of the last element in the
3271      *                 first array to be tested
3272      * @param b the second array to be tested fro equality
3273      * @param bFromIndex the index (inclusive) of the first element in the
3274      *                   second array to be tested
3275      * @param bToIndex the index (exclusive) of the last element in the
3276      *                 second array to be tested
3277      * @return {@code true} if the two arrays, over the specified ranges, are
3278      *         equal
3279      * @throws IllegalArgumentException
3280      *         if {@code aFromIndex > aToIndex} or
3281      *         if {@code bFromIndex > bToIndex}
3282      * @throws ArrayIndexOutOfBoundsException
3283      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
3284      *         if {@code bFromIndex < 0 or bToIndex > b.length}
3285      * @throws NullPointerException
3286      *         if either array is {@code null}
3287      * @since 9
3288      */
3289     public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
3290                                  Object[] b, int bFromIndex, int bToIndex) {
3291         rangeCheck(a.length, aFromIndex, aToIndex);
3292         rangeCheck(b.length, bFromIndex, bToIndex);
3293 
3294         int aLength = aToIndex - aFromIndex;
3295         int bLength = bToIndex - bFromIndex;
3296         if (aLength != bLength)
3297             return false;
3298 
3299         for (int i = 0; i < aLength; i++) {
3300             if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
3301                 return false;
3302         }
3303 
3304         return true;
3305     }
3306 
3307     // Filling
3308 
3309     /**
3310      * Assigns the specified long value to each element of the specified array
3311      * of longs.
3312      *
3313      * @param a the array to be filled
3314      * @param val the value to be stored in all elements of the array
3315      */
3316     public static void fill(long[] a, long val) {
3317         for (int i = 0, len = a.length; i < len; i++)
3318             a[i] = val;
3319     }
3320 


5636         return stream(array, 0, array.length);
5637     }
5638 
5639     /**
5640      * Returns a sequential {@link DoubleStream} with the specified range of the
5641      * specified array as its source.
5642      *
5643      * @param array the array, assumed to be unmodified during use
5644      * @param startInclusive the first index to cover, inclusive
5645      * @param endExclusive index immediately past the last index to cover
5646      * @return a {@code DoubleStream} for the array range
5647      * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5648      *         negative, {@code endExclusive} is less than
5649      *         {@code startInclusive}, or {@code endExclusive} is greater than
5650      *         the array size
5651      * @since 1.8
5652      */
5653     public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
5654         return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
5655     }
5656 
5657 
5658     // Comparison methods
5659 
5660     // Compare boolean
5661 
5662     /**
5663      * Compares two {@code boolean} arrays lexicographically.
5664      *
5665      * <p>If the two arrays share a common prefix then the lexicographic
5666      * comparison is the result of comparing two elements, as if by
5667      * {@link Boolean#compare(boolean, boolean)}, at an index within the
5668      * respective arrays that is the prefix length.
5669      * Otherwise, one array is a proper prefix of the other and, lexicographic
5670      * comparison is the result of comparing the two array lengths.
5671      * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
5672      * common and proper prefix.)
5673      *
5674      * <p>A {@code null} array reference is considered lexicographically less
5675      * than a non-{@code null} array reference.  Two {@code null} array
5676      * references are considered equal.
5677      *
5678      * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
5679      * more specifically the following holds for arrays {@code a} and {@code b}:
5680      * <pre>{@code
5681      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
5682      * }</pre>
5683      *
5684      * @apiNote
5685      * <p>This method behaves as if (for non-{@code null} array references):
5686      * <pre>{@code
5687      *     int i = Arrays.mismatch(a, b);
5688      *     if (i >= 0 && i < Math.min(a.length, b.length))
5689      *         return Boolean.compare(a[i], b[i]);
5690      *     return a.length - b.length;
5691      * }</pre>
5692      *
5693      * @param a the first array to compare
5694      * @param b the second array to compare
5695      * @return the value {@code 0} if the first and second array are equal and
5696      *         contain the same elements in the same order;
5697      *         a value less than {@code 0} if the first array is
5698      *         lexicographically less than the second array; and
5699      *         a value greater than {@code 0} if the first array is
5700      *         lexicographically greater than the second array
5701      * @since 9
5702      */
5703     public static int compare(boolean[] a, boolean[] b) {
5704         if (a == b)
5705             return 0;
5706         if (a == null || b == null)
5707             return a == null ? -1 : 1;
5708 
5709         int length = Math.min(a.length, b.length);
5710         for (int i = 0; i < length; i++) {
5711             if (a[i] != b[i]) return Boolean.compare(a[i], b[i]);
5712         }
5713 
5714         return a.length - b.length;
5715     }
5716 
5717     /**
5718      * Compares two {@code boolean} arrays lexicographically over the specified
5719      * ranges.
5720      *
5721      * <p>If the two arrays, over the specified ranges, share a common prefix
5722      * then the lexicographic comparison is the result of comparing two
5723      * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
5724      * relative index within the respective arrays that is the length of the
5725      * prefix.
5726      * Otherwise, one array is a proper prefix of the other and, lexicographic
5727      * comparison is the result of comparing the two range lengths.
5728      * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
5729      * definition of a common and proper prefix.)
5730      *
5731      * <p>The comparison is consistent with
5732      * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
5733      * specifically the following holds for arrays {@code a} and {@code b} with
5734      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
5735      * [{@code bFromIndex}, {@code btoIndex}) respectively:
5736      * <pre>{@code
5737      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
5738      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
5739      * }</pre>
5740      *
5741      * @apiNote
5742      * <p>This method behaves as if:
5743      * <pre>{@code
5744      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
5745      *                             b, bFromIndex, bToIndex);
5746      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
5747      *         return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
5748      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
5749      * }</pre>
5750      *
5751      * @param a the first array to compare
5752      * @param aFromIndex the index (inclusive) of the first element in the
5753      *                   first array to be compared
5754      * @param aToIndex the index (exclusive) of the last element in the
5755      *                 first array to be compared
5756      * @param b the second array to compare
5757      * @param bFromIndex the index (inclusive) of the first element in the
5758      *                   second array to be compared
5759      * @param bToIndex the index (exclusive) of the last element in the
5760      *                 second array to be compared
5761      * @return the value {@code 0} if, over the specified ranges, the first and
5762      *         second array are equal and contain the same elements in the same
5763      *         order;
5764      *         a value less than {@code 0} if, over the specified ranges, the
5765      *         first array is lexicographically less than the second array; and
5766      *         a value greater than {@code 0} if, over the specified ranges, the
5767      *         first array is lexicographically greater than the second array
5768      * @throws IllegalArgumentException
5769      *         if {@code aFromIndex > aToIndex} or
5770      *         if {@code bFromIndex > bToIndex}
5771      * @throws ArrayIndexOutOfBoundsException
5772      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
5773      *         if {@code bFromIndex < 0 or bToIndex > b.length}
5774      * @throws NullPointerException
5775      *         if either array is {@code null}
5776      * @since 9
5777      */
5778     public static int compare(boolean[] a, int aFromIndex, int aToIndex,
5779                               boolean[] b, int bFromIndex, int bToIndex) {
5780         rangeCheck(a.length, aFromIndex, aToIndex);
5781         rangeCheck(b.length, bFromIndex, bToIndex);
5782 
5783         int aLength = aToIndex - aFromIndex;
5784         int bLength = bToIndex - bFromIndex;
5785         int length = Math.min(aLength, bLength);
5786         for (int i = 0; i < length; i++) {
5787             boolean va = a[aFromIndex++];
5788             boolean vb = b[bFromIndex++];
5789             if (va != vb) return Boolean.compare(va, vb);
5790         }
5791 
5792         return aLength - bLength;
5793     }
5794 
5795     // Compare byte
5796 
5797     /**
5798      * Compares two {@code byte} arrays lexicographically.
5799      *
5800      * <p>If the two arrays share a common prefix then the lexicographic
5801      * comparison is the result of comparing two elements, as if by
5802      * {@link Byte#compare(byte, byte)}, at an index within the respective
5803      * arrays that is the prefix length.
5804      * Otherwise, one array is a proper prefix of the other and, lexicographic
5805      * comparison is the result of comparing the two array lengths.
5806      * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
5807      * proper prefix.)
5808      *
5809      * <p>A {@code null} array reference is considered lexicographically less
5810      * than a non-{@code null} array reference.  Two {@code null} array
5811      * references are considered equal.
5812      *
5813      * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
5814      * more specifically the following holds for arrays {@code a} and {@code b}:
5815      * <pre>{@code
5816      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
5817      * }</pre>
5818      *
5819      * @apiNote
5820      * <p>This method behaves as if (for non-{@code null} array references):
5821      * <pre>{@code
5822      *     int i = Arrays.mismatch(a, b);
5823      *     if (i >= 0 && i < Math.min(a.length, b.length))
5824      *         return Byte.compare(a[i], b[i]);
5825      *     return a.length - b.length;
5826      * }</pre>
5827      *
5828      * @param a the first array to compare
5829      * @param b the second array to compare
5830      * @return the value {@code 0} if the first and second array are equal and
5831      *         contain the same elements in the same order;
5832      *         a value less than {@code 0} if the first array is
5833      *         lexicographically less than the second array; and
5834      *         a value greater than {@code 0} if the first array is
5835      *         lexicographically greater than the second array
5836      * @since 9
5837      */
5838     public static int compare(byte[] a, byte[] b) {
5839         if (a == b)
5840             return 0;
5841         if (a == null || b == null)
5842             return a == null ? -1 : 1;
5843 
5844         int length = Math.min(a.length, b.length);
5845         for (int i = 0; i < length; i++) {
5846             if (a[i] != b[i]) return Byte.compare(a[i], b[i]);
5847         }
5848 
5849         return a.length - b.length;
5850     }
5851 
5852     /**
5853      * Compares two {@code byte} arrays lexicographically over the specified
5854      * ranges.
5855      *
5856      * <p>If the two arrays, over the specified ranges, share a common prefix
5857      * then the lexicographic comparison is the result of comparing two
5858      * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
5859      * within the respective arrays that is the length of the prefix.
5860      * Otherwise, one array is a proper prefix of the other and, lexicographic
5861      * comparison is the result of comparing the two range lengths.
5862      * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
5863      * definition of a common and proper prefix.)
5864      *
5865      * <p>The comparison is consistent with
5866      * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
5867      * specifically the following holds for arrays {@code a} and {@code b} with
5868      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
5869      * [{@code bFromIndex}, {@code btoIndex}) respectively:
5870      * <pre>{@code
5871      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
5872      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
5873      * }</pre>
5874      *
5875      * @apiNote
5876      * <p>This method behaves as if:
5877      * <pre>{@code
5878      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
5879      *                             b, bFromIndex, bToIndex);
5880      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
5881      *         return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
5882      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
5883      * }</pre>
5884      *
5885      * @param a the first array to compare
5886      * @param aFromIndex the index (inclusive) of the first element in the
5887      *                   first array to be compared
5888      * @param aToIndex the index (exclusive) of the last element in the
5889      *                 first array to be compared
5890      * @param b the second array to compare
5891      * @param bFromIndex the index (inclusive) of the first element in the
5892      *                   second array to be compared
5893      * @param bToIndex the index (exclusive) of the last element in the
5894      *                 second array to be compared
5895      * @return the value {@code 0} if, over the specified ranges, the first and
5896      *         second array are equal and contain the same elements in the same
5897      *         order;
5898      *         a value less than {@code 0} if, over the specified ranges, the
5899      *         first array is lexicographically less than the second array; and
5900      *         a value greater than {@code 0} if, over the specified ranges, the
5901      *         first array is lexicographically greater than the second array
5902      * @throws IllegalArgumentException
5903      *         if {@code aFromIndex > aToIndex} or
5904      *         if {@code bFromIndex > bToIndex}
5905      * @throws ArrayIndexOutOfBoundsException
5906      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
5907      *         if {@code bFromIndex < 0 or bToIndex > b.length}
5908      * @throws NullPointerException
5909      *         if either array is {@code null}
5910      * @since 9
5911      */
5912     public static int compare(byte[] a, int aFromIndex, int aToIndex,
5913                               byte[] b, int bFromIndex, int bToIndex) {
5914         rangeCheck(a.length, aFromIndex, aToIndex);
5915         rangeCheck(b.length, bFromIndex, bToIndex);
5916 
5917         int aLength = aToIndex - aFromIndex;
5918         int bLength = bToIndex - bFromIndex;
5919         int length = Math.min(aLength, bLength);
5920         for (int i = 0; i < length; i++) {
5921             byte va = a[aFromIndex++];
5922             byte vb = b[bFromIndex++];
5923             if (va != vb) return Byte.compare(va, vb);
5924         }
5925 
5926         return aLength - bLength;
5927     }
5928 
5929     /**
5930      * Compares two {@code byte} arrays lexicographically, numerically treating
5931      * elements as unsigned.
5932      *
5933      * <p>If the two arrays share a common prefix then the lexicographic
5934      * comparison is the result of comparing two elements, as if by
5935      * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
5936      * respective arrays that is the prefix length.
5937      * Otherwise, one array is a proper prefix of the other and, lexicographic
5938      * comparison is the result of comparing the two array lengths.
5939      * (See {@link #mismatch(byte[], byte[])} for the definition of a common
5940      * and proper prefix.)
5941      *
5942      * <p>A {@code null} array reference is considered lexicographically less
5943      * than a non-{@code null} array reference.  Two {@code null} array
5944      * references are considered equal.
5945      *
5946      * @apiNote
5947      * <p>This method behaves as if (for non-{@code null} array references):
5948      * <pre>{@code
5949      *     int i = Arrays.mismatch(a, b);
5950      *     if (i >= 0 && i < Math.min(a.length, b.length))
5951      *         return Byte.compareUnsigned(a[i], b[i]);
5952      *     return a.length - b.length;
5953      * }</pre>
5954      *
5955      * @param a the first array to compare
5956      * @param b the second array to compare
5957      * @return the value {@code 0} if the first and second array are
5958      *         equal and contain the same elements in the same order;
5959      *         a value less than {@code 0} if the first array is
5960      *         lexicographically less than the second array; and
5961      *         a value greater than {@code 0} if the first array is
5962      *         lexicographically greater than the second array
5963      * @since 9
5964      */
5965     public static int compareUnsigned(byte[] a, byte[] b) {
5966         if (a == b)
5967             return 0;
5968         if (a == null || b == null)
5969             return a == null ? -1 : 1;
5970 
5971         int length = Math.min(a.length, b.length);
5972         for (int i = 0; i < length; i++) {
5973             if (a[i] != b[i]) return Byte.compareUnsigned(a[i], b[i]);
5974         }
5975 
5976         return a.length - b.length;
5977     }
5978 
5979 
5980     /**
5981      * Compares two {@code byte} arrays lexicographically over the specified
5982      * ranges, numerically treating elements as unsigned.
5983      *
5984      * <p>If the two arrays, over the specified ranges, share a common prefix
5985      * then the lexicographic comparison is the result of comparing two
5986      * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
5987      * relative index within the respective arrays that is the length of the
5988      * prefix.
5989      * Otherwise, one array is a proper prefix of the other and, lexicographic
5990      * comparison is the result of comparing the two range lengths.
5991      * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
5992      * definition of a common and proper prefix.)
5993      *
5994      * @apiNote
5995      * <p>This method behaves as if:
5996      * <pre>{@code
5997      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
5998      *                             b, bFromIndex, bToIndex);
5999      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6000      *         return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6001      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6002      * }</pre>
6003      *
6004      * @param a the first array to compare
6005      * @param aFromIndex the index (inclusive) of the first element in the
6006      *                   first array to be compared
6007      * @param aToIndex the index (exclusive) of the last element in the
6008      *                 first array to be compared
6009      * @param b the second array to compare
6010      * @param bFromIndex the index (inclusive) of the first element in the
6011      *                   second array to be compared
6012      * @param bToIndex the index (exclusive) of the last element in the
6013      *                 second array to be compared
6014      * @return the value {@code 0} if, over the specified ranges, the first and
6015      *         second array are equal and contain the same elements in the same
6016      *         order;
6017      *         a value less than {@code 0} if, over the specified ranges, the
6018      *         first array is lexicographically less than the second array; and
6019      *         a value greater than {@code 0} if, over the specified ranges, the
6020      *         first array is lexicographically greater than the second array
6021      * @throws IllegalArgumentException
6022      *         if {@code aFromIndex > aToIndex} or
6023      *         if {@code bFromIndex > bToIndex}
6024      * @throws ArrayIndexOutOfBoundsException
6025      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6026      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6027      * @throws NullPointerException
6028      *         if either array is null
6029      * @since 9
6030      */
6031     public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
6032                                       byte[] b, int bFromIndex, int bToIndex) {
6033         rangeCheck(a.length, aFromIndex, aToIndex);
6034         rangeCheck(b.length, bFromIndex, bToIndex);
6035 
6036         int aLength = aToIndex - aFromIndex;
6037         int bLength = bToIndex - bFromIndex;
6038         int length = Math.min(aLength, bLength);
6039         for (int i = 0; i < length; i++) {
6040             byte va = a[aFromIndex++];
6041             byte vb = b[bFromIndex++];
6042             if (va != vb) return Byte.compareUnsigned(va, vb);
6043         }
6044 
6045         return aLength - bLength;
6046     }
6047 
6048     // Compare short
6049 
6050     /**
6051      * Compares two {@code short} arrays lexicographically.
6052      *
6053      * <p>If the two arrays share a common prefix then the lexicographic
6054      * comparison is the result of comparing two elements, as if by
6055      * {@link Short#compare(short, short)}, at an index within the respective
6056      * arrays that is the prefix length.
6057      * Otherwise, one array is a proper prefix of the other and, lexicographic
6058      * comparison is the result of comparing the two array lengths.
6059      * (See {@link #mismatch(short[], short[])} for the definition of a common
6060      * and proper prefix.)
6061      *
6062      * <p>A {@code null} array reference is considered lexicographically less
6063      * than a non-{@code null} array reference.  Two {@code null} array
6064      * references are considered equal.
6065      *
6066      * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
6067      * more specifically the following holds for arrays {@code a} and {@code b}:
6068      * <pre>{@code
6069      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6070      * }</pre>
6071      *
6072      * @apiNote
6073      * <p>This method behaves as if (for non-{@code null} array references):
6074      * <pre>{@code
6075      *     int i = Arrays.mismatch(a, b);
6076      *     if (i >= 0 && i < Math.min(a.length, b.length))
6077      *         return Short.compare(a[i], b[i]);
6078      *     return a.length - b.length;
6079      * }</pre>
6080      *
6081      * @param a the first array to compare
6082      * @param b the second array to compare
6083      * @return the value {@code 0} if the first and second array are equal and
6084      *         contain the same elements in the same order;
6085      *         a value less than {@code 0} if the first array is
6086      *         lexicographically less than the second array; and
6087      *         a value greater than {@code 0} if the first array is
6088      *         lexicographically greater than the second array
6089      * @since 9
6090      */
6091     public static int compare(short[] a, short[] b) {
6092         if (a == b)
6093             return 0;
6094         if (a == null || b == null)
6095             return a == null ? -1 : 1;
6096 
6097         int length = Math.min(a.length, b.length);
6098         for (int i = 0; i < length; i++) {
6099             if (a[i] != b[i]) return Short.compare(a[i], b[i]);
6100         }
6101 
6102         return a.length - b.length;
6103     }
6104 
6105     /**
6106      * Compares two {@code short} arrays lexicographically over the specified
6107      * ranges.
6108      *
6109      * <p>If the two arrays, over the specified ranges, share a common prefix
6110      * then the lexicographic comparison is the result of comparing two
6111      * elements, as if by {@link Short#compare(short, short)}, at a relative
6112      * index within the respective arrays that is the length of the prefix.
6113      * Otherwise, one array is a proper prefix of the other and, lexicographic
6114      * comparison is the result of comparing the two range lengths.
6115      * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
6116      * definition of a common and proper prefix.)
6117      *
6118      * <p>The comparison is consistent with
6119      * {@link #equals(short[], int, int, short[], int, int) equals}, more
6120      * specifically the following holds for arrays {@code a} and {@code b} with
6121      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6122      * [{@code bFromIndex}, {@code btoIndex}) respectively:
6123      * <pre>{@code
6124      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6125      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6126      * }</pre>
6127      *
6128      * @apiNote
6129      * <p>This method behaves as if:
6130      * <pre>{@code
6131      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6132      *                             b, bFromIndex, bToIndex);
6133      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6134      *         return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
6135      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6136      * }</pre>
6137      *
6138      * @param a the first array to compare
6139      * @param aFromIndex the index (inclusive) of the first element in the
6140      *                   first array to be compared
6141      * @param aToIndex the index (exclusive) of the last element in the
6142      *                 first array to be compared
6143      * @param b the second array to compare
6144      * @param bFromIndex the index (inclusive) of the first element in the
6145      *                   second array to be compared
6146      * @param bToIndex the index (exclusive) of the last element in the
6147      *                 second array to be compared
6148      * @return the value {@code 0} if, over the specified ranges, the first and
6149      *         second array are equal and contain the same elements in the same
6150      *         order;
6151      *         a value less than {@code 0} if, over the specified ranges, the
6152      *         first array is lexicographically less than the second array; and
6153      *         a value greater than {@code 0} if, over the specified ranges, the
6154      *         first array is lexicographically greater than the second array
6155      * @throws IllegalArgumentException
6156      *         if {@code aFromIndex > aToIndex} or
6157      *         if {@code bFromIndex > bToIndex}
6158      * @throws ArrayIndexOutOfBoundsException
6159      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6160      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6161      * @throws NullPointerException
6162      *         if either array is {@code null}
6163      * @since 9
6164      */
6165     public static int compare(short[] a, int aFromIndex, int aToIndex,
6166                               short[] b, int bFromIndex, int bToIndex) {
6167         rangeCheck(a.length, aFromIndex, aToIndex);
6168         rangeCheck(b.length, bFromIndex, bToIndex);
6169 
6170         int aLength = aToIndex - aFromIndex;
6171         int bLength = bToIndex - bFromIndex;
6172         int length = Math.min(aLength, bLength);
6173         for (int i = 0; i < length; i++) {
6174             short va = a[aFromIndex++];
6175             short vb = b[bFromIndex++];
6176             if (va != vb) return Short.compare(va, vb);
6177         }
6178 
6179         return aLength - bLength;
6180     }
6181 
6182     /**
6183      * Compares two {@code short} arrays lexicographically, numerically treating
6184      * elements as unsigned.
6185      *
6186      * <p>If the two arrays share a common prefix then the lexicographic
6187      * comparison is the result of comparing two elements, as if by
6188      * {@link Short#compareUnsigned(short, short)}, at an index within the
6189      * respective arrays that is the prefix length.
6190      * Otherwise, one array is a proper prefix of the other and, lexicographic
6191      * comparison is the result of comparing the two array lengths.
6192      * (See {@link #mismatch(short[], short[])} for the definition of a common
6193      * and proper prefix.)
6194      *
6195      * <p>A {@code null} array reference is considered lexicographically less
6196      * than a non-{@code null} array reference.  Two {@code null} array
6197      * references are considered equal.
6198      *
6199      * @apiNote
6200      * <p>This method behaves as if (for non-{@code null} array references):
6201      * <pre>{@code
6202      *     int i = Arrays.mismatch(a, b);
6203      *     if (i >= 0 && i < Math.min(a.length, b.length))
6204      *         return Short.compareUnsigned(a[i], b[i]);
6205      *     return a.length - b.length;
6206      * }</pre>
6207      *
6208      * @param a the first array to compare
6209      * @param b the second array to compare
6210      * @return the value {@code 0} if the first and second array are
6211      *         equal and contain the same elements in the same order;
6212      *         a value less than {@code 0} if the first array is
6213      *         lexicographically less than the second array; and
6214      *         a value greater than {@code 0} if the first array is
6215      *         lexicographically greater than the second array
6216      * @since 9
6217      */
6218     public static int compareUnsigned(short[] a, short[] b) {
6219         if (a == b)
6220             return 0;
6221         if (a == null || b == null)
6222             return a == null ? -1 : 1;
6223 
6224         int length = Math.min(a.length, b.length);
6225         for (int i = 0; i < length; i++) {
6226             if (a[i] != b[i]) return Short.compareUnsigned(a[i], b[i]);
6227         }
6228 
6229         return a.length - b.length;
6230     }
6231 
6232     /**
6233      * Compares two {@code short} arrays lexicographically over the specified
6234      * ranges, numerically treating elements as unsigned.
6235      *
6236      * <p>If the two arrays, over the specified ranges, share a common prefix
6237      * then the lexicographic comparison is the result of comparing two
6238      * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
6239      * relative index within the respective arrays that is the length of the
6240      * prefix.
6241      * Otherwise, one array is a proper prefix of the other and, lexicographic
6242      * comparison is the result of comparing the two range lengths.
6243      * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
6244      * definition of a common and proper prefix.)
6245      *
6246      * @apiNote
6247      * <p>This method behaves as if:
6248      * <pre>{@code
6249      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6250      *                             b, bFromIndex, bToIndex);
6251      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6252      *         return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6253      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6254      * }</pre>
6255      *
6256      * @param a the first array to compare
6257      * @param aFromIndex the index (inclusive) of the first element in the
6258      *                   first array to be compared
6259      * @param aToIndex the index (exclusive) of the last element in the
6260      *                 first array to be compared
6261      * @param b the second array to compare
6262      * @param bFromIndex the index (inclusive) of the first element in the
6263      *                   second array to be compared
6264      * @param bToIndex the index (exclusive) of the last element in the
6265      *                 second array to be compared
6266      * @return the value {@code 0} if, over the specified ranges, the first and
6267      *         second array are equal and contain the same elements in the same
6268      *         order;
6269      *         a value less than {@code 0} if, over the specified ranges, the
6270      *         first array is lexicographically less than the second array; and
6271      *         a value greater than {@code 0} if, over the specified ranges, the
6272      *         first array is lexicographically greater than the second array
6273      * @throws IllegalArgumentException
6274      *         if {@code aFromIndex > aToIndex} or
6275      *         if {@code bFromIndex > bToIndex}
6276      * @throws ArrayIndexOutOfBoundsException
6277      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6278      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6279      * @throws NullPointerException
6280      *         if either array is null
6281      * @since 9
6282      */
6283     public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
6284                                       short[] b, int bFromIndex, int bToIndex) {
6285         rangeCheck(a.length, aFromIndex, aToIndex);
6286         rangeCheck(b.length, bFromIndex, bToIndex);
6287 
6288         int aLength = aToIndex - aFromIndex;
6289         int bLength = bToIndex - bFromIndex;
6290         int length = Math.min(aLength, bLength);
6291         for (int i = 0; i < length; i++) {
6292             short va = a[aFromIndex++];
6293             short vb = b[bFromIndex++];
6294             if (va != vb) return Short.compareUnsigned(va, vb);
6295         }
6296 
6297         return aLength - bLength;
6298     }
6299 
6300     // Compare char
6301 
6302     /**
6303      * Compares two {@code char} arrays lexicographically.
6304      *
6305      * <p>If the two arrays share a common prefix then the lexicographic
6306      * comparison is the result of comparing two elements, as if by
6307      * {@link Character#compare(char, char)}, at an index within the respective
6308      * arrays that is the prefix length.
6309      * Otherwise, one array is a proper prefix of the other and, lexicographic
6310      * comparison is the result of comparing the two array lengths.
6311      * (See {@link #mismatch(char[], char[])} for the definition of a common and
6312      * proper prefix.)
6313      *
6314      * <p>A {@code null} array reference is considered lexicographically less
6315      * than a non-{@code null} array reference.  Two {@code null} array
6316      * references are considered equal.
6317      *
6318      * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
6319      * more specifically the following holds for arrays {@code a} and {@code b}:
6320      * <pre>{@code
6321      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6322      * }</pre>
6323      *
6324      * @apiNote
6325      * <p>This method behaves as if (for non-{@code null} array references):
6326      * <pre>{@code
6327      *     int i = Arrays.mismatch(a, b);
6328      *     if (i >= 0 && i < Math.min(a.length, b.length))
6329      *         return Character.compare(a[i], b[i]);
6330      *     return a.length - b.length;
6331      * }</pre>
6332      *
6333      * @param a the first array to compare
6334      * @param b the second array to compare
6335      * @return the value {@code 0} if the first and second array are equal and
6336      *         contain the same elements in the same order;
6337      *         a value less than {@code 0} if the first array is
6338      *         lexicographically less than the second array; and
6339      *         a value greater than {@code 0} if the first array is
6340      *         lexicographically greater than the second array
6341      * @since 9
6342      */
6343     public static int compare(char[] a, char[] b) {
6344         if (a == b)
6345             return 0;
6346         if (a == null || b == null)
6347             return a == null ? -1 : 1;
6348 
6349         int length = Math.min(a.length, b.length);
6350         for (int i = 0; i < length; i++) {
6351             if (a[i] != b[i]) return Character.compare(a[i], b[i]);
6352         }
6353 
6354         return a.length - b.length;
6355     }
6356 
6357     /**
6358      * Compares two {@code char} arrays lexicographically over the specified
6359      * ranges.
6360      *
6361      * <p>If the two arrays, over the specified ranges, share a common prefix
6362      * then the lexicographic comparison is the result of comparing two
6363      * elements, as if by {@link Character#compare(char, char)}, at a relative
6364      * index within the respective arrays that is the length of the prefix.
6365      * Otherwise, one array is a proper prefix of the other and, lexicographic
6366      * comparison is the result of comparing the two range lengths.
6367      * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
6368      * definition of a common and proper prefix.)
6369      *
6370      * <p>The comparison is consistent with
6371      * {@link #equals(char[], int, int, char[], int, int) equals}, more
6372      * specifically the following holds for arrays {@code a} and {@code b} with
6373      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6374      * [{@code bFromIndex}, {@code btoIndex}) respectively:
6375      * <pre>{@code
6376      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6377      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6378      * }</pre>
6379      *
6380      * @apiNote
6381      * <p>This method behaves as if:
6382      * <pre>{@code
6383      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6384      *                             b, bFromIndex, bToIndex);
6385      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6386      *         return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
6387      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6388      * }</pre>
6389      *
6390      * @param a the first array to compare
6391      * @param aFromIndex the index (inclusive) of the first element in the
6392      *                   first array to be compared
6393      * @param aToIndex the index (exclusive) of the last element in the
6394      *                 first array to be compared
6395      * @param b the second array to compare
6396      * @param bFromIndex the index (inclusive) of the first element in the
6397      *                   second array to be compared
6398      * @param bToIndex the index (exclusive) of the last element in the
6399      *                 second array to be compared
6400      * @return the value {@code 0} if, over the specified ranges, the first and
6401      *         second array are equal and contain the same elements in the same
6402      *         order;
6403      *         a value less than {@code 0} if, over the specified ranges, the
6404      *         first array is lexicographically less than the second array; and
6405      *         a value greater than {@code 0} if, over the specified ranges, the
6406      *         first array is lexicographically greater than the second array
6407      * @throws IllegalArgumentException
6408      *         if {@code aFromIndex > aToIndex} or
6409      *         if {@code bFromIndex > bToIndex}
6410      * @throws ArrayIndexOutOfBoundsException
6411      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6412      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6413      * @throws NullPointerException
6414      *         if either array is {@code null}
6415      * @since 9
6416      */
6417     public static int compare(char[] a, int aFromIndex, int aToIndex,
6418                               char[] b, int bFromIndex, int bToIndex) {
6419         rangeCheck(a.length, aFromIndex, aToIndex);
6420         rangeCheck(b.length, bFromIndex, bToIndex);
6421 
6422         int aLength = aToIndex - aFromIndex;
6423         int bLength = bToIndex - bFromIndex;
6424         int length = Math.min(aLength, bLength);
6425         for (int i = 0; i < length; i++) {
6426             char va = a[aFromIndex++];
6427             char vb = b[bFromIndex++];
6428             if (va != vb) return Character.compare(va, vb);
6429         }
6430 
6431         return aLength - bLength;
6432     }
6433 
6434     // Compare int
6435 
6436     /**
6437      * Compares two {@code int} arrays lexicographically.
6438      *
6439      * <p>If the two arrays share a common prefix then the lexicographic
6440      * comparison is the result of comparing two elements, as if by
6441      * {@link Integer#compare(int, int)}, at an index within the respective
6442      * arrays that is the prefix length.
6443      * Otherwise, one array is a proper prefix of the other and, lexicographic
6444      * comparison is the result of comparing the two array lengths.
6445      * (See {@link #mismatch(int[], int[])} for the definition of a common and
6446      * proper prefix.)
6447      *
6448      * <p>A {@code null} array reference is considered lexicographically less
6449      * than a non-{@code null} array reference.  Two {@code null} array
6450      * references are considered equal.
6451      *
6452      * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
6453      * more specifically the following holds for arrays {@code a} and {@code b}:
6454      * <pre>{@code
6455      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6456      * }</pre>
6457      *
6458      * @apiNote
6459      * <p>This method behaves as if (for non-{@code null} array references):
6460      * <pre>{@code
6461      *     int i = Arrays.mismatch(a, b);
6462      *     if (i >= 0 && i < Math.min(a.length, b.length))
6463      *         return Integer.compare(a[i], b[i]);
6464      *     return a.length - b.length;
6465      * }</pre>
6466      *
6467      * @param a the first array to compare
6468      * @param b the second array to compare
6469      * @return the value {@code 0} if the first and second array are equal and
6470      *         contain the same elements in the same order;
6471      *         a value less than {@code 0} if the first array is
6472      *         lexicographically less than the second array; and
6473      *         a value greater than {@code 0} if the first array is
6474      *         lexicographically greater than the second array
6475      * @since 9
6476      */
6477     public static int compare(int[] a, int[] b) {
6478         if (a == b)
6479             return 0;
6480         if (a == null || b == null)
6481             return a == null ? -1 : 1;
6482 
6483         int length = Math.min(a.length, b.length);
6484         for (int i = 0; i < length; i++) {
6485             if (a[i] != b[i]) return Integer.compare(a[i], b[i]);
6486         }
6487 
6488         return a.length - b.length;
6489     }
6490 
6491     /**
6492      * Compares two {@code int} arrays lexicographically over the specified
6493      * ranges.
6494      *
6495      * <p>If the two arrays, over the specified ranges, share a common prefix
6496      * then the lexicographic comparison is the result of comparing two
6497      * elements, as if by {@link Integer#compare(int, int)}, at a relative index
6498      * within the respective arrays that is the length of the prefix.
6499      * Otherwise, one array is a proper prefix of the other and, lexicographic
6500      * comparison is the result of comparing the two range lengths.
6501      * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
6502      * definition of a common and proper prefix.)
6503      *
6504      * <p>The comparison is consistent with
6505      * {@link #equals(int[], int, int, int[], int, int) equals}, more
6506      * specifically the following holds for arrays {@code a} and {@code b} with
6507      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6508      * [{@code bFromIndex}, {@code btoIndex}) respectively:
6509      * <pre>{@code
6510      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6511      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6512      * }</pre>
6513      *
6514      * @apiNote
6515      * <p>This method behaves as if:
6516      * <pre>{@code
6517      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6518      *                             b, bFromIndex, bToIndex);
6519      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6520      *         return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
6521      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6522      * }</pre>
6523      *
6524      * @param a the first array to compare
6525      * @param aFromIndex the index (inclusive) of the first element in the
6526      *                   first array to be compared
6527      * @param aToIndex the index (exclusive) of the last element in the
6528      *                 first array to be compared
6529      * @param b the second array to compare
6530      * @param bFromIndex the index (inclusive) of the first element in the
6531      *                   second array to be compared
6532      * @param bToIndex the index (exclusive) of the last element in the
6533      *                 second array to be compared
6534      * @return the value {@code 0} if, over the specified ranges, the first and
6535      *         second array are equal and contain the same elements in the same
6536      *         order;
6537      *         a value less than {@code 0} if, over the specified ranges, the
6538      *         first array is lexicographically less than the second array; and
6539      *         a value greater than {@code 0} if, over the specified ranges, the
6540      *         first array is lexicographically greater than the second array
6541      * @throws IllegalArgumentException
6542      *         if {@code aFromIndex > aToIndex} or
6543      *         if {@code bFromIndex > bToIndex}
6544      * @throws ArrayIndexOutOfBoundsException
6545      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6546      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6547      * @throws NullPointerException
6548      *         if either array is {@code null}
6549      * @since 9
6550      */
6551     public static int compare(int[] a, int aFromIndex, int aToIndex,
6552                               int[] b, int bFromIndex, int bToIndex) {
6553         rangeCheck(a.length, aFromIndex, aToIndex);
6554         rangeCheck(b.length, bFromIndex, bToIndex);
6555 
6556         int aLength = aToIndex - aFromIndex;
6557         int bLength = bToIndex - bFromIndex;
6558         int length = Math.min(aLength, bLength);
6559         for (int i = 0; i < length; i++) {
6560             int va = a[aFromIndex++];
6561             int vb = b[bFromIndex++];
6562             if (va != vb) return Integer.compare(va, vb);
6563         }
6564 
6565         return aLength - bLength;
6566     }
6567 
6568     /**
6569      * Compares two {@code int} arrays lexicographically, numerically treating
6570      * elements as unsigned.
6571      *
6572      * <p>If the two arrays share a common prefix then the lexicographic
6573      * comparison is the result of comparing two elements, as if by
6574      * {@link Integer#compareUnsigned(int, int)}, at an index within the
6575      * respective arrays that is the prefix length.
6576      * Otherwise, one array is a proper prefix of the other and, lexicographic
6577      * comparison is the result of comparing the two array lengths.
6578      * (See {@link #mismatch(int[], int[])} for the definition of a common
6579      * and proper prefix.)
6580      *
6581      * <p>A {@code null} array reference is considered lexicographically less
6582      * than a non-{@code null} array reference.  Two {@code null} array
6583      * references are considered equal.
6584      *
6585      * @apiNote
6586      * <p>This method behaves as if (for non-{@code null} array references):
6587      * <pre>{@code
6588      *     int i = Arrays.mismatch(a, b);
6589      *     if (i >= 0 && i < Math.min(a.length, b.length))
6590      *         return Integer.compareUnsigned(a[i], b[i]);
6591      *     return a.length - b.length;
6592      * }</pre>
6593      *
6594      * @param a the first array to compare
6595      * @param b the second array to compare
6596      * @return the value {@code 0} if the first and second array are
6597      *         equal and contain the same elements in the same order;
6598      *         a value less than {@code 0} if the first array is
6599      *         lexicographically less than the second array; and
6600      *         a value greater than {@code 0} if the first array is
6601      *         lexicographically greater than the second array
6602      * @since 9
6603      */
6604     public static int compareUnsigned(int[] a, int[] b) {
6605         if (a == b)
6606             return 0;
6607         if (a == null || b == null)
6608             return a == null ? -1 : 1;
6609 
6610         int length = Math.min(a.length, b.length);
6611         for (int i = 0; i < length; i++) {
6612             if (a[i] != b[i]) return Integer.compareUnsigned(a[i], b[i]);
6613         }
6614 
6615         return a.length - b.length;
6616     }
6617 
6618     /**
6619      * Compares two {@code int} arrays lexicographically over the specified
6620      * ranges, numerically treating elements as unsigned.
6621      *
6622      * <p>If the two arrays, over the specified ranges, share a common prefix
6623      * then the lexicographic comparison is the result of comparing two
6624      * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
6625      * relative index within the respective arrays that is the length of the
6626      * prefix.
6627      * Otherwise, one array is a proper prefix of the other and, lexicographic
6628      * comparison is the result of comparing the two range lengths.
6629      * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
6630      * definition of a common and proper prefix.)
6631      *
6632      * @apiNote
6633      * <p>This method behaves as if:
6634      * <pre>{@code
6635      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6636      *                             b, bFromIndex, bToIndex);
6637      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6638      *         return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6639      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6640      * }</pre>
6641      *
6642      * @param a the first array to compare
6643      * @param aFromIndex the index (inclusive) of the first element in the
6644      *                   first array to be compared
6645      * @param aToIndex the index (exclusive) of the last element in the
6646      *                 first array to be compared
6647      * @param b the second array to compare
6648      * @param bFromIndex the index (inclusive) of the first element in the
6649      *                   second array to be compared
6650      * @param bToIndex the index (exclusive) of the last element in the
6651      *                 second array to be compared
6652      * @return the value {@code 0} if, over the specified ranges, the first and
6653      *         second array are equal and contain the same elements in the same
6654      *         order;
6655      *         a value less than {@code 0} if, over the specified ranges, the
6656      *         first array is lexicographically less than the second array; and
6657      *         a value greater than {@code 0} if, over the specified ranges, the
6658      *         first array is lexicographically greater than the second array
6659      * @throws IllegalArgumentException
6660      *         if {@code aFromIndex > aToIndex} or
6661      *         if {@code bFromIndex > bToIndex}
6662      * @throws ArrayIndexOutOfBoundsException
6663      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6664      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6665      * @throws NullPointerException
6666      *         if either array is null
6667      * @since 9
6668      */
6669     public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
6670                                       int[] b, int bFromIndex, int bToIndex) {
6671         rangeCheck(a.length, aFromIndex, aToIndex);
6672         rangeCheck(b.length, bFromIndex, bToIndex);
6673 
6674         int aLength = aToIndex - aFromIndex;
6675         int bLength = bToIndex - bFromIndex;
6676         int length = Math.min(aLength, bLength);
6677         for (int i = 0; i < length; i++) {
6678             int va = a[aFromIndex++];
6679             int vb = b[bFromIndex++];
6680             if (va != vb) return Integer.compareUnsigned(va, vb);
6681         }
6682 
6683         return aLength - bLength;
6684     }
6685 
6686     // Compare long
6687 
6688     /**
6689      * Compares two {@code long} arrays lexicographically.
6690      *
6691      * <p>If the two arrays share a common prefix then the lexicographic
6692      * comparison is the result of comparing two elements, as if by
6693      * {@link Long#compare(long, long)}, at an index within the respective
6694      * arrays that is the prefix length.
6695      * Otherwise, one array is a proper prefix of the other and, lexicographic
6696      * comparison is the result of comparing the two array lengths.
6697      * (See {@link #mismatch(long[], long[])} for the definition of a common and
6698      * proper prefix.)
6699      *
6700      * <p>A {@code null} array reference is considered lexicographically less
6701      * than a non-{@code null} array reference.  Two {@code null} array
6702      * references are considered equal.
6703      *
6704      * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
6705      * more specifically the following holds for arrays {@code a} and {@code b}:
6706      * <pre>{@code
6707      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6708      * }</pre>
6709      *
6710      * @apiNote
6711      * <p>This method behaves as if (for non-{@code null} array references):
6712      * <pre>{@code
6713      *     int i = Arrays.mismatch(a, b);
6714      *     if (i >= 0 && i < Math.min(a.length, b.length))
6715      *         return Long.compare(a[i], b[i]);
6716      *     return a.length - b.length;
6717      * }</pre>
6718      *
6719      * @param a the first array to compare
6720      * @param b the second array to compare
6721      * @return the value {@code 0} if the first and second array are equal and
6722      *         contain the same elements in the same order;
6723      *         a value less than {@code 0} if the first array is
6724      *         lexicographically less than the second array; and
6725      *         a value greater than {@code 0} if the first array is
6726      *         lexicographically greater than the second array
6727      * @since 9
6728      */
6729     public static int compare(long[] a, long[] b) {
6730         if (a == b)
6731             return 0;
6732         if (a == null || b == null)
6733             return a == null ? -1 : 1;
6734 
6735         int length = Math.min(a.length, b.length);
6736         for (int i = 0; i < length; i++) {
6737             if (a[i] != b[i]) return Long.compare(a[i], b[i]);
6738         }
6739 
6740         return a.length - b.length;
6741     }
6742 
6743     /**
6744      * Compares two {@code long} arrays lexicographically over the specified
6745      * ranges.
6746      *
6747      * <p>If the two arrays, over the specified ranges, share a common prefix
6748      * then the lexicographic comparison is the result of comparing two
6749      * elements, as if by {@link Long#compare(long, long)}, at a relative index
6750      * within the respective arrays that is the length of the prefix.
6751      * Otherwise, one array is a proper prefix of the other and, lexicographic
6752      * comparison is the result of comparing the two range lengths.
6753      * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
6754      * definition of a common and proper prefix.)
6755      *
6756      * <p>The comparison is consistent with
6757      * {@link #equals(long[], int, int, long[], int, int) equals}, more
6758      * specifically the following holds for arrays {@code a} and {@code b} with
6759      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6760      * [{@code bFromIndex}, {@code btoIndex}) respectively:
6761      * <pre>{@code
6762      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6763      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6764      * }</pre>
6765      *
6766      * @apiNote
6767      * <p>This method behaves as if:
6768      * <pre>{@code
6769      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6770      *                             b, bFromIndex, bToIndex);
6771      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6772      *         return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
6773      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6774      * }</pre>
6775      *
6776      * @param a the first array to compare
6777      * @param aFromIndex the index (inclusive) of the first element in the
6778      *                   first array to be compared
6779      * @param aToIndex the index (exclusive) of the last element in the
6780      *                 first array to be compared
6781      * @param b the second array to compare
6782      * @param bFromIndex the index (inclusive) of the first element in the
6783      *                   second array to be compared
6784      * @param bToIndex the index (exclusive) of the last element in the
6785      *                 second array to be compared
6786      * @return the value {@code 0} if, over the specified ranges, the first and
6787      *         second array are equal and contain the same elements in the same
6788      *         order;
6789      *         a value less than {@code 0} if, over the specified ranges, the
6790      *         first array is lexicographically less than the second array; and
6791      *         a value greater than {@code 0} if, over the specified ranges, the
6792      *         first array is lexicographically greater than the second array
6793      * @throws IllegalArgumentException
6794      *         if {@code aFromIndex > aToIndex} or
6795      *         if {@code bFromIndex > bToIndex}
6796      * @throws ArrayIndexOutOfBoundsException
6797      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6798      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6799      * @throws NullPointerException
6800      *         if either array is {@code null}
6801      * @since 9
6802      */
6803     public static int compare(long[] a, int aFromIndex, int aToIndex,
6804                               long[] b, int bFromIndex, int bToIndex) {
6805         rangeCheck(a.length, aFromIndex, aToIndex);
6806         rangeCheck(b.length, bFromIndex, bToIndex);
6807 
6808         int aLength = aToIndex - aFromIndex;
6809         int bLength = bToIndex - bFromIndex;
6810         int length = Math.min(aLength, bLength);
6811         for (int i = 0; i < length; i++) {
6812             long va = a[aFromIndex++];
6813             long vb = b[bFromIndex++];
6814             if (va != vb) return Long.compare(va, vb);
6815         }
6816 
6817         return aLength - bLength;
6818     }
6819 
6820     /**
6821      * Compares two {@code long} arrays lexicographically, numerically treating
6822      * elements as unsigned.
6823      *
6824      * <p>If the two arrays share a common prefix then the lexicographic
6825      * comparison is the result of comparing two elements, as if by
6826      * {@link Long#compareUnsigned(long, long)}, at an index within the
6827      * respective arrays that is the prefix length.
6828      * Otherwise, one array is a proper prefix of the other and, lexicographic
6829      * comparison is the result of comparing the two array lengths.
6830      * (See {@link #mismatch(long[], long[])} for the definition of a common
6831      * and proper prefix.)
6832      *
6833      * <p>A {@code null} array reference is considered lexicographically less
6834      * than a non-{@code null} array reference.  Two {@code null} array
6835      * references are considered equal.
6836      *
6837      * @apiNote
6838      * <p>This method behaves as if (for non-{@code null} array references):
6839      * <pre>{@code
6840      *     int i = Arrays.mismatch(a, b);
6841      *     if (i >= 0 && i < Math.min(a.length, b.length))
6842      *         return Long.compareUnsigned(a[i], b[i]);
6843      *     return a.length - b.length;
6844      * }</pre>
6845      *
6846      * @param a the first array to compare
6847      * @param b the second array to compare
6848      * @return the value {@code 0} if the first and second array are
6849      *         equal and contain the same elements in the same order;
6850      *         a value less than {@code 0} if the first array is
6851      *         lexicographically less than the second array; and
6852      *         a value greater than {@code 0} if the first array is
6853      *         lexicographically greater than the second array
6854      * @since 9
6855      */
6856     public static int compareUnsigned(long[] a, long[] b) {
6857         if (a == b)
6858             return 0;
6859         if (a == null || b == null)
6860             return a == null ? -1 : 1;
6861 
6862         int length = Math.min(a.length, b.length);
6863         for (int i = 0; i < length; i++) {
6864             if (a[i] != b[i]) return Long.compareUnsigned(a[i], b[i]);
6865         }
6866 
6867         return a.length - b.length;
6868     }
6869 
6870     /**
6871      * Compares two {@code long} arrays lexicographically over the specified
6872      * ranges, numerically treating elements as unsigned.
6873      *
6874      * <p>If the two arrays, over the specified ranges, share a common prefix
6875      * then the lexicographic comparison is the result of comparing two
6876      * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
6877      * relative index within the respective arrays that is the length of the
6878      * prefix.
6879      * Otherwise, one array is a proper prefix of the other and, lexicographic
6880      * comparison is the result of comparing the two range lengths.
6881      * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
6882      * definition of a common and proper prefix.)
6883      *
6884      * @apiNote
6885      * <p>This method behaves as if:
6886      * <pre>{@code
6887      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6888      *                             b, bFromIndex, bToIndex);
6889      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6890      *         return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6891      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6892      * }</pre>
6893      *
6894      * @param a the first array to compare
6895      * @param aFromIndex the index (inclusive) of the first element in the
6896      *                   first array to be compared
6897      * @param aToIndex the index (exclusive) of the last element in the
6898      *                 first array to be compared
6899      * @param b the second array to compare
6900      * @param bFromIndex the index (inclusive) of the first element in the
6901      *                   second array to be compared
6902      * @param bToIndex the index (exclusive) of the last element in the
6903      *                 second array to be compared
6904      * @return the value {@code 0} if, over the specified ranges, the first and
6905      *         second array are equal and contain the same elements in the same
6906      *         order;
6907      *         a value less than {@code 0} if, over the specified ranges, the
6908      *         first array is lexicographically less than the second array; and
6909      *         a value greater than {@code 0} if, over the specified ranges, the
6910      *         first array is lexicographically greater than the second array
6911      * @throws IllegalArgumentException
6912      *         if {@code aFromIndex > aToIndex} or
6913      *         if {@code bFromIndex > bToIndex}
6914      * @throws ArrayIndexOutOfBoundsException
6915      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
6916      *         if {@code bFromIndex < 0 or bToIndex > b.length}
6917      * @throws NullPointerException
6918      *         if either array is null
6919      * @since 9
6920      */
6921     public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
6922                                       long[] b, int bFromIndex, int bToIndex) {
6923         rangeCheck(a.length, aFromIndex, aToIndex);
6924         rangeCheck(b.length, bFromIndex, bToIndex);
6925 
6926         int aLength = aToIndex - aFromIndex;
6927         int bLength = bToIndex - bFromIndex;
6928         int length = Math.min(aLength, bLength);
6929         for (int i = 0; i < length; i++) {
6930             long va = a[aFromIndex++];
6931             long vb = b[bFromIndex++];
6932             if (va != vb) return Long.compareUnsigned(va, vb);
6933         }
6934 
6935         return aLength - bLength;
6936     }
6937 
6938     // Compare float
6939 
6940     /**
6941      * Compares two {@code float} arrays lexicographically.
6942      *
6943      * <p>If the two arrays share a common prefix then the lexicographic
6944      * comparison is the result of comparing two elements, as if by
6945      * {@link Float#compare(float, float)}, at an index within the respective
6946      * arrays that is the prefix length.
6947      * Otherwise, one array is a proper prefix of the other and, lexicographic
6948      * comparison is the result of comparing the two array lengths.
6949      * (See {@link #mismatch(float[], float[])} for the definition of a common
6950      * and proper prefix.)
6951      *
6952      * <p>A {@code null} array reference is considered lexicographically less
6953      * than a non-{@code null} array reference.  Two {@code null} array
6954      * references are considered equal.
6955      *
6956      * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
6957      * more specifically the following holds for arrays {@code a} and {@code b}:
6958      * <pre>{@code
6959      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6960      * }</pre>
6961      *
6962      * @apiNote
6963      * <p>This method behaves as if (for non-{@code null} array references):
6964      * <pre>{@code
6965      *     int i = Arrays.mismatch(a, b);
6966      *     if (i >= 0 && i < Math.min(a.length, b.length))
6967      *         return Float.compare(a[i], b[i]);
6968      *     return a.length - b.length;
6969      * }</pre>
6970      *
6971      * @param a the first array to compare
6972      * @param b the second array to compare
6973      * @return the value {@code 0} if the first and second array are equal and
6974      *         contain the same elements in the same order;
6975      *         a value less than {@code 0} if the first array is
6976      *         lexicographically less than the second array; and
6977      *         a value greater than {@code 0} if the first array is
6978      *         lexicographically greater than the second array
6979      * @since 9
6980      */
6981     public static int compare(float[] a, float[] b) {
6982         if (a == b)
6983             return 0;
6984         if (a == null || b == null)
6985             return a == null ? -1 : 1;
6986 
6987         int length = Math.min(a.length, b.length);
6988         for (int i = 0; i < length; i++) {
6989             float va = a[i], vb = b[i];
6990             if (Float.floatToRawIntBits(va) != Float.floatToRawIntBits(vb)) {
6991                 int c = Float.compare(va, vb);
6992                 if (c != 0) return c;
6993             }
6994         }
6995 
6996         return a.length - b.length;
6997     }
6998 
6999     /**
7000      * Compares two {@code float} arrays lexicographically over the specified
7001      * ranges.
7002      *
7003      * <p>If the two arrays, over the specified ranges, share a common prefix
7004      * then the lexicographic comparison is the result of comparing two
7005      * elements, as if by {@link Float#compare(float, float)}, at a relative
7006      * index within the respective arrays that is the length of the prefix.
7007      * Otherwise, one array is a proper prefix of the other and, lexicographic
7008      * comparison is the result of comparing the two range lengths.
7009      * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
7010      * definition of a common and proper prefix.)
7011      *
7012      * <p>The comparison is consistent with
7013      * {@link #equals(float[], int, int, float[], int, int) equals}, more
7014      * specifically the following holds for arrays {@code a} and {@code b} with
7015      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7016      * [{@code bFromIndex}, {@code btoIndex}) respectively:
7017      * <pre>{@code
7018      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7019      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7020      * }</pre>
7021      *
7022      * @apiNote
7023      * <p>This method behaves as if:
7024      * <pre>{@code
7025      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7026      *                             b, bFromIndex, bToIndex);
7027      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7028      *         return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
7029      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7030      * }</pre>
7031      *
7032      * @param a the first array to compare
7033      * @param aFromIndex the index (inclusive) of the first element in the
7034      *                   first array to be compared
7035      * @param aToIndex the index (exclusive) of the last element in the
7036      *                 first array to be compared
7037      * @param b the second array to compare
7038      * @param bFromIndex the index (inclusive) of the first element in the
7039      *                   second array to be compared
7040      * @param bToIndex the index (exclusive) of the last element in the
7041      *                 second array to be compared
7042      * @return the value {@code 0} if, over the specified ranges, the first and
7043      *         second array are equal and contain the same elements in the same
7044      *         order;
7045      *         a value less than {@code 0} if, over the specified ranges, the
7046      *         first array is lexicographically less than the second array; and
7047      *         a value greater than {@code 0} if, over the specified ranges, the
7048      *         first array is lexicographically greater than the second array
7049      * @throws IllegalArgumentException
7050      *         if {@code aFromIndex > aToIndex} or
7051      *         if {@code bFromIndex > bToIndex}
7052      * @throws ArrayIndexOutOfBoundsException
7053      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7054      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7055      * @throws NullPointerException
7056      *         if either array is {@code null}
7057      * @since 9
7058      */
7059     public static int compare(float[] a, int aFromIndex, int aToIndex,
7060                               float[] b, int bFromIndex, int bToIndex) {
7061         rangeCheck(a.length, aFromIndex, aToIndex);
7062         rangeCheck(b.length, bFromIndex, bToIndex);
7063 
7064         int aLength = aToIndex - aFromIndex;
7065         int bLength = bToIndex - bFromIndex;
7066         int length = Math.min(aLength, bLength);
7067         for (int i = 0; i < length; i++) {
7068             float va = a[aFromIndex++], vb = b[bFromIndex++];
7069             if (Float.floatToRawIntBits(va) != Float.floatToRawIntBits(vb)) {
7070                 int c = Float.compare(va, vb);
7071                 if (c != 0) return c;
7072             }
7073         }
7074 
7075         return aLength - bLength;
7076     }
7077 
7078     // Compare double
7079 
7080     /**
7081      * Compares two {@code double} arrays lexicographically.
7082      *
7083      * <p>If the two arrays share a common prefix then the lexicographic
7084      * comparison is the result of comparing two elements, as if by
7085      * {@link Double#compare(double, double)}, at an index within the respective
7086      * arrays that is the prefix length.
7087      * Otherwise, one array is a proper prefix of the other and, lexicographic
7088      * comparison is the result of comparing the two array lengths.
7089      * (See {@link #mismatch(double[], double[])} for the definition of a common
7090      * and proper prefix.)
7091      *
7092      * <p>A {@code null} array reference is considered lexicographically less
7093      * than a non-{@code null} array reference.  Two {@code null} array
7094      * references are considered equal.
7095      *
7096      * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
7097      * more specifically the following holds for arrays {@code a} and {@code b}:
7098      * <pre>{@code
7099      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
7100      * }</pre>
7101      *
7102      * @apiNote
7103      * <p>This method behaves as if (for non-{@code null} array references):
7104      * <pre>{@code
7105      *     int i = Arrays.mismatch(a, b);
7106      *     if (i >= 0 && i < Math.min(a.length, b.length))
7107      *         return Double.compare(a[i], b[i]);
7108      *     return a.length - b.length;
7109      * }</pre>
7110      *
7111      * @param a the first array to compare
7112      * @param b the second array to compare
7113      * @return the value {@code 0} if the first and second array are equal and
7114      *         contain the same elements in the same order;
7115      *         a value less than {@code 0} if the first array is
7116      *         lexicographically less than the second array; and
7117      *         a value greater than {@code 0} if the first array is
7118      *         lexicographically greater than the second array
7119      * @since 9
7120      */
7121     public static int compare(double[] a, double[] b) {
7122         if (a == b)
7123             return 0;
7124         if (a == null || b == null)
7125             return a == null ? -1 : 1;
7126 
7127         int length = Math.min(a.length, b.length);
7128         for (int i = 0; i < length; i++) {
7129             double va = a[i], vb = b[i];
7130             if (Double.doubleToRawLongBits(va) != Double.doubleToRawLongBits(vb)) {
7131                 int c = Double.compare(va, vb);
7132                 if (c != 0) return c;
7133             }
7134         }
7135 
7136         return a.length - b.length;
7137     }
7138 
7139     /**
7140      * Compares two {@code double} arrays lexicographically over the specified
7141      * ranges.
7142      *
7143      * <p>If the two arrays, over the specified ranges, share a common prefix
7144      * then the lexicographic comparison is the result of comparing two
7145      * elements, as if by {@link Double#compare(double, double)}, at a relative
7146      * index within the respective arrays that is the length of the prefix.
7147      * Otherwise, one array is a proper prefix of the other and, lexicographic
7148      * comparison is the result of comparing the two range lengths.
7149      * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
7150      * definition of a common and proper prefix.)
7151      *
7152      * <p>The comparison is consistent with
7153      * {@link #equals(double[], int, int, double[], int, int) equals}, more
7154      * specifically the following holds for arrays {@code a} and {@code b} with
7155      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7156      * [{@code bFromIndex}, {@code btoIndex}) respectively:
7157      * <pre>{@code
7158      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7159      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7160      * }</pre>
7161      *
7162      * @apiNote
7163      * <p>This method behaves as if:
7164      * <pre>{@code
7165      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7166      *                             b, bFromIndex, bToIndex);
7167      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7168      *         return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
7169      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7170      * }</pre>
7171      *
7172      * @param a the first array to compare
7173      * @param aFromIndex the index (inclusive) of the first element in the
7174      *                   first array to be compared
7175      * @param aToIndex the index (exclusive) of the last element in the
7176      *                 first array to be compared
7177      * @param b the second array to compare
7178      * @param bFromIndex the index (inclusive) of the first element in the
7179      *                   second array to be compared
7180      * @param bToIndex the index (exclusive) of the last element in the
7181      *                 second array to be compared
7182      * @return the value {@code 0} if, over the specified ranges, the first and
7183      *         second array are equal and contain the same elements in the same
7184      *         order;
7185      *         a value less than {@code 0} if, over the specified ranges, the
7186      *         first array is lexicographically less than the second array; and
7187      *         a value greater than {@code 0} if, over the specified ranges, the
7188      *         first array is lexicographically greater than the second array
7189      * @throws IllegalArgumentException
7190      *         if {@code aFromIndex > aToIndex} or
7191      *         if {@code bFromIndex > bToIndex}
7192      * @throws ArrayIndexOutOfBoundsException
7193      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7194      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7195      * @throws NullPointerException
7196      *         if either array is {@code null}
7197      * @since 9
7198      */
7199     public static int compare(double[] a, int aFromIndex, int aToIndex,
7200                               double[] b, int bFromIndex, int bToIndex) {
7201         rangeCheck(a.length, aFromIndex, aToIndex);
7202         rangeCheck(b.length, bFromIndex, bToIndex);
7203 
7204         int aLength = aToIndex - aFromIndex;
7205         int bLength = bToIndex - bFromIndex;
7206         int length = Math.min(aLength, bLength);
7207         for (int i = 0; i < length; i++) {
7208             double va = a[aFromIndex++], vb = b[bFromIndex++];
7209             if (Double.doubleToRawLongBits(va) != Double.doubleToRawLongBits(vb)) {
7210                 int c = Double.compare(va, vb);
7211                 if (c != 0) return c;
7212             }
7213         }
7214 
7215         return aLength - bLength;
7216     }
7217 
7218     // Compare objects
7219 
7220     /**
7221      * Compares two {@code Object} arrays, within comparable elements,
7222      * lexicographically.
7223      *
7224      * <p>If the two arrays share a common prefix then the lexicographic
7225      * comparison is the result of comparing two elements of type {@code T} at
7226      * an index {@code i} within the respective arrays that is the prefix
7227      * length, as if by:
7228      * <pre>{@code
7229      *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
7230      *         compare(a[i], b[i])
7231      * }</pre>
7232      * Otherwise, one array is a proper prefix of the other and, lexicographic
7233      * comparison is the result of comparing the two array lengths.
7234      * (See {@link #mismatch(Object[], Object[])} for the definition of a common
7235      * and proper prefix.)
7236      *
7237      * <p>A {@code null} array reference is considered lexicographically less
7238      * than a non-{@code null} array reference.  Two {@code null} array
7239      * references are considered equal.
7240      * A {@code null} array element is considered lexicographically than a
7241      * non-{@code null} array element.  Two {@code null} array elements are
7242      * considered equal.
7243      *
7244      * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
7245      * more specifically the following holds for arrays {@code a} and {@code b}:
7246      * <pre>{@code
7247      *     Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
7248      * }</pre>
7249      *
7250      * @apiNote
7251      * <p>This method behaves as if (for non-{@code null} array references
7252      * and elements):
7253      * <pre>{@code
7254      *     int i = Arrays.mismatch(a, b);
7255      *     if (i >= 0 && i < Math.min(a.length, b.length))
7256      *         return a[i].compareTo(b[i]);
7257      *     return a.length - b.length;
7258      * }</pre>
7259      *
7260      * @param a the first array to compare
7261      * @param b the second array to compare
7262      * @param <T> the type of comparable array elements
7263      * @return the value {@code 0} if the first and second array are equal and
7264      *         contain the same elements in the same order;
7265      *         a value less than {@code 0} if the first array is
7266      *         lexicographically less than the second array; and
7267      *         a value greater than {@code 0} if the first array is
7268      *         lexicographically greater than the second array
7269      * @since 9
7270      */
7271     public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
7272         if (a == b)
7273             return 0;
7274         // A null array is less than a non-null array
7275         if (a == null || b == null)
7276             return a == null ? -1 : 1;
7277 
7278         int length = Math.min(a.length, b.length);
7279         for (int i = 0; i < length; i++) {
7280             T oa = a[i];
7281             T ob = b[i];
7282             if (oa != ob) {
7283                 // A null element is less than a non-null element
7284                 if (oa == null || ob == null)
7285                     return oa == null ? -1 : 1;
7286                 int v = oa.compareTo(ob);
7287                 if (v != 0) {
7288                     return v;
7289                 }
7290             }
7291         }
7292 
7293         return a.length - b.length;
7294     }
7295 
7296     /**
7297      * Compares two {@code Object} arrays lexicographically over the specified
7298      * ranges.
7299      *
7300      * <p>If the two arrays, over the specified ranges, share a common prefix
7301      * then the lexicographic comparison is the result of comparing two
7302      * elements of type {@code T} at a relative index {@code i} within the
7303      * respective arrays that is the prefix length, as if by:
7304      * <pre>{@code
7305      *     Comparator.nullsFirst(Comparator.<T>naturalOrder()).
7306      *         compare(a[aFromIndex + i, b[bFromIndex + i])
7307      * }</pre>
7308      * Otherwise, one array is a proper prefix of the other and, lexicographic
7309      * comparison is the result of comparing the two range lengths.
7310      * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
7311      * definition of a common and proper prefix.)
7312      *
7313      * <p>The comparison is consistent with
7314      * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
7315      * specifically the following holds for arrays {@code a} and {@code b} with
7316      * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7317      * [{@code bFromIndex}, {@code btoIndex}) respectively:
7318      * <pre>{@code
7319      *     Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7320      *         (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7321      * }</pre>
7322      *
7323      * @apiNote
7324      * <p>This method behaves as if (for non-{@code null} array elements):
7325      * <pre>{@code
7326      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7327      *                             b, bFromIndex, bToIndex);
7328      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7329      *         return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
7330      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7331      * }</pre>
7332      *
7333      * @param a the first array to compare
7334      * @param aFromIndex the index (inclusive) of the first element in the
7335      *                   first array to be compared
7336      * @param aToIndex the index (exclusive) of the last element in the
7337      *                 first array to be compared
7338      * @param b the second array to compare
7339      * @param bFromIndex the index (inclusive) of the first element in the
7340      *                   second array to be compared
7341      * @param bToIndex the index (exclusive) of the last element in the
7342      *                 second array to be compared
7343      * @param <T> the type of comparable array elements
7344      * @return the value {@code 0} if, over the specified ranges, the first and
7345      *         second array are equal and contain the same elements in the same
7346      *         order;
7347      *         a value less than {@code 0} if, over the specified ranges, the
7348      *         first array is lexicographically less than the second array; and
7349      *         a value greater than {@code 0} if, over the specified ranges, the
7350      *         first array is lexicographically greater than the second array
7351      * @throws IllegalArgumentException
7352      *         if {@code aFromIndex > aToIndex} or
7353      *         if {@code bFromIndex > bToIndex}
7354      * @throws ArrayIndexOutOfBoundsException
7355      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7356      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7357      * @throws NullPointerException
7358      *         if either array is {@code null}
7359      * @since 9
7360      */
7361     public static <T extends Comparable<? super T>> int compare(
7362             T[] a, int aFromIndex, int aToIndex,
7363             T[] b, int bFromIndex, int bToIndex) {
7364         rangeCheck(a.length, aFromIndex, aToIndex);
7365         rangeCheck(b.length, bFromIndex, bToIndex);
7366 
7367         int aLength = aToIndex - aFromIndex;
7368         int bLength = bToIndex - bFromIndex;
7369         int length = Math.min(aLength, bLength);
7370         for (int i = 0; i < length; i++) {
7371             T oa = a[aFromIndex++];
7372             T ob = b[bFromIndex++];
7373             if (oa != ob) {
7374                 if (oa == null || ob == null)
7375                     return oa == null ? -1 : 1;
7376                 int v = oa.compareTo(ob);
7377                 if (v != 0) {
7378                     return v;
7379                 }
7380             }
7381         }
7382 
7383         return aLength - bLength;
7384     }
7385 
7386     /**
7387      * Compares two {@code Object} arrays lexicographically using a specified
7388      * comparator.
7389      *
7390      * <p>If the two arrays share a common prefix then the lexicographic
7391      * comparison is the result of comparing with the specified comparator two
7392      * elements at an index within the respective arrays that is the prefix
7393      * length.
7394      * Otherwise, one array is a proper prefix of the other and, lexicographic
7395      * comparison is the result of comparing the two array lengths.
7396      * (See {@link #mismatch(Object[], Object[])} for the definition of a common
7397      * and proper prefix.)
7398      *
7399      * <p>A {@code null} array reference is considered lexicographically less
7400      * than a non-{@code null} array reference.  Two {@code null} array
7401      * references are considered equal.
7402      *
7403      * @apiNote
7404      * <p>This method behaves as if (for non-{@code null} array references):
7405      * <pre>{@code
7406      *     int i = Arrays.mismatch(a, b, cmp);
7407      *     if (i >= 0 && i < Math.min(a.length, b.length))
7408      *         return cmp.compare(a[i], b[i]);
7409      *     return a.length - b.length;
7410      * }</pre>
7411      *
7412      * @param a the first array to compare
7413      * @param b the second array to compare
7414      * @param cmp the comparator to compare array elements
7415      * @param <T> the type of array elements
7416      * @return the value {@code 0} if the first and second array are equal and
7417      *         contain the same elements in the same order;
7418      *         a value less than {@code 0} if the first array is
7419      *         lexicographically less than the second array; and
7420      *         a value greater than {@code 0} if the first array is
7421      *         lexicographically greater than the second array
7422      * @throws NullPointerException if the comparator is {@code null}
7423      * @since 9
7424      */
7425     public static <T> int compare(T[] a, T[] b,
7426                                   Comparator<? super T> cmp) {
7427         Objects.requireNonNull(cmp);
7428         if (a == b)
7429             return 0;
7430         if (a == null || b == null)
7431             return a == null ? -1 : 1;
7432 
7433         int length = Math.min(a.length, b.length);
7434         for (int i = 0; i < length; i++) {
7435             T oa = a[i];
7436             T ob = b[i];
7437             if (oa != ob) {
7438                 // Null-value comparison is deferred to the comparator
7439                 int v = cmp.compare(oa, ob);
7440                 if (v != 0) {
7441                     return v;
7442                 }
7443             }
7444         }
7445 
7446         return a.length - b.length;
7447     }
7448 
7449     /**
7450      * Compares two {@code Object} arrays lexicographically over the specified
7451      * ranges.
7452      *
7453      * <p>If the two arrays, over the specified ranges, share a common prefix
7454      * then the lexicographic comparison is the result of comparing with the
7455      * specified comparator two elements at a relative index within the
7456      * respective arrays that is the prefix length.
7457      * Otherwise, one array is a proper prefix of the other and, lexicographic
7458      * comparison is the result of comparing the two range lengths.
7459      * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
7460      * definition of a common and proper prefix.)
7461      *
7462      * @apiNote
7463      * <p>This method behaves as if (for non-{@code null} array elements):
7464      * <pre>{@code
7465      *     int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7466      *                             b, bFromIndex, bToIndex, cmp);
7467      *     if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7468      *         return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
7469      *     return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7470      * }</pre>
7471      *
7472      * @param a the first array to compare
7473      * @param aFromIndex the index (inclusive) of the first element in the
7474      *                   first array to be compared
7475      * @param aToIndex the index (exclusive) of the last element in the
7476      *                 first array to be compared
7477      * @param b the second array to compare
7478      * @param bFromIndex the index (inclusive) of the first element in the
7479      *                   second array to be compared
7480      * @param bToIndex the index (exclusive) of the last element in the
7481      *                 second array to be compared
7482      * @param cmp the comparator to compare array elements
7483      * @param <T> the type of array elements
7484      * @return the value {@code 0} if, over the specified ranges, the first and
7485      *         second array are equal and contain the same elements in the same
7486      *         order;
7487      *         a value less than {@code 0} if, over the specified ranges, the
7488      *         first array is lexicographically less than the second array; and
7489      *         a value greater than {@code 0} if, over the specified ranges, the
7490      *         first array is lexicographically greater than the second array
7491      * @throws IllegalArgumentException
7492      *         if {@code aFromIndex > aToIndex} or
7493      *         if {@code bFromIndex > bToIndex}
7494      * @throws ArrayIndexOutOfBoundsException
7495      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7496      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7497      * @throws NullPointerException
7498      *         if either array or the comparator is {@code null}
7499      * @since 9
7500      */
7501     public static <T> int compare(
7502             T[] a, int aFromIndex, int aToIndex,
7503             T[] b, int bFromIndex, int bToIndex,
7504             Comparator<? super T> cmp) {
7505         Objects.requireNonNull(cmp);
7506         rangeCheck(a.length, aFromIndex, aToIndex);
7507         rangeCheck(b.length, bFromIndex, bToIndex);
7508 
7509         int aLength = aToIndex - aFromIndex;
7510         int bLength = bToIndex - bFromIndex;
7511         int length = Math.min(aLength, bLength);
7512         for (int i = 0; i < length; i++) {
7513             T oa = a[aFromIndex++];
7514             T ob = b[bFromIndex++];
7515             if (oa != ob) {
7516                 // Null-value comparison is deferred to the comparator
7517                 int v = cmp.compare(oa, ob);
7518                 if (v != 0) {
7519                     return v;
7520                 }
7521             }
7522         }
7523 
7524         return aLength - bLength;
7525     }
7526 
7527 
7528     // Mismatch methods
7529 
7530     // Mismatch boolean
7531 
7532     /**
7533      * Finds and returns the index of the first mismatch between two
7534      * {@code boolean} arrays, otherwise return -1 if no mismatch is found.  The
7535      * index will be in the range of 0 (inclusive) up to the length (inclusive)
7536      * of the smaller array.
7537      *
7538      * <p>If the two arrays share a common prefix then the returned index is the
7539      * length of the common prefix and it follows that there is a mismatch
7540      * between the two elements at that index within the respective arrays.
7541      * If one array is a proper prefix of the other then the returned index is
7542      * the length of the smaller array and it follows that the index is only
7543      * valid for the larger array.
7544      * Otherwise, there is no mismatch.
7545      *
7546      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7547      * prefix of length {@code pl} if the following expression is true:
7548      * <pre>{@code
7549      *     pl >= 0 &&
7550      *     pl < Math.min(a.length, b.length) &&
7551      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
7552      *     a[pl] != b[pl]
7553      * }</pre>
7554      * Note that a common prefix length of {@code 0} indicates that the first
7555      * elements from each array mismatch.
7556      *
7557      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7558      * prefix if the following expression is true:
7559      * <pre>{@code
7560      *     a.length != b.length &&
7561      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
7562      *                   b, 0, Math.min(a.length, b.length))
7563      * }</pre>
7564      *
7565      * @param a the first array to be tested for a mismatch
7566      * @param b the second array to be tested for a mismatch
7567      * @return the index of the first mismatch between the two arrays,
7568      *         otherwise {@code -1}.
7569      * @throws NullPointerException
7570      *         if either array is {@code null}
7571      * @since 9
7572      */
7573     public static int mismatch(boolean[] a, boolean[] b) {
7574         int length = Math.min(a.length, b.length); // Check null array refs
7575         if (a == b)
7576             return -1;
7577 
7578         for (int i = 0; i < length; i++) {
7579             if (a[i] != b[i]) return i;
7580         }
7581 
7582         return a.length != b.length ? length : -1;
7583     }
7584 
7585     /**
7586      * Finds and returns the relative index of the first mismatch between two
7587      * {@code boolean} arrays over the specified ranges, otherwise return -1 if
7588      * no mismatch is found.  The index will be in the range of 0 (inclusive) up
7589      * to the length (inclusive) of the smaller range.
7590      *
7591      * <p>If the two arrays, over the specified ranges, share a common prefix
7592      * then the returned relative index is the length of the common prefix and
7593      * it follows that there is a mismatch between the two elements at that
7594      * relative index within the respective arrays.
7595      * If one array is a proper prefix of the other, over the specified ranges,
7596      * then the returned relative index is the length of the smaller range and
7597      * it follows that the relative index is only valid for the array with the
7598      * larger range.
7599      * Otherwise, there is no mismatch.
7600      *
7601      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7602      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7603      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7604      * prefix of length {@code pl} if the following expression is true:
7605      * <pre>{@code
7606      *     pl >= 0 &&
7607      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7608      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7609      *     a[aFromIndex + pl] != b[bFromIndex + pl]
7610      * }</pre>
7611      * Note that a common prefix length of {@code 0} indicates that the first
7612      * elements from each array mismatch.
7613      *
7614      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7615      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7616      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7617      * if the following expression is true:
7618      * <pre>{@code
7619      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7620      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7621      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7622      * }</pre>
7623      *
7624      * @param a the first array to be tested for a mismatch
7625      * @param aFromIndex the index (inclusive) of the first element in the
7626      *                   first array to be tested
7627      * @param aToIndex the index (exclusive) of the last element in the
7628      *                 first array to be tested
7629      * @param b the second array to be tested for a mismatch
7630      * @param bFromIndex the index (inclusive) of the first element in the
7631      *                   second array to be tested
7632      * @param bToIndex the index (exclusive) of the last element in the
7633      *                 second array to be tested
7634      * @return the relative index of the first mismatch between the two arrays
7635      *         over the specified ranges, otherwise {@code -1}.
7636      * @throws IllegalArgumentException
7637      *         if {@code aFromIndex > aToIndex} or
7638      *         if {@code bFromIndex > bToIndex}
7639      * @throws ArrayIndexOutOfBoundsException
7640      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7641      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7642      * @throws NullPointerException
7643      *         if either array is {@code null}
7644      * @since 9
7645      */
7646     public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
7647                                boolean[] b, int bFromIndex, int bToIndex) {
7648         rangeCheck(a.length, aFromIndex, aToIndex);
7649         rangeCheck(b.length, bFromIndex, bToIndex);
7650 
7651         int aLength = aToIndex - aFromIndex;
7652         int bLength = bToIndex - bFromIndex;
7653         int length = Math.min(aLength, bLength);
7654         for (int i = 0; i < length; i++) {
7655             if (a[aFromIndex++] != b[bFromIndex++]) return i;
7656         }
7657 
7658         return aLength != bLength ? length : -1;
7659     }
7660 
7661     // Mismatch byte
7662 
7663     /**
7664      * Finds and returns the index of the first mismatch between two {@code byte}
7665      * arrays, otherwise return -1 if no mismatch is found.  The index will be
7666      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7667      * array.
7668      *
7669      * <p>If the two arrays share a common prefix then the returned index is the
7670      * length of the common prefix and it follows that there is a mismatch
7671      * between the two elements at that index within the respective arrays.
7672      * If one array is a proper prefix of the other then the returned index is
7673      * the length of the smaller array and it follows that the index is only
7674      * valid for the larger array.
7675      * Otherwise, there is no mismatch.
7676      *
7677      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7678      * prefix of length {@code pl} if the following expression is true:
7679      * <pre>{@code
7680      *     pl >= 0 &&
7681      *     pl < Math.min(a.length, b.length) &&
7682      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
7683      *     a[pl] != b[pl]
7684      * }</pre>
7685      * Note that a common prefix length of {@code 0} indicates that the first
7686      * elements from each array mismatch.
7687      *
7688      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7689      * prefix if the following expression is true:
7690      * <pre>{@code
7691      *     a.length != b.length &&
7692      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
7693      *                   b, 0, Math.min(a.length, b.length))
7694      * }</pre>
7695      *
7696      * @param a the first array to be tested for a mismatch
7697      * @param b the second array to be tested for a mismatch
7698      * @return the index of the first mismatch between the two arrays,
7699      *         otherwise {@code -1}.
7700      * @throws NullPointerException
7701      *         if either array is {@code null}
7702      * @since 9
7703      */
7704     public static int mismatch(byte[] a, byte[] b) {
7705         int length = Math.min(a.length, b.length); // Check null array refs
7706         if (a == b)
7707             return -1;
7708 
7709         for (int i = 0; i < length; i++) {
7710             if (a[i] != b[i]) return i;
7711         }
7712 
7713         return a.length != b.length ? length : -1;
7714     }
7715 
7716     /**
7717      * Finds and returns the relative index of the first mismatch between two
7718      * {@code byte} arrays over the specified ranges, otherwise return -1 if no
7719      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
7720      * the length (inclusive) of the smaller range.
7721      *
7722      * <p>If the two arrays, over the specified ranges, share a common prefix
7723      * then the returned relative index is the length of the common prefix and
7724      * it follows that there is a mismatch between the two elements at that
7725      * relative index within the respective arrays.
7726      * If one array is a proper prefix of the other, over the specified ranges,
7727      * then the returned relative index is the length of the smaller range and
7728      * it follows that the relative index is only valid for the array with the
7729      * larger range.
7730      * Otherwise, there is no mismatch.
7731      *
7732      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7733      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7734      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7735      * prefix of length {@code pl} if the following expression is true:
7736      * <pre>{@code
7737      *     pl >= 0 &&
7738      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7739      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7740      *     a[aFromIndex + pl] != b[bFromIndex + pl]
7741      * }</pre>
7742      * Note that a common prefix length of {@code 0} indicates that the first
7743      * elements from each array mismatch.
7744      *
7745      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7746      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7747      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7748      * if the following expression is true:
7749      * <pre>{@code
7750      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7751      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7752      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7753      * }</pre>
7754      *
7755      * @param a the first array to be tested for a mismatch
7756      * @param aFromIndex the index (inclusive) of the first element in the
7757      *                   first array to be tested
7758      * @param aToIndex the index (exclusive) of the last element in the
7759      *                 first array to be tested
7760      * @param b the second array to be tested for a mismatch
7761      * @param bFromIndex the index (inclusive) of the first element in the
7762      *                   second array to be tested
7763      * @param bToIndex the index (exclusive) of the last element in the
7764      *                 second array to be tested
7765      * @return the relative index of the first mismatch between the two arrays
7766      *         over the specified ranges, otherwise {@code -1}.
7767      * @throws IllegalArgumentException
7768      *         if {@code aFromIndex > aToIndex} or
7769      *         if {@code bFromIndex > bToIndex}
7770      * @throws ArrayIndexOutOfBoundsException
7771      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7772      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7773      * @throws NullPointerException
7774      *         if either array is {@code null}
7775      * @since 9
7776      */
7777     public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
7778                                byte[] b, int bFromIndex, int bToIndex) {
7779         rangeCheck(a.length, aFromIndex, aToIndex);
7780         rangeCheck(b.length, bFromIndex, bToIndex);
7781 
7782         int aLength = aToIndex - aFromIndex;
7783         int bLength = bToIndex - bFromIndex;
7784         int length = Math.min(aLength, bLength);
7785         for (int i = 0; i < length; i++) {
7786             if (a[aFromIndex++] != b[bFromIndex++]) return i;
7787         }
7788 
7789         return aLength != bLength ? length : -1;
7790     }
7791 
7792     // Mismatch char
7793 
7794     /**
7795      * Finds and returns the index of the first mismatch between two {@code char}
7796      * arrays, otherwise return -1 if no mismatch is found.  The index will be
7797      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7798      * array.
7799      *
7800      * <p>If the two arrays share a common prefix then the returned index is the
7801      * length of the common prefix and it follows that there is a mismatch
7802      * between the two elements at that index within the respective arrays.
7803      * If one array is a proper prefix of the other then the returned index is
7804      * the length of the smaller array and it follows that the index is only
7805      * valid for the larger array.
7806      * Otherwise, there is no mismatch.
7807      *
7808      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7809      * prefix of length {@code pl} if the following expression is true:
7810      * <pre>{@code
7811      *     pl >= 0 &&
7812      *     pl < Math.min(a.length, b.length) &&
7813      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
7814      *     a[pl] != b[pl]
7815      * }</pre>
7816      * Note that a common prefix length of {@code 0} indicates that the first
7817      * elements from each array mismatch.
7818      *
7819      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7820      * prefix if the following expression is true:
7821      * <pre>{@code
7822      *     a.length != b.length &&
7823      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
7824      *                   b, 0, Math.min(a.length, b.length))
7825      * }</pre>
7826      *
7827      * @param a the first array to be tested for a mismatch
7828      * @param b the second array to be tested for a mismatch
7829      * @return the index of the first mismatch between the two arrays,
7830      *         otherwise {@code -1}.
7831      * @throws NullPointerException
7832      *         if either array is {@code null}
7833      * @since 9
7834      */
7835     public static int mismatch(char[] a, char[] b) {
7836         int length = Math.min(a.length, b.length); // Check null array refs
7837         if (a == b)
7838             return -1;
7839 
7840         for (int i = 0; i < length; i++) {
7841             if (a[i] != b[i]) return i;
7842         }
7843 
7844         return a.length != b.length ? length : -1;
7845     }
7846 
7847     /**
7848      * Finds and returns the relative index of the first mismatch between two
7849      * {@code char} arrays over the specified ranges, otherwise return -1 if no
7850      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
7851      * the length (inclusive) of the smaller range.
7852      *
7853      * <p>If the two arrays, over the specified ranges, share a common prefix
7854      * then the returned relative index is the length of the common prefix and
7855      * it follows that there is a mismatch between the two elements at that
7856      * relative index within the respective arrays.
7857      * If one array is a proper prefix of the other, over the specified ranges,
7858      * then the returned relative index is the length of the smaller range and
7859      * it follows that the relative index is only valid for the array with the
7860      * larger range.
7861      * Otherwise, there is no mismatch.
7862      *
7863      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7864      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7865      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7866      * prefix of length {@code pl} if the following expression is true:
7867      * <pre>{@code
7868      *     pl >= 0 &&
7869      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7870      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7871      *     a[aFromIndex + pl] != b[bFromIndex + pl]
7872      * }</pre>
7873      * Note that a common prefix length of {@code 0} indicates that the first
7874      * elements from each array mismatch.
7875      *
7876      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7877      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7878      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7879      * if the following expression is true:
7880      * <pre>{@code
7881      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7882      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7883      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7884      * }</pre>
7885      *
7886      * @param a the first array to be tested for a mismatch
7887      * @param aFromIndex the index (inclusive) of the first element in the
7888      *                   first array to be tested
7889      * @param aToIndex the index (exclusive) of the last element in the
7890      *                 first array to be tested
7891      * @param b the second array to be tested for a mismatch
7892      * @param bFromIndex the index (inclusive) of the first element in the
7893      *                   second array to be tested
7894      * @param bToIndex the index (exclusive) of the last element in the
7895      *                 second array to be tested
7896      * @return the relative index of the first mismatch between the two arrays
7897      *         over the specified ranges, otherwise {@code -1}.
7898      * @throws IllegalArgumentException
7899      *         if {@code aFromIndex > aToIndex} or
7900      *         if {@code bFromIndex > bToIndex}
7901      * @throws ArrayIndexOutOfBoundsException
7902      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
7903      *         if {@code bFromIndex < 0 or bToIndex > b.length}
7904      * @throws NullPointerException
7905      *         if either array is {@code null}
7906      * @since 9
7907      */
7908     public static int mismatch(char[] a, int aFromIndex, int aToIndex,
7909                                char[] b, int bFromIndex, int bToIndex) {
7910         rangeCheck(a.length, aFromIndex, aToIndex);
7911         rangeCheck(b.length, bFromIndex, bToIndex);
7912 
7913         int aLength = aToIndex - aFromIndex;
7914         int bLength = bToIndex - bFromIndex;
7915         int length = Math.min(aLength, bLength);
7916         for (int i = 0; i < length; i++) {
7917             if (a[aFromIndex++] != b[bFromIndex++]) return i;
7918         }
7919 
7920         return aLength != bLength ? length : -1;
7921     }
7922 
7923     // Mismatch short
7924 
7925     /**
7926      * Finds and returns the index of the first mismatch between two {@code short}
7927      * arrays, otherwise return -1 if no mismatch is found.  The index will be
7928      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7929      * array.
7930      *
7931      * <p>If the two arrays share a common prefix then the returned index is the
7932      * length of the common prefix and it follows that there is a mismatch
7933      * between the two elements at that index within the respective arrays.
7934      * If one array is a proper prefix of the other then the returned index is
7935      * the length of the smaller array and it follows that the index is only
7936      * valid for the larger array.
7937      * Otherwise, there is no mismatch.
7938      *
7939      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7940      * prefix of length {@code pl} if the following expression is true:
7941      * <pre>{@code
7942      *     pl >= 0 &&
7943      *     pl < Math.min(a.length, b.length) &&
7944      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
7945      *     a[pl] != b[pl]
7946      * }</pre>
7947      * Note that a common prefix length of {@code 0} indicates that the first
7948      * elements from each array mismatch.
7949      *
7950      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7951      * prefix if the following expression is true:
7952      * <pre>{@code
7953      *     a.length != b.length &&
7954      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
7955      *                   b, 0, Math.min(a.length, b.length))
7956      * }</pre>
7957      *
7958      * @param a the first array to be tested for a mismatch
7959      * @param b the second array to be tested for a mismatch
7960      * @return the index of the first mismatch between the two arrays,
7961      *         otherwise {@code -1}.
7962      * @throws NullPointerException
7963      *         if either array is {@code null}
7964      * @since 9
7965      */
7966     public static int mismatch(short[] a, short[] b) {
7967         int length = Math.min(a.length, b.length); // Check null array refs
7968         if (a == b)
7969             return -1;
7970 
7971         for (int i = 0; i < length; i++) {
7972             if (a[i] != b[i]) return i;
7973         }
7974 
7975         return a.length != b.length ? length : -1;
7976     }
7977 
7978     /**
7979      * Finds and returns the relative index of the first mismatch between two
7980      * {@code short} arrays over the specified ranges, otherwise return -1 if no
7981      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
7982      * the length (inclusive) of the smaller range.
7983      *
7984      * <p>If the two arrays, over the specified ranges, share a common prefix
7985      * then the returned relative index is the length of the common prefix and
7986      * it follows that there is a mismatch between the two elements at that
7987      * relative index within the respective arrays.
7988      * If one array is a proper prefix of the other, over the specified ranges,
7989      * then the returned relative index is the length of the smaller range and
7990      * it follows that the relative index is only valid for the array with the
7991      * larger range.
7992      * Otherwise, there is no mismatch.
7993      *
7994      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7995      * ranges [{@code aFromIndex}, {@code atoIndex}) and
7996      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7997      * prefix of length {@code pl} if the following expression is true:
7998      * <pre>{@code
7999      *     pl >= 0 &&
8000      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8001      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8002      *     a[aFromIndex + pl] != b[bFromIndex + pl]
8003      * }</pre>
8004      * Note that a common prefix length of {@code 0} indicates that the first
8005      * elements from each array mismatch.
8006      *
8007      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8008      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8009      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8010      * if the following expression is true:
8011      * <pre>{@code
8012      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8013      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8014      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8015      * }</pre>
8016      *
8017      * @param a the first array to be tested for a mismatch
8018      * @param aFromIndex the index (inclusive) of the first element in the
8019      *                   first array to be tested
8020      * @param aToIndex the index (exclusive) of the last element in the
8021      *                 first array to be tested
8022      * @param b the second array to be tested for a mismatch
8023      * @param bFromIndex the index (inclusive) of the first element in the
8024      *                   second array to be tested
8025      * @param bToIndex the index (exclusive) of the last element in the
8026      *                 second array to be tested
8027      * @return the relative index of the first mismatch between the two arrays
8028      *         over the specified ranges, otherwise {@code -1}.
8029      * @throws IllegalArgumentException
8030      *         if {@code aFromIndex > aToIndex} or
8031      *         if {@code bFromIndex > bToIndex}
8032      * @throws ArrayIndexOutOfBoundsException
8033      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8034      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8035      * @throws NullPointerException
8036      *         if either array is {@code null}
8037      * @since 9
8038      */
8039     public static int mismatch(short[] a, int aFromIndex, int aToIndex,
8040                                short[] b, int bFromIndex, int bToIndex) {
8041         rangeCheck(a.length, aFromIndex, aToIndex);
8042         rangeCheck(b.length, bFromIndex, bToIndex);
8043 
8044         int aLength = aToIndex - aFromIndex;
8045         int bLength = bToIndex - bFromIndex;
8046         int length = Math.min(aLength, bLength);
8047         for (int i = 0; i < length; i++) {
8048             if (a[aFromIndex++] != b[bFromIndex++]) return i;
8049         }
8050 
8051         return aLength != bLength ? length : -1;
8052     }
8053 
8054     // Mismatch int
8055 
8056     /**
8057      * Finds and returns the index of the first mismatch between two {@code int}
8058      * arrays, otherwise return -1 if no mismatch is found.  The index will be
8059      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8060      * array.
8061      *
8062      * <p>If the two arrays share a common prefix then the returned index is the
8063      * length of the common prefix and it follows that there is a mismatch
8064      * between the two elements at that index within the respective arrays.
8065      * If one array is a proper prefix of the other then the returned index is
8066      * the length of the smaller array and it follows that the index is only
8067      * valid for the larger array.
8068      * Otherwise, there is no mismatch.
8069      *
8070      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8071      * prefix of length {@code pl} if the following expression is true:
8072      * <pre>{@code
8073      *     pl >= 0 &&
8074      *     pl < Math.min(a.length, b.length) &&
8075      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
8076      *     a[pl] != b[pl]
8077      * }</pre>
8078      * Note that a common prefix length of {@code 0} indicates that the first
8079      * elements from each array mismatch.
8080      *
8081      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8082      * prefix if the following expression is true:
8083      * <pre>{@code
8084      *     a.length != b.length &&
8085      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8086      *                   b, 0, Math.min(a.length, b.length))
8087      * }</pre>
8088      *
8089      * @param a the first array to be tested for a mismatch
8090      * @param b the second array to be tested for a mismatch
8091      * @return the index of the first mismatch between the two arrays,
8092      *         otherwise {@code -1}.
8093      * @throws NullPointerException
8094      *         if either array is {@code null}
8095      * @since 9
8096      */
8097     public static int mismatch(int[] a, int[] b) {
8098         int length = Math.min(a.length, b.length); // Check null array refs
8099         if (a == b)
8100             return -1;
8101 
8102         for (int i = 0; i < length; i++) {
8103             if (a[i] != b[i]) return i;
8104         }
8105 
8106         return a.length != b.length ? length : -1;
8107     }
8108 
8109     /**
8110      * Finds and returns the relative index of the first mismatch between two
8111      * {@code int} arrays over the specified ranges, otherwise return -1 if no
8112      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
8113      * the length (inclusive) of the smaller range.
8114      *
8115      * <p>If the two arrays, over the specified ranges, share a common prefix
8116      * then the returned relative index is the length of the common prefix and
8117      * it follows that there is a mismatch between the two elements at that
8118      * relative index within the respective arrays.
8119      * If one array is a proper prefix of the other, over the specified ranges,
8120      * then the returned relative index is the length of the smaller range and
8121      * it follows that the relative index is only valid for the array with the
8122      * larger range.
8123      * Otherwise, there is no mismatch.
8124      *
8125      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8126      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8127      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8128      * prefix of length {@code pl} if the following expression is true:
8129      * <pre>{@code
8130      *     pl >= 0 &&
8131      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8132      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8133      *     a[aFromIndex + pl] != b[bFromIndex + pl]
8134      * }</pre>
8135      * Note that a common prefix length of {@code 0} indicates that the first
8136      * elements from each array mismatch.
8137      *
8138      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8139      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8140      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8141      * if the following expression is true:
8142      * <pre>{@code
8143      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8144      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8145      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8146      * }</pre>
8147      *
8148      * @param a the first array to be tested for a mismatch
8149      * @param aFromIndex the index (inclusive) of the first element in the
8150      *                   first array to be tested
8151      * @param aToIndex the index (exclusive) of the last element in the
8152      *                 first array to be tested
8153      * @param b the second array to be tested for a mismatch
8154      * @param bFromIndex the index (inclusive) of the first element in the
8155      *                   second array to be tested
8156      * @param bToIndex the index (exclusive) of the last element in the
8157      *                 second array to be tested
8158      * @return the relative index of the first mismatch between the two arrays
8159      *         over the specified ranges, otherwise {@code -1}.
8160      * @throws IllegalArgumentException
8161      *         if {@code aFromIndex > aToIndex} or
8162      *         if {@code bFromIndex > bToIndex}
8163      * @throws ArrayIndexOutOfBoundsException
8164      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8165      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8166      * @throws NullPointerException
8167      *         if either array is {@code null}
8168      * @since 9
8169      */
8170     public static int mismatch(int[] a, int aFromIndex, int aToIndex,
8171                                int[] b, int bFromIndex, int bToIndex) {
8172         rangeCheck(a.length, aFromIndex, aToIndex);
8173         rangeCheck(b.length, bFromIndex, bToIndex);
8174 
8175         int aLength = aToIndex - aFromIndex;
8176         int bLength = bToIndex - bFromIndex;
8177         int length = Math.min(aLength, bLength);
8178         for (int i = 0; i < length; i++) {
8179             if (a[aFromIndex++] != b[bFromIndex++]) return i;
8180         }
8181 
8182         return aLength != bLength ? length : -1;
8183     }
8184 
8185     // Mismatch long
8186 
8187     /**
8188      * Finds and returns the index of the first mismatch between two {@code long}
8189      * arrays, otherwise return -1 if no mismatch is found.  The index will be
8190      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8191      * array.
8192      *
8193      * <p>If the two arrays share a common prefix then the returned index is the
8194      * length of the common prefix and it follows that there is a mismatch
8195      * between the two elements at that index within the respective arrays.
8196      * If one array is a proper prefix of the other then the returned index is
8197      * the length of the smaller array and it follows that the index is only
8198      * valid for the larger array.
8199      * Otherwise, there is no mismatch.
8200      *
8201      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8202      * prefix of length {@code pl} if the following expression is true:
8203      * <pre>{@code
8204      *     pl >= 0 &&
8205      *     pl < Math.min(a.length, b.length) &&
8206      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
8207      *     a[pl] != b[pl]
8208      * }</pre>
8209      * Note that a common prefix length of {@code 0} indicates that the first
8210      * elements from each array mismatch.
8211      *
8212      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8213      * prefix if the following expression is true:
8214      * <pre>{@code
8215      *     a.length != b.length &&
8216      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8217      *                   b, 0, Math.min(a.length, b.length))
8218      * }</pre>
8219      *
8220      * @param a the first array to be tested for a mismatch
8221      * @param b the second array to be tested for a mismatch
8222      * @return the index of the first mismatch between the two arrays,
8223      *         otherwise {@code -1}.
8224      * @throws NullPointerException
8225      *         if either array is {@code null}
8226      * @since 9
8227      */
8228     public static int mismatch(long[] a, long[] b) {
8229         int length = Math.min(a.length, b.length); // Check null array refs
8230         if (a == b)
8231             return -1;
8232 
8233         for (int i = 0; i < length; i++) {
8234             if (a[i] != b[i]) return i;
8235         }
8236 
8237         return a.length != b.length ? length : -1;
8238     }
8239 
8240     /**
8241      * Finds and returns the relative index of the first mismatch between two
8242      * {@code long} arrays over the specified ranges, otherwise return -1 if no
8243      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
8244      * the length (inclusive) of the smaller range.
8245      *
8246      * <p>If the two arrays, over the specified ranges, share a common prefix
8247      * then the returned relative index is the length of the common prefix and
8248      * it follows that there is a mismatch between the two elements at that
8249      * relative index within the respective arrays.
8250      * If one array is a proper prefix of the other, over the specified ranges,
8251      * then the returned relative index is the length of the smaller range and
8252      * it follows that the relative index is only valid for the array with the
8253      * larger range.
8254      * Otherwise, there is no mismatch.
8255      *
8256      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8257      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8258      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8259      * prefix of length {@code pl} if the following expression is true:
8260      * <pre>{@code
8261      *     pl >= 0 &&
8262      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8263      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8264      *     a[aFromIndex + pl] != b[bFromIndex + pl]
8265      * }</pre>
8266      * Note that a common prefix length of {@code 0} indicates that the first
8267      * elements from each array mismatch.
8268      *
8269      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8270      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8271      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8272      * if the following expression is true:
8273      * <pre>{@code
8274      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8275      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8276      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8277      * }</pre>
8278      *
8279      * @param a the first array to be tested for a mismatch
8280      * @param aFromIndex the index (inclusive) of the first element in the
8281      *                   first array to be tested
8282      * @param aToIndex the index (exclusive) of the last element in the
8283      *                 first array to be tested
8284      * @param b the second array to be tested for a mismatch
8285      * @param bFromIndex the index (inclusive) of the first element in the
8286      *                   second array to be tested
8287      * @param bToIndex the index (exclusive) of the last element in the
8288      *                 second array to be tested
8289      * @return the relative index of the first mismatch between the two arrays
8290      *         over the specified ranges, otherwise {@code -1}.
8291      * @throws IllegalArgumentException
8292      *         if {@code aFromIndex > aToIndex} or
8293      *         if {@code bFromIndex > bToIndex}
8294      * @throws ArrayIndexOutOfBoundsException
8295      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8296      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8297      * @throws NullPointerException
8298      *         if either array is {@code null}
8299      * @since 9
8300      */
8301     public static int mismatch(long[] a, int aFromIndex, int aToIndex,
8302                                long[] b, int bFromIndex, int bToIndex) {
8303         rangeCheck(a.length, aFromIndex, aToIndex);
8304         rangeCheck(b.length, bFromIndex, bToIndex);
8305 
8306         int aLength = aToIndex - aFromIndex;
8307         int bLength = bToIndex - bFromIndex;
8308         int length = Math.min(aLength, bLength);
8309         for (int i = 0; i < length; i++) {
8310             if (a[aFromIndex++] != b[bFromIndex++]) return i;
8311         }
8312 
8313         return aLength != bLength ? length : -1;
8314     }
8315 
8316     // Mismatch float
8317 
8318     /**
8319      * Finds and returns the index of the first mismatch between two {@code float}
8320      * arrays, otherwise return -1 if no mismatch is found.  The index will be
8321      * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8322      * array.
8323      *
8324      * <p>If the two arrays share a common prefix then the returned index is the
8325      * length of the common prefix and it follows that there is a mismatch
8326      * between the two elements at that index within the respective arrays.
8327      * If one array is a proper prefix of the other then the returned index is
8328      * the length of the smaller array and it follows that the index is only
8329      * valid for the larger array.
8330      * Otherwise, there is no mismatch.
8331      *
8332      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8333      * prefix of length {@code pl} if the following expression is true:
8334      * <pre>{@code
8335      *     pl >= 0 &&
8336      *     pl < Math.min(a.length, b.length) &&
8337      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
8338      *     Float.compare(a[pl], b[pl]) != 0
8339      * }</pre>
8340      * Note that a common prefix length of {@code 0} indicates that the first
8341      * elements from each array mismatch.
8342      *
8343      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8344      * prefix if the following expression is true:
8345      * <pre>{@code
8346      *     a.length != b.length &&
8347      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8348      *                   b, 0, Math.min(a.length, b.length))
8349      * }</pre>
8350      *
8351      * @param a the first array to be tested for a mismatch
8352      * @param b the second array to be tested for a mismatch
8353      * @return the index of the first mismatch between the two arrays,
8354      *         otherwise {@code -1}.
8355      * @throws NullPointerException
8356      *         if either array is {@code null}
8357      * @since 9
8358      */
8359     public static int mismatch(float[] a, float[] b) {
8360         int length = Math.min(a.length, b.length); // Check null array refs
8361         if (a == b)
8362             return -1;
8363 
8364         for (int i = 0; i < length; i++) {
8365             float va = a[i], vb = b[i];
8366             if (Float.floatToRawIntBits(va) != Float.floatToRawIntBits(vb))
8367                 if (!Float.isNaN(va) || !Float.isNaN(vb))
8368                     return i;
8369         }
8370 
8371         return a.length != b.length ? length : -1;
8372     }
8373 
8374     /**
8375      * Finds and returns the relative index of the first mismatch between two
8376      * {@code float} arrays over the specified ranges, otherwise return -1 if no
8377      * mismatch is found.  The index will be in the range of 0 (inclusive) up to
8378      * the length (inclusive) of the smaller range.
8379      *
8380      * <p>If the two arrays, over the specified ranges, share a common prefix
8381      * then the returned relative index is the length of the common prefix and
8382      * it follows that there is a mismatch between the two elements at that
8383      * relative index within the respective arrays.
8384      * If one array is a proper prefix of the other, over the specified ranges,
8385      * then the returned relative index is the length of the smaller range and
8386      * it follows that the relative index is only valid for the array with the
8387      * larger range.
8388      * Otherwise, there is no mismatch.
8389      *
8390      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8391      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8392      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8393      * prefix of length {@code pl} if the following expression is true:
8394      * <pre>{@code
8395      *     pl >= 0 &&
8396      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8397      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8398      *     Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8399      * }</pre>
8400      * Note that a common prefix length of {@code 0} indicates that the first
8401      * elements from each array mismatch.
8402      *
8403      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8404      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8405      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8406      * if the following expression is true:
8407      * <pre>{@code
8408      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8409      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8410      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8411      * }</pre>
8412      *
8413      * @param a the first array to be tested for a mismatch
8414      * @param aFromIndex the index (inclusive) of the first element in the
8415      *                   first array to be tested
8416      * @param aToIndex the index (exclusive) of the last element in the
8417      *                 first array to be tested
8418      * @param b the second array to be tested for a mismatch
8419      * @param bFromIndex the index (inclusive) of the first element in the
8420      *                   second array to be tested
8421      * @param bToIndex the index (exclusive) of the last element in the
8422      *                 second array to be tested
8423      * @return the relative index of the first mismatch between the two arrays
8424      *         over the specified ranges, otherwise {@code -1}.
8425      * @throws IllegalArgumentException
8426      *         if {@code aFromIndex > aToIndex} or
8427      *         if {@code bFromIndex > bToIndex}
8428      * @throws ArrayIndexOutOfBoundsException
8429      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8430      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8431      * @throws NullPointerException
8432      *         if either array is {@code null}
8433      * @since 9
8434      */
8435     public static int mismatch(float[] a, int aFromIndex, int aToIndex,
8436                                float[] b, int bFromIndex, int bToIndex) {
8437         rangeCheck(a.length, aFromIndex, aToIndex);
8438         rangeCheck(b.length, bFromIndex, bToIndex);
8439 
8440         int aLength = aToIndex - aFromIndex;
8441         int bLength = bToIndex - bFromIndex;
8442         int length = Math.min(aLength, bLength);
8443         for (int i = 0; i < length; i++) {
8444             float va = a[aFromIndex++], vb = b[bFromIndex++];
8445             if (Float.floatToRawIntBits(va) != Float.floatToRawIntBits(vb))
8446                 if (!Float.isNaN(va) || !Float.isNaN(vb))
8447                     return i;
8448         }
8449 
8450         return aLength != bLength ? length : -1;
8451     }
8452 
8453     // Mismatch double
8454 
8455     /**
8456      * Finds and returns the index of the first mismatch between two
8457      * {@code double} arrays, otherwise return -1 if no mismatch is found.  The
8458      * index will be in the range of 0 (inclusive) up to the length (inclusive)
8459      * of the smaller array.
8460      *
8461      * <p>If the two arrays share a common prefix then the returned index is the
8462      * length of the common prefix and it follows that there is a mismatch
8463      * between the two elements at that index within the respective arrays.
8464      * If one array is a proper prefix of the other then the returned index is
8465      * the length of the smaller array and it follows that the index is only
8466      * valid for the larger array.
8467      * Otherwise, there is no mismatch.
8468      *
8469      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8470      * prefix of length {@code pl} if the following expression is true:
8471      * <pre>{@code
8472      *     pl >= 0 &&
8473      *     pl < Math.min(a.length, b.length) &&
8474      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
8475      *     Double.compare(a[pl], b[pl]) != 0
8476      * }</pre>
8477      * Note that a common prefix length of {@code 0} indicates that the first
8478      * elements from each array mismatch.
8479      *
8480      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8481      * prefix if the following expression is true:
8482      * <pre>{@code
8483      *     a.length != b.length &&
8484      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8485      *                   b, 0, Math.min(a.length, b.length))
8486      * }</pre>
8487      *
8488      * @param a the first array to be tested for a mismatch
8489      * @param b the second array to be tested for a mismatch
8490      * @return the index of the first mismatch between the two arrays,
8491      *         otherwise {@code -1}.
8492      * @throws NullPointerException
8493      *         if either array is {@code null}
8494      * @since 9
8495      */
8496     public static int mismatch(double[] a, double[] b) {
8497         int length = Math.min(a.length, b.length); // Check null array refs
8498         if (a == b)
8499             return -1;
8500 
8501         for (int i = 0; i < length; i++) {
8502             double va = a[i], vb = b[i];
8503             if (Double.doubleToRawLongBits(va) != Double.doubleToRawLongBits(vb))
8504                 if (!Double.isNaN(va) || !Double.isNaN(vb))
8505                     return i;
8506         }
8507 
8508         return a.length != b.length ? length : -1;
8509     }
8510 
8511     /**
8512      * Finds and returns the relative index of the first mismatch between two
8513      * {@code double} arrays over the specified ranges, otherwise return -1 if
8514      * no mismatch is found.  The index will be in the range of 0 (inclusive) up
8515      * to the length (inclusive) of the smaller range.
8516      *
8517      * <p>If the two arrays, over the specified ranges, share a common prefix
8518      * then the returned relative index is the length of the common prefix and
8519      * it follows that there is a mismatch between the two elements at that
8520      * relative index within the respective arrays.
8521      * If one array is a proper prefix of the other, over the specified ranges,
8522      * then the returned relative index is the length of the smaller range and
8523      * it follows that the relative index is only valid for the array with the
8524      * larger range.
8525      * Otherwise, there is no mismatch.
8526      *
8527      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8528      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8529      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8530      * prefix of length {@code pl} if the following expression is true:
8531      * <pre>{@code
8532      *     pl >= 0 &&
8533      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8534      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8535      *     Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8536      * }</pre>
8537      * Note that a common prefix length of {@code 0} indicates that the first
8538      * elements from each array mismatch.
8539      *
8540      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8541      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8542      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8543      * if the following expression is true:
8544      * <pre>{@code
8545      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8546      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8547      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8548      * }</pre>
8549      *
8550      * @param a the first array to be tested for a mismatch
8551      * @param aFromIndex the index (inclusive) of the first element in the
8552      *                   first array to be tested
8553      * @param aToIndex the index (exclusive) of the last element in the
8554      *                 first array to be tested
8555      * @param b the second array to be tested for a mismatch
8556      * @param bFromIndex the index (inclusive) of the first element in the
8557      *                   second array to be tested
8558      * @param bToIndex the index (exclusive) of the last element in the
8559      *                 second array to be tested
8560      * @return the relative index of the first mismatch between the two arrays
8561      *         over the specified ranges, otherwise {@code -1}.
8562      * @throws IllegalArgumentException
8563      *         if {@code aFromIndex > aToIndex} or
8564      *         if {@code bFromIndex > bToIndex}
8565      * @throws ArrayIndexOutOfBoundsException
8566      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8567      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8568      * @throws NullPointerException
8569      *         if either array is {@code null}
8570      * @since 9
8571      */
8572     public static int mismatch(double[] a, int aFromIndex, int aToIndex,
8573                                double[] b, int bFromIndex, int bToIndex) {
8574         rangeCheck(a.length, aFromIndex, aToIndex);
8575         rangeCheck(b.length, bFromIndex, bToIndex);
8576 
8577         int aLength = aToIndex - aFromIndex;
8578         int bLength = bToIndex - bFromIndex;
8579         int length = Math.min(aLength, bLength);
8580         for (int i = 0; i < length; i++) {
8581             double va = a[aFromIndex++], vb = b[bFromIndex++];
8582             if (Double.doubleToRawLongBits(va) != Double.doubleToRawLongBits(vb))
8583                 if (!Double.isNaN(va) || !Double.isNaN(vb))
8584                     return i;
8585         }
8586 
8587         return aLength != bLength ? length : -1;
8588     }
8589 
8590     // Mismatch objects
8591 
8592     /**
8593      * Finds and returns the index of the first mismatch between two
8594      * {@code Object} arrays, otherwise return -1 if no mismatch is found.  The
8595      * index will be in the range of 0 (inclusive) up to the length (inclusive)
8596      * of the smaller array.
8597      *
8598      * <p>If the two arrays share a common prefix then the returned index is the
8599      * length of the common prefix and it follows that there is a mismatch
8600      * between the two elements at that index within the respective arrays.
8601      * If one array is a proper prefix of the other then the returned index is
8602      * the length of the smaller array and it follows that the index is only
8603      * valid for the larger array.
8604      * Otherwise, there is no mismatch.
8605      *
8606      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8607      * prefix of length {@code pl} if the following expression is true:
8608      * <pre>{@code
8609      *     pl >= 0 &&
8610      *     pl < Math.min(a.length, b.length) &&
8611      *     Arrays.equals(a, 0, pl, b, 0, pl) &&
8612      *     !Objects.equals(a[pl], b[pl])
8613      * }</pre>
8614      * Note that a common prefix length of {@code 0} indicates that the first
8615      * elements from each array mismatch.
8616      *
8617      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8618      * prefix if the following expression is true:
8619      * <pre>{@code
8620      *     a.length != b.length &&
8621      *     Arrays.equals(a, 0, Math.min(a.length, b.length),
8622      *                   b, 0, Math.min(a.length, b.length))
8623      * }</pre>
8624      *
8625      * @param a the first array to be tested for a mismatch
8626      * @param b the second array to be tested for a mismatch
8627      * @return the index of the first mismatch between the two arrays,
8628      *         otherwise {@code -1}.
8629      * @throws NullPointerException
8630      *         if either array is {@code null}
8631      * @since 9
8632      */
8633     public static int mismatch(Object[] a, Object[] b) {
8634         int length = Math.min(a.length, b.length); // Check null array refs
8635         if (a == b)
8636             return -1;
8637 
8638         for (int i = 0; i < length; i++) {
8639             if (!Objects.equals(a[i], b[i]))
8640                 return i;
8641         }
8642 
8643         return a.length != b.length ? length : -1;
8644     }
8645 
8646     /**
8647      * Finds and returns the relative index of the first mismatch between two
8648      * {@code Object} arrays over the specified ranges, otherwise return -1 if
8649      * no mismatch is found.  The index will be in the range of 0 (inclusive) up
8650      * to the length (inclusive) of the smaller range.
8651      *
8652      * <p>If the two arrays, over the specified ranges, share a common prefix
8653      * then the returned relative index is the length of the common prefix and
8654      * it follows that there is a mismatch between the two elements at that
8655      * relative index within the respective arrays.
8656      * If one array is a proper prefix of the other, over the specified ranges,
8657      * then the returned relative index is the length of the smaller range and
8658      * it follows that the relative index is only valid for the array with the
8659      * larger range.
8660      * Otherwise, there is no mismatch.
8661      *
8662      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8663      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8664      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8665      * prefix of length {@code pl} if the following expression is true:
8666      * <pre>{@code
8667      *     pl >= 0 &&
8668      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8669      *     Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8670      *     !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
8671      * }</pre>
8672      * Note that a common prefix length of {@code 0} indicates that the first
8673      * elements from each array mismatch.
8674      *
8675      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8676      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8677      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8678      * if the following expression is true:
8679      * <pre>{@code
8680      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8681      *     Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8682      *                   b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8683      * }</pre>
8684      *
8685      * @param a the first array to be tested for a mismatch
8686      * @param aFromIndex the index (inclusive) of the first element in the
8687      *                   first array to be tested
8688      * @param aToIndex the index (exclusive) of the last element in the
8689      *                 first array to be tested
8690      * @param b the second array to be tested for a mismatch
8691      * @param bFromIndex the index (inclusive) of the first element in the
8692      *                   second array to be tested
8693      * @param bToIndex the index (exclusive) of the last element in the
8694      *                 second array to be tested
8695      * @return the relative index of the first mismatch between the two arrays
8696      *         over the specified ranges, otherwise {@code -1}.
8697      * @throws IllegalArgumentException
8698      *         if {@code aFromIndex > aToIndex} or
8699      *         if {@code bFromIndex > bToIndex}
8700      * @throws ArrayIndexOutOfBoundsException
8701      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8702      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8703      * @throws NullPointerException
8704      *         if either array is {@code null}
8705      * @since 9
8706      */
8707     public static int mismatch(
8708             Object[] a, int aFromIndex, int aToIndex,
8709             Object[] b, int bFromIndex, int bToIndex) {
8710         rangeCheck(a.length, aFromIndex, aToIndex);
8711         rangeCheck(b.length, bFromIndex, bToIndex);
8712 
8713         int aLength = aToIndex - aFromIndex;
8714         int bLength = bToIndex - bFromIndex;
8715         int length = Math.min(aLength, bLength);
8716         for (int i = 0; i < length; i++) {
8717             if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
8718                 return i;
8719         }
8720 
8721         return aLength != bLength ? length : -1;
8722     }
8723 
8724     /**
8725      * Finds and returns the index of the first mismatch between two
8726      * {@code Object} arrays, otherwise return -1 if no mismatch is found.
8727      * The index will be in the range of 0 (inclusive) up to the length
8728      * (inclusive) of the smaller array.
8729      *
8730      * <p>The specified comparator is used to determine if two array elements
8731      * from the each array are not equal.
8732      *
8733      * <p>If the two arrays share a common prefix then the returned index is the
8734      * length of the common prefix and it follows that there is a mismatch
8735      * between the two elements at that index within the respective arrays.
8736      * If one array is a proper prefix of the other then the returned index is
8737      * the length of the smaller array and it follows that the index is only
8738      * valid for the larger array.
8739      * Otherwise, there is no mismatch.
8740      *
8741      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8742      * prefix of length {@code pl} if the following expression is true:
8743      * <pre>{@code
8744      *     pl >= 0 &&
8745      *     pl < Math.min(a.length, b.length) &&
8746      *     IntStream.range(0, pl).
8747      *         map(i -> cmp.compare(a[i], b[i])).
8748      *         allMatch(c -> c == 0) &&
8749      *     cmp.compare(a[pl], b[pl]) != 0
8750      * }</pre>
8751      * Note that a common prefix length of {@code 0} indicates that the first
8752      * elements from each array mismatch.
8753      *
8754      * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8755      * prefix if the following expression is true:
8756      * <pre>{@code
8757      *     a.length != b.length &&
8758      *     IntStream.range(0, Math.min(a.length, b.length)).
8759      *         map(i -> cmp.compare(a[i], b[i])).
8760      *         allMatch(c -> c == 0) &&
8761      * }</pre>
8762      *
8763      * @param a the first array to be tested for a mismatch
8764      * @param b the second array to be tested for a mismatch
8765      * @param cmp the comparator to compare array elements
8766      * @param <T> the type of array elements
8767      * @return the index of the first mismatch between the two arrays,
8768      *         otherwise {@code -1}.
8769      * @throws NullPointerException
8770      *         if either array or the comparator is {@code null}
8771      * @since 9
8772      */
8773     public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
8774         Objects.requireNonNull(cmp);
8775         int length = Math.min(a.length, b.length); // Check null array refs
8776         if (a == b)
8777             return -1;
8778 
8779         for (int i = 0; i < length; i++) {
8780             T oa = a[i];
8781             T ob = b[i];
8782             if (oa != ob) {
8783                 // Null-value comparison is deferred to the comparator
8784                 int v = cmp.compare(oa, ob);
8785                 if (v != 0) {
8786                     return i;
8787                 }
8788             }
8789         }
8790 
8791         return a.length != b.length ? length : -1;
8792     }
8793 
8794     /**
8795      * Finds and returns the relative index of the first mismatch between two
8796      * {@code Object} arrays over the specified ranges, otherwise return -1 if
8797      * no mismatch is found.  The index will be in the range of 0 (inclusive) up
8798      * to the length (inclusive) of the smaller range.
8799      *
8800      * <p>If the two arrays, over the specified ranges, share a common prefix
8801      * then the returned relative index is the length of the common prefix and
8802      * it follows that there is a mismatch between the two elements at that
8803      * relative index within the respective arrays.
8804      * If one array is a proper prefix of the other, over the specified ranges,
8805      * then the returned relative index is the length of the smaller range and
8806      * it follows that the relative index is only valid for the array with the
8807      * larger range.
8808      * Otherwise, there is no mismatch.
8809      *
8810      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8811      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8812      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8813      * prefix of length {@code pl} if the following expression is true:
8814      * <pre>{@code
8815      *     pl >= 0 &&
8816      *     pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8817      *     IntStream.range(0, pl).
8818      *         map(i -> cmp.compare(a[aFromIndex + i], b[bFromIndex + i])).
8819      *         allMatch(c -> c == 0) &&
8820      *     cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8821      * }</pre>
8822      * Note that a common prefix length of {@code 0} indicates that the first
8823      * elements from each array mismatch.
8824      *
8825      * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8826      * ranges [{@code aFromIndex}, {@code atoIndex}) and
8827      * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8828      * if the following expression is true:
8829      * <pre>{@code
8830      *     (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8831      *     IntStream.range(0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex)).
8832      *         map(i -> cmp.compare(a[aFromIndex + i], b[bFromIndex + i])).
8833      *         allMatch(c -> c == 0)
8834      * }</pre>
8835      *
8836      * @param a the first array to be tested for a mismatch
8837      * @param aFromIndex the index (inclusive) of the first element in the
8838      *                   first array to be tested
8839      * @param aToIndex the index (exclusive) of the last element in the
8840      *                 first array to be tested
8841      * @param b the second array to be tested for a mismatch
8842      * @param bFromIndex the index (inclusive) of the first element in the
8843      *                   second array to be tested
8844      * @param bToIndex the index (exclusive) of the last element in the
8845      *                 second array to be tested
8846      * @param cmp the comparator to compare array elements
8847      * @param <T> the type of array elements
8848      * @return the relative index of the first mismatch between the two arrays
8849      *         over the specified ranges, otherwise {@code -1}.
8850      * @throws IllegalArgumentException
8851      *         if {@code aFromIndex > aToIndex} or
8852      *         if {@code bFromIndex > bToIndex}
8853      * @throws ArrayIndexOutOfBoundsException
8854      *         if {@code aFromIndex < 0 or aToIndex > a.length} or
8855      *         if {@code bFromIndex < 0 or bToIndex > b.length}
8856      * @throws NullPointerException
8857      *         if either array or the comparator is {@code null}
8858      * @since 9
8859      */
8860     public static <T> int mismatch(
8861             T[] a, int aFromIndex, int aToIndex,
8862             T[] b, int bFromIndex, int bToIndex,
8863             Comparator<? super T> cmp) {
8864         Objects.requireNonNull(cmp);
8865         rangeCheck(a.length, aFromIndex, aToIndex);
8866         rangeCheck(b.length, bFromIndex, bToIndex);
8867 
8868         int aLength = aToIndex - aFromIndex;
8869         int bLength = bToIndex - bFromIndex;
8870         int length = Math.min(aLength, bLength);
8871         for (int i = 0; i < length; i++) {
8872             T oa = a[aFromIndex++];
8873             T ob = b[bFromIndex++];
8874             if (oa != ob) {
8875                 // Null-value comparison is deferred to the comparator
8876                 int v = cmp.compare(oa, ob);
8877                 if (v != 0) {
8878                     return i;
8879                 }
8880             }
8881         }
8882 
8883         return aLength != bLength ? length : -1;
8884     }
8885 }
< prev index next >