Path: blob/master/src/java.base/share/classes/java/util/Arrays.java
41152 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import jdk.internal.util.ArraysSupport;28import jdk.internal.vm.annotation.IntrinsicCandidate;2930import java.io.Serializable;31import java.lang.reflect.Array;32import java.util.concurrent.ForkJoinPool;33import java.util.function.BinaryOperator;34import java.util.function.Consumer;35import java.util.function.DoubleBinaryOperator;36import java.util.function.IntBinaryOperator;37import java.util.function.IntFunction;38import java.util.function.IntToDoubleFunction;39import java.util.function.IntToLongFunction;40import java.util.function.IntUnaryOperator;41import java.util.function.LongBinaryOperator;42import java.util.function.UnaryOperator;43import java.util.stream.DoubleStream;44import java.util.stream.IntStream;45import java.util.stream.LongStream;46import java.util.stream.Stream;47import java.util.stream.StreamSupport;4849/**50* This class contains various methods for manipulating arrays (such as51* sorting and searching). This class also contains a static factory52* that allows arrays to be viewed as lists.53*54* <p>The methods in this class all throw a {@code NullPointerException},55* if the specified array reference is null, except where noted.56*57* <p>The documentation for the methods contained in this class includes58* brief descriptions of the <i>implementations</i>. Such descriptions should59* be regarded as <i>implementation notes</i>, rather than parts of the60* <i>specification</i>. Implementors should feel free to substitute other61* algorithms, so long as the specification itself is adhered to. (For62* example, the algorithm used by {@code sort(Object[])} does not have to be63* a MergeSort, but it does have to be <i>stable</i>.)64*65* <p>This class is a member of the66* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">67* Java Collections Framework</a>.68*69* @author Josh Bloch70* @author Neal Gafter71* @author John Rose72* @since 1.273*/74public class Arrays {7576// Suppresses default constructor, ensuring non-instantiability.77private Arrays() {}7879/*80* Sorting methods. Note that all public "sort" methods take the81* same form: performing argument checks if necessary, and then82* expanding arguments into those required for the internal83* implementation methods residing in other package-private84* classes (except for legacyMergeSort, included in this class).85*/8687/**88* Sorts the specified array into ascending numerical order.89*90* @implNote The sorting algorithm is a Dual-Pivot Quicksort91* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm92* offers O(n log(n)) performance on all data sets, and is typically93* faster than traditional (one-pivot) Quicksort implementations.94*95* @param a the array to be sorted96*/97public static void sort(int[] a) {98DualPivotQuicksort.sort(a, 0, 0, a.length);99}100101/**102* Sorts the specified range of the array into ascending order. The range103* to be sorted extends from the index {@code fromIndex}, inclusive, to104* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},105* the range to be sorted is empty.106*107* @implNote The sorting algorithm is a Dual-Pivot Quicksort108* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm109* offers O(n log(n)) performance on all data sets, and is typically110* faster than traditional (one-pivot) Quicksort implementations.111*112* @param a the array to be sorted113* @param fromIndex the index of the first element, inclusive, to be sorted114* @param toIndex the index of the last element, exclusive, to be sorted115*116* @throws IllegalArgumentException if {@code fromIndex > toIndex}117* @throws ArrayIndexOutOfBoundsException118* if {@code fromIndex < 0} or {@code toIndex > a.length}119*/120public static void sort(int[] a, int fromIndex, int toIndex) {121rangeCheck(a.length, fromIndex, toIndex);122DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);123}124125/**126* Sorts the specified array into ascending numerical order.127*128* @implNote The sorting algorithm is a Dual-Pivot Quicksort129* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm130* offers O(n log(n)) performance on all data sets, and is typically131* faster than traditional (one-pivot) Quicksort implementations.132*133* @param a the array to be sorted134*/135public static void sort(long[] a) {136DualPivotQuicksort.sort(a, 0, 0, a.length);137}138139/**140* Sorts the specified range of the array into ascending order. The range141* to be sorted extends from the index {@code fromIndex}, inclusive, to142* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},143* the range to be sorted is empty.144*145* @implNote The sorting algorithm is a Dual-Pivot Quicksort146* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm147* offers O(n log(n)) performance on all data sets, and is typically148* faster than traditional (one-pivot) Quicksort implementations.149*150* @param a the array to be sorted151* @param fromIndex the index of the first element, inclusive, to be sorted152* @param toIndex the index of the last element, exclusive, to be sorted153*154* @throws IllegalArgumentException if {@code fromIndex > toIndex}155* @throws ArrayIndexOutOfBoundsException156* if {@code fromIndex < 0} or {@code toIndex > a.length}157*/158public static void sort(long[] a, int fromIndex, int toIndex) {159rangeCheck(a.length, fromIndex, toIndex);160DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);161}162163/**164* Sorts the specified array into ascending numerical order.165*166* @implNote The sorting algorithm is a Dual-Pivot Quicksort167* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm168* offers O(n log(n)) performance on all data sets, and is typically169* faster than traditional (one-pivot) Quicksort implementations.170*171* @param a the array to be sorted172*/173public static void sort(short[] a) {174DualPivotQuicksort.sort(a, 0, a.length);175}176177/**178* Sorts the specified range of the array into ascending order. The range179* to be sorted extends from the index {@code fromIndex}, inclusive, to180* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},181* the range to be sorted is empty.182*183* @implNote The sorting algorithm is a Dual-Pivot Quicksort184* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm185* offers O(n log(n)) performance on all data sets, and is typically186* faster than traditional (one-pivot) Quicksort implementations.187*188* @param a the array to be sorted189* @param fromIndex the index of the first element, inclusive, to be sorted190* @param toIndex the index of the last element, exclusive, to be sorted191*192* @throws IllegalArgumentException if {@code fromIndex > toIndex}193* @throws ArrayIndexOutOfBoundsException194* if {@code fromIndex < 0} or {@code toIndex > a.length}195*/196public static void sort(short[] a, int fromIndex, int toIndex) {197rangeCheck(a.length, fromIndex, toIndex);198DualPivotQuicksort.sort(a, fromIndex, toIndex);199}200201/**202* Sorts the specified array into ascending numerical order.203*204* @implNote The sorting algorithm is a Dual-Pivot Quicksort205* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm206* offers O(n log(n)) performance on all data sets, and is typically207* faster than traditional (one-pivot) Quicksort implementations.208*209* @param a the array to be sorted210*/211public static void sort(char[] a) {212DualPivotQuicksort.sort(a, 0, a.length);213}214215/**216* Sorts the specified range of the array into ascending order. The range217* to be sorted extends from the index {@code fromIndex}, inclusive, to218* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},219* the range to be sorted is empty.220*221* @implNote The sorting algorithm is a Dual-Pivot Quicksort222* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm223* offers O(n log(n)) performance on all data sets, and is typically224* faster than traditional (one-pivot) Quicksort implementations.225*226* @param a the array to be sorted227* @param fromIndex the index of the first element, inclusive, to be sorted228* @param toIndex the index of the last element, exclusive, to be sorted229*230* @throws IllegalArgumentException if {@code fromIndex > toIndex}231* @throws ArrayIndexOutOfBoundsException232* if {@code fromIndex < 0} or {@code toIndex > a.length}233*/234public static void sort(char[] a, int fromIndex, int toIndex) {235rangeCheck(a.length, fromIndex, toIndex);236DualPivotQuicksort.sort(a, fromIndex, toIndex);237}238239/**240* Sorts the specified array into ascending numerical order.241*242* @implNote The sorting algorithm is a Dual-Pivot Quicksort243* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm244* offers O(n log(n)) performance on all data sets, and is typically245* faster than traditional (one-pivot) Quicksort implementations.246*247* @param a the array to be sorted248*/249public static void sort(byte[] a) {250DualPivotQuicksort.sort(a, 0, a.length);251}252253/**254* Sorts the specified range of the array into ascending order. The range255* to be sorted extends from the index {@code fromIndex}, inclusive, to256* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},257* the range to be sorted is empty.258*259* @implNote The sorting algorithm is a Dual-Pivot Quicksort260* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm261* offers O(n log(n)) performance on all data sets, and is typically262* faster than traditional (one-pivot) Quicksort implementations.263*264* @param a the array to be sorted265* @param fromIndex the index of the first element, inclusive, to be sorted266* @param toIndex the index of the last element, exclusive, to be sorted267*268* @throws IllegalArgumentException if {@code fromIndex > toIndex}269* @throws ArrayIndexOutOfBoundsException270* if {@code fromIndex < 0} or {@code toIndex > a.length}271*/272public static void sort(byte[] a, int fromIndex, int toIndex) {273rangeCheck(a.length, fromIndex, toIndex);274DualPivotQuicksort.sort(a, fromIndex, toIndex);275}276277/**278* Sorts the specified array into ascending numerical order.279*280* <p>The {@code <} relation does not provide a total order on all float281* values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}282* value compares neither less than, greater than, nor equal to any value,283* even itself. This method uses the total order imposed by the method284* {@link Float#compareTo}: {@code -0.0f} is treated as less than value285* {@code 0.0f} and {@code Float.NaN} is considered greater than any286* other value and all {@code Float.NaN} values are considered equal.287*288* @implNote The sorting algorithm is a Dual-Pivot Quicksort289* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm290* offers O(n log(n)) performance on all data sets, and is typically291* faster than traditional (one-pivot) Quicksort implementations.292*293* @param a the array to be sorted294*/295public static void sort(float[] a) {296DualPivotQuicksort.sort(a, 0, 0, a.length);297}298299/**300* Sorts the specified range of the array into ascending order. The range301* to be sorted extends from the index {@code fromIndex}, inclusive, to302* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},303* the range to be sorted is empty.304*305* <p>The {@code <} relation does not provide a total order on all float306* values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}307* value compares neither less than, greater than, nor equal to any value,308* even itself. This method uses the total order imposed by the method309* {@link Float#compareTo}: {@code -0.0f} is treated as less than value310* {@code 0.0f} and {@code Float.NaN} is considered greater than any311* other value and all {@code Float.NaN} values are considered equal.312*313* @implNote The sorting algorithm is a Dual-Pivot Quicksort314* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm315* offers O(n log(n)) performance on all data sets, and is typically316* faster than traditional (one-pivot) Quicksort implementations.317*318* @param a the array to be sorted319* @param fromIndex the index of the first element, inclusive, to be sorted320* @param toIndex the index of the last element, exclusive, to be sorted321*322* @throws IllegalArgumentException if {@code fromIndex > toIndex}323* @throws ArrayIndexOutOfBoundsException324* if {@code fromIndex < 0} or {@code toIndex > a.length}325*/326public static void sort(float[] a, int fromIndex, int toIndex) {327rangeCheck(a.length, fromIndex, toIndex);328DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);329}330331/**332* Sorts the specified array into ascending numerical order.333*334* <p>The {@code <} relation does not provide a total order on all double335* values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}336* value compares neither less than, greater than, nor equal to any value,337* even itself. This method uses the total order imposed by the method338* {@link Double#compareTo}: {@code -0.0d} is treated as less than value339* {@code 0.0d} and {@code Double.NaN} is considered greater than any340* other value and all {@code Double.NaN} values are considered equal.341*342* @implNote The sorting algorithm is a Dual-Pivot Quicksort343* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm344* offers O(n log(n)) performance on all data sets, and is typically345* faster than traditional (one-pivot) Quicksort implementations.346*347* @param a the array to be sorted348*/349public static void sort(double[] a) {350DualPivotQuicksort.sort(a, 0, 0, a.length);351}352353/**354* Sorts the specified range of the array into ascending order. The range355* to be sorted extends from the index {@code fromIndex}, inclusive, to356* the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},357* the range to be sorted is empty.358*359* <p>The {@code <} relation does not provide a total order on all double360* values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}361* value compares neither less than, greater than, nor equal to any value,362* even itself. This method uses the total order imposed by the method363* {@link Double#compareTo}: {@code -0.0d} is treated as less than value364* {@code 0.0d} and {@code Double.NaN} is considered greater than any365* other value and all {@code Double.NaN} values are considered equal.366*367* @implNote The sorting algorithm is a Dual-Pivot Quicksort368* by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm369* offers O(n log(n)) performance on all data sets, and is typically370* faster than traditional (one-pivot) Quicksort implementations.371*372* @param a the array to be sorted373* @param fromIndex the index of the first element, inclusive, to be sorted374* @param toIndex the index of the last element, exclusive, to be sorted375*376* @throws IllegalArgumentException if {@code fromIndex > toIndex}377* @throws ArrayIndexOutOfBoundsException378* if {@code fromIndex < 0} or {@code toIndex > a.length}379*/380public static void sort(double[] a, int fromIndex, int toIndex) {381rangeCheck(a.length, fromIndex, toIndex);382DualPivotQuicksort.sort(a, 0, fromIndex, toIndex);383}384385/**386* Sorts the specified array into ascending numerical order.387*388* @implNote The sorting algorithm is a Dual-Pivot Quicksort by389* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm390* offers O(n log(n)) performance on all data sets, and is typically391* faster than traditional (one-pivot) Quicksort implementations.392*393* @param a the array to be sorted394*395* @since 1.8396*/397public static void parallelSort(byte[] a) {398DualPivotQuicksort.sort(a, 0, a.length);399}400401/**402* Sorts the specified range of the array into ascending numerical order.403* The range to be sorted extends from the index {@code fromIndex},404* inclusive, to the index {@code toIndex}, exclusive. If405* {@code fromIndex == toIndex}, the range to be sorted is empty.406*407* @implNote The sorting algorithm is a Dual-Pivot Quicksort by408* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm409* offers O(n log(n)) performance on all data sets, and is typically410* faster than traditional (one-pivot) Quicksort implementations.411*412* @param a the array to be sorted413* @param fromIndex the index of the first element, inclusive, to be sorted414* @param toIndex the index of the last element, exclusive, to be sorted415*416* @throws IllegalArgumentException if {@code fromIndex > toIndex}417* @throws ArrayIndexOutOfBoundsException418* if {@code fromIndex < 0} or {@code toIndex > a.length}419*420* @since 1.8421*/422public static void parallelSort(byte[] a, int fromIndex, int toIndex) {423rangeCheck(a.length, fromIndex, toIndex);424DualPivotQuicksort.sort(a, fromIndex, toIndex);425}426427/**428* Sorts the specified array into ascending numerical order.429*430* @implNote The sorting algorithm is a Dual-Pivot Quicksort by431* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm432* offers O(n log(n)) performance on all data sets, and is typically433* faster than traditional (one-pivot) Quicksort implementations.434*435* @param a the array to be sorted436*437* @since 1.8438*/439public static void parallelSort(char[] a) {440DualPivotQuicksort.sort(a, 0, a.length);441}442443/**444* Sorts the specified range of the array into ascending numerical order.445* The range to be sorted extends from the index {@code fromIndex},446* inclusive, to the index {@code toIndex}, exclusive. If447* {@code fromIndex == toIndex}, the range to be sorted is empty.448*449* @implNote The sorting algorithm is a Dual-Pivot Quicksort by450* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm451* offers O(n log(n)) performance on all data sets, and is typically452* faster than traditional (one-pivot) Quicksort implementations.453*454* @param a the array to be sorted455* @param fromIndex the index of the first element, inclusive, to be sorted456* @param toIndex the index of the last element, exclusive, to be sorted457*458* @throws IllegalArgumentException if {@code fromIndex > toIndex}459* @throws ArrayIndexOutOfBoundsException460* if {@code fromIndex < 0} or {@code toIndex > a.length}461*462* @since 1.8463*/464public static void parallelSort(char[] a, int fromIndex, int toIndex) {465rangeCheck(a.length, fromIndex, toIndex);466DualPivotQuicksort.sort(a, fromIndex, toIndex);467}468469/**470* Sorts the specified array into ascending numerical order.471*472* @implNote The sorting algorithm is a Dual-Pivot Quicksort by473* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm474* offers O(n log(n)) performance on all data sets, and is typically475* faster than traditional (one-pivot) Quicksort implementations.476*477* @param a the array to be sorted478*479* @since 1.8480*/481public static void parallelSort(short[] a) {482DualPivotQuicksort.sort(a, 0, a.length);483}484485/**486* Sorts the specified range of the array into ascending numerical order.487* The range to be sorted extends from the index {@code fromIndex},488* inclusive, to the index {@code toIndex}, exclusive. If489* {@code fromIndex == toIndex}, the range to be sorted is empty.490*491* @implNote The sorting algorithm is a Dual-Pivot Quicksort by492* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm493* offers O(n log(n)) performance on all data sets, and is typically494* faster than traditional (one-pivot) Quicksort implementations.495*496* @param a the array to be sorted497* @param fromIndex the index of the first element, inclusive, to be sorted498* @param toIndex the index of the last element, exclusive, to be sorted499*500* @throws IllegalArgumentException if {@code fromIndex > toIndex}501* @throws ArrayIndexOutOfBoundsException502* if {@code fromIndex < 0} or {@code toIndex > a.length}503*504* @since 1.8505*/506public static void parallelSort(short[] a, int fromIndex, int toIndex) {507rangeCheck(a.length, fromIndex, toIndex);508DualPivotQuicksort.sort(a, fromIndex, toIndex);509}510511/**512* Sorts the specified array into ascending numerical order.513*514* @implNote The sorting algorithm is a Dual-Pivot Quicksort by515* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm516* offers O(n log(n)) performance on all data sets, and is typically517* faster than traditional (one-pivot) Quicksort implementations.518*519* @param a the array to be sorted520*521* @since 1.8522*/523public static void parallelSort(int[] a) {524DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);525}526527/**528* Sorts the specified range of the array into ascending numerical order.529* The range to be sorted extends from the index {@code fromIndex},530* inclusive, to the index {@code toIndex}, exclusive. If531* {@code fromIndex == toIndex}, the range to be sorted is empty.532*533* @implNote The sorting algorithm is a Dual-Pivot Quicksort by534* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm535* offers O(n log(n)) performance on all data sets, and is typically536* faster than traditional (one-pivot) Quicksort implementations.537*538* @param a the array to be sorted539* @param fromIndex the index of the first element, inclusive, to be sorted540* @param toIndex the index of the last element, exclusive, to be sorted541*542* @throws IllegalArgumentException if {@code fromIndex > toIndex}543* @throws ArrayIndexOutOfBoundsException544* if {@code fromIndex < 0} or {@code toIndex > a.length}545*546* @since 1.8547*/548public static void parallelSort(int[] a, int fromIndex, int toIndex) {549rangeCheck(a.length, fromIndex, toIndex);550DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);551}552553/**554* Sorts the specified array into ascending numerical order.555*556* @implNote The sorting algorithm is a Dual-Pivot Quicksort by557* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm558* offers O(n log(n)) performance on all data sets, and is typically559* faster than traditional (one-pivot) Quicksort implementations.560*561* @param a the array to be sorted562*563* @since 1.8564*/565public static void parallelSort(long[] a) {566DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);567}568569/**570* Sorts the specified range of the array into ascending numerical order.571* The range to be sorted extends from the index {@code fromIndex},572* inclusive, to the index {@code toIndex}, exclusive. If573* {@code fromIndex == toIndex}, the range to be sorted is empty.574*575* @implNote The sorting algorithm is a Dual-Pivot Quicksort by576* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm577* offers O(n log(n)) performance on all data sets, and is typically578* faster than traditional (one-pivot) Quicksort implementations.579*580* @param a the array to be sorted581* @param fromIndex the index of the first element, inclusive, to be sorted582* @param toIndex the index of the last element, exclusive, to be sorted583*584* @throws IllegalArgumentException if {@code fromIndex > toIndex}585* @throws ArrayIndexOutOfBoundsException586* if {@code fromIndex < 0} or {@code toIndex > a.length}587*588* @since 1.8589*/590public static void parallelSort(long[] a, int fromIndex, int toIndex) {591rangeCheck(a.length, fromIndex, toIndex);592DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);593}594595/**596* Sorts the specified array into ascending numerical order.597*598* <p>The {@code <} relation does not provide a total order on all float599* values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}600* value compares neither less than, greater than, nor equal to any value,601* even itself. This method uses the total order imposed by the method602* {@link Float#compareTo}: {@code -0.0f} is treated as less than value603* {@code 0.0f} and {@code Float.NaN} is considered greater than any604* other value and all {@code Float.NaN} values are considered equal.605*606* @implNote The sorting algorithm is a Dual-Pivot Quicksort by607* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm608* offers O(n log(n)) performance on all data sets, and is typically609* faster than traditional (one-pivot) Quicksort implementations.610*611* @param a the array to be sorted612*613* @since 1.8614*/615public static void parallelSort(float[] a) {616DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);617}618619/**620* Sorts the specified range of the array into ascending numerical order.621* The range to be sorted extends from the index {@code fromIndex},622* inclusive, to the index {@code toIndex}, exclusive. If623* {@code fromIndex == toIndex}, the range to be sorted is empty.624*625* <p>The {@code <} relation does not provide a total order on all float626* values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}627* value compares neither less than, greater than, nor equal to any value,628* even itself. This method uses the total order imposed by the method629* {@link Float#compareTo}: {@code -0.0f} is treated as less than value630* {@code 0.0f} and {@code Float.NaN} is considered greater than any631* other value and all {@code Float.NaN} values are considered equal.632*633* @implNote The sorting algorithm is a Dual-Pivot Quicksort by634* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm635* offers O(n log(n)) performance on all data sets, and is typically636* faster than traditional (one-pivot) Quicksort implementations.637*638* @param a the array to be sorted639* @param fromIndex the index of the first element, inclusive, to be sorted640* @param toIndex the index of the last element, exclusive, to be sorted641*642* @throws IllegalArgumentException if {@code fromIndex > toIndex}643* @throws ArrayIndexOutOfBoundsException644* if {@code fromIndex < 0} or {@code toIndex > a.length}645*646* @since 1.8647*/648public static void parallelSort(float[] a, int fromIndex, int toIndex) {649rangeCheck(a.length, fromIndex, toIndex);650DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);651}652653/**654* Sorts the specified array into ascending numerical order.655*656* <p>The {@code <} relation does not provide a total order on all double657* values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}658* value compares neither less than, greater than, nor equal to any value,659* even itself. This method uses the total order imposed by the method660* {@link Double#compareTo}: {@code -0.0d} is treated as less than value661* {@code 0.0d} and {@code Double.NaN} is considered greater than any662* other value and all {@code Double.NaN} values are considered equal.663*664* @implNote The sorting algorithm is a Dual-Pivot Quicksort by665* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm666* offers O(n log(n)) performance on all data sets, and is typically667* faster than traditional (one-pivot) Quicksort implementations.668*669* @param a the array to be sorted670*671* @since 1.8672*/673public static void parallelSort(double[] a) {674DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), 0, a.length);675}676677/**678* Sorts the specified range of the array into ascending numerical order.679* The range to be sorted extends from the index {@code fromIndex},680* inclusive, to the index {@code toIndex}, exclusive. If681* {@code fromIndex == toIndex}, the range to be sorted is empty.682*683* <p>The {@code <} relation does not provide a total order on all double684* values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}685* value compares neither less than, greater than, nor equal to any value,686* even itself. This method uses the total order imposed by the method687* {@link Double#compareTo}: {@code -0.0d} is treated as less than value688* {@code 0.0d} and {@code Double.NaN} is considered greater than any689* other value and all {@code Double.NaN} values are considered equal.690*691* @implNote The sorting algorithm is a Dual-Pivot Quicksort by692* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm693* offers O(n log(n)) performance on all data sets, and is typically694* faster than traditional (one-pivot) Quicksort implementations.695*696* @param a the array to be sorted697* @param fromIndex the index of the first element, inclusive, to be sorted698* @param toIndex the index of the last element, exclusive, to be sorted699*700* @throws IllegalArgumentException if {@code fromIndex > toIndex}701* @throws ArrayIndexOutOfBoundsException702* if {@code fromIndex < 0} or {@code toIndex > a.length}703*704* @since 1.8705*/706public static void parallelSort(double[] a, int fromIndex, int toIndex) {707rangeCheck(a.length, fromIndex, toIndex);708DualPivotQuicksort.sort(a, ForkJoinPool.getCommonPoolParallelism(), fromIndex, toIndex);709}710711/**712* Checks that {@code fromIndex} and {@code toIndex} are in713* the range and throws an exception if they aren't.714*/715static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {716if (fromIndex > toIndex) {717throw new IllegalArgumentException(718"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");719}720if (fromIndex < 0) {721throw new ArrayIndexOutOfBoundsException(fromIndex);722}723if (toIndex > arrayLength) {724throw new ArrayIndexOutOfBoundsException(toIndex);725}726}727728/**729* A comparator that implements the natural ordering of a group of730* mutually comparable elements. May be used when a supplied731* comparator is null. To simplify code-sharing within underlying732* implementations, the compare method only declares type Object733* for its second argument.734*735* Arrays class implementor's note: It is an empirical matter736* whether ComparableTimSort offers any performance benefit over737* TimSort used with this comparator. If not, you are better off738* deleting or bypassing ComparableTimSort. There is currently no739* empirical case for separating them for parallel sorting, so all740* public Object parallelSort methods use the same comparator741* based implementation.742*/743static final class NaturalOrder implements Comparator<Object> {744@SuppressWarnings("unchecked")745public int compare(Object first, Object second) {746return ((Comparable<Object>)first).compareTo(second);747}748static final NaturalOrder INSTANCE = new NaturalOrder();749}750751/**752* The minimum array length below which a parallel sorting753* algorithm will not further partition the sorting task. Using754* smaller sizes typically results in memory contention across755* tasks that makes parallel speedups unlikely.756*/757private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;758759/**760* Sorts the specified array of objects into ascending order, according761* to the {@linkplain Comparable natural ordering} of its elements.762* All elements in the array must implement the {@link Comparable}763* interface. Furthermore, all elements in the array must be764* <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must765* not throw a {@code ClassCastException} for any elements {@code e1}766* and {@code e2} in the array).767*768* <p>This sort is guaranteed to be <i>stable</i>: equal elements will769* not be reordered as a result of the sort.770*771* @implNote The sorting algorithm is a parallel sort-merge that breaks the772* array into sub-arrays that are themselves sorted and then merged. When773* the sub-array length reaches a minimum granularity, the sub-array is774* sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}775* method. If the length of the specified array is less than the minimum776* granularity, then it is sorted using the appropriate {@link777* Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a778* working space no greater than the size of the original array. The779* {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to780* execute any parallel tasks.781*782* @param <T> the class of the objects to be sorted783* @param a the array to be sorted784*785* @throws ClassCastException if the array contains elements that are not786* <i>mutually comparable</i> (for example, strings and integers)787* @throws IllegalArgumentException (optional) if the natural788* ordering of the array elements is found to violate the789* {@link Comparable} contract790*791* @since 1.8792*/793@SuppressWarnings("unchecked")794public static <T extends Comparable<? super T>> void parallelSort(T[] a) {795int n = a.length, p, g;796if (n <= MIN_ARRAY_SORT_GRAN ||797(p = ForkJoinPool.getCommonPoolParallelism()) == 1)798TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);799else800new ArraysParallelSortHelpers.FJObject.Sorter<>801(null, a,802(T[])Array.newInstance(a.getClass().getComponentType(), n),8030, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?804MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();805}806807/**808* Sorts the specified range of the specified array of objects into809* ascending order, according to the810* {@linkplain Comparable natural ordering} of its811* elements. The range to be sorted extends from index812* {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.813* (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All814* elements in this range must implement the {@link Comparable}815* interface. Furthermore, all elements in this range must be <i>mutually816* comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a817* {@code ClassCastException} for any elements {@code e1} and818* {@code e2} in the array).819*820* <p>This sort is guaranteed to be <i>stable</i>: equal elements will821* not be reordered as a result of the sort.822*823* @implNote The sorting algorithm is a parallel sort-merge that breaks the824* array into sub-arrays that are themselves sorted and then merged. When825* the sub-array length reaches a minimum granularity, the sub-array is826* sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}827* method. If the length of the specified array is less than the minimum828* granularity, then it is sorted using the appropriate {@link829* Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working830* space no greater than the size of the specified range of the original831* array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is832* used to execute any parallel tasks.833*834* @param <T> the class of the objects to be sorted835* @param a the array to be sorted836* @param fromIndex the index of the first element (inclusive) to be837* sorted838* @param toIndex the index of the last element (exclusive) to be sorted839* @throws IllegalArgumentException if {@code fromIndex > toIndex} or840* (optional) if the natural ordering of the array elements is841* found to violate the {@link Comparable} contract842* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or843* {@code toIndex > a.length}844* @throws ClassCastException if the array contains elements that are845* not <i>mutually comparable</i> (for example, strings and846* integers).847*848* @since 1.8849*/850@SuppressWarnings("unchecked")851public static <T extends Comparable<? super T>>852void parallelSort(T[] a, int fromIndex, int toIndex) {853rangeCheck(a.length, fromIndex, toIndex);854int n = toIndex - fromIndex, p, g;855if (n <= MIN_ARRAY_SORT_GRAN ||856(p = ForkJoinPool.getCommonPoolParallelism()) == 1)857TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);858else859new ArraysParallelSortHelpers.FJObject.Sorter<>860(null, a,861(T[])Array.newInstance(a.getClass().getComponentType(), n),862fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?863MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();864}865866/**867* Sorts the specified array of objects according to the order induced by868* the specified comparator. All elements in the array must be869* <i>mutually comparable</i> by the specified comparator (that is,870* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}871* for any elements {@code e1} and {@code e2} in the array).872*873* <p>This sort is guaranteed to be <i>stable</i>: equal elements will874* not be reordered as a result of the sort.875*876* @implNote The sorting algorithm is a parallel sort-merge that breaks the877* array into sub-arrays that are themselves sorted and then merged. When878* the sub-array length reaches a minimum granularity, the sub-array is879* sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}880* method. If the length of the specified array is less than the minimum881* granularity, then it is sorted using the appropriate {@link882* Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a883* working space no greater than the size of the original array. The884* {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to885* execute any parallel tasks.886*887* @param <T> the class of the objects to be sorted888* @param a the array to be sorted889* @param cmp the comparator to determine the order of the array. A890* {@code null} value indicates that the elements'891* {@linkplain Comparable natural ordering} should be used.892* @throws ClassCastException if the array contains elements that are893* not <i>mutually comparable</i> using the specified comparator894* @throws IllegalArgumentException (optional) if the comparator is895* found to violate the {@link java.util.Comparator} contract896*897* @since 1.8898*/899@SuppressWarnings("unchecked")900public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {901if (cmp == null)902cmp = NaturalOrder.INSTANCE;903int n = a.length, p, g;904if (n <= MIN_ARRAY_SORT_GRAN ||905(p = ForkJoinPool.getCommonPoolParallelism()) == 1)906TimSort.sort(a, 0, n, cmp, null, 0, 0);907else908new ArraysParallelSortHelpers.FJObject.Sorter<>909(null, a,910(T[])Array.newInstance(a.getClass().getComponentType(), n),9110, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?912MIN_ARRAY_SORT_GRAN : g, cmp).invoke();913}914915/**916* Sorts the specified range of the specified array of objects according917* to the order induced by the specified comparator. The range to be918* sorted extends from index {@code fromIndex}, inclusive, to index919* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the920* range to be sorted is empty.) All elements in the range must be921* <i>mutually comparable</i> by the specified comparator (that is,922* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}923* for any elements {@code e1} and {@code e2} in the range).924*925* <p>This sort is guaranteed to be <i>stable</i>: equal elements will926* not be reordered as a result of the sort.927*928* @implNote The sorting algorithm is a parallel sort-merge that breaks the929* array into sub-arrays that are themselves sorted and then merged. When930* the sub-array length reaches a minimum granularity, the sub-array is931* sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}932* method. If the length of the specified array is less than the minimum933* granularity, then it is sorted using the appropriate {@link934* Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working935* space no greater than the size of the specified range of the original936* array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is937* used to execute any parallel tasks.938*939* @param <T> the class of the objects to be sorted940* @param a the array to be sorted941* @param fromIndex the index of the first element (inclusive) to be942* sorted943* @param toIndex the index of the last element (exclusive) to be sorted944* @param cmp the comparator to determine the order of the array. A945* {@code null} value indicates that the elements'946* {@linkplain Comparable natural ordering} should be used.947* @throws IllegalArgumentException if {@code fromIndex > toIndex} or948* (optional) if the natural ordering of the array elements is949* found to violate the {@link Comparable} contract950* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or951* {@code toIndex > a.length}952* @throws ClassCastException if the array contains elements that are953* not <i>mutually comparable</i> (for example, strings and954* integers).955*956* @since 1.8957*/958@SuppressWarnings("unchecked")959public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,960Comparator<? super T> cmp) {961rangeCheck(a.length, fromIndex, toIndex);962if (cmp == null)963cmp = NaturalOrder.INSTANCE;964int n = toIndex - fromIndex, p, g;965if (n <= MIN_ARRAY_SORT_GRAN ||966(p = ForkJoinPool.getCommonPoolParallelism()) == 1)967TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);968else969new ArraysParallelSortHelpers.FJObject.Sorter<>970(null, a,971(T[])Array.newInstance(a.getClass().getComponentType(), n),972fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?973MIN_ARRAY_SORT_GRAN : g, cmp).invoke();974}975976/*977* Sorting of complex type arrays.978*/979980/**981* Old merge sort implementation can be selected (for982* compatibility with broken comparators) using a system property.983* Cannot be a static boolean in the enclosing class due to984* circular dependencies. To be removed in a future release.985*/986static final class LegacyMergeSort {987@SuppressWarnings("removal")988private static final boolean userRequested =989java.security.AccessController.doPrivileged(990new sun.security.action.GetBooleanAction(991"java.util.Arrays.useLegacyMergeSort")).booleanValue();992}993994/**995* Sorts the specified array of objects into ascending order, according996* to the {@linkplain Comparable natural ordering} of its elements.997* All elements in the array must implement the {@link Comparable}998* interface. Furthermore, all elements in the array must be999* <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must1000* not throw a {@code ClassCastException} for any elements {@code e1}1001* and {@code e2} in the array).1002*1003* <p>This sort is guaranteed to be <i>stable</i>: equal elements will1004* not be reordered as a result of the sort.1005*1006* <p>Implementation note: This implementation is a stable, adaptive,1007* iterative mergesort that requires far fewer than n lg(n) comparisons1008* when the input array is partially sorted, while offering the1009* performance of a traditional mergesort when the input array is1010* randomly ordered. If the input array is nearly sorted, the1011* implementation requires approximately n comparisons. Temporary1012* storage requirements vary from a small constant for nearly sorted1013* input arrays to n/2 object references for randomly ordered input1014* arrays.1015*1016* <p>The implementation takes equal advantage of ascending and1017* descending order in its input array, and can take advantage of1018* ascending and descending order in different parts of the same1019* input array. It is well-suited to merging two or more sorted arrays:1020* simply concatenate the arrays and sort the resulting array.1021*1022* <p>The implementation was adapted from Tim Peters's list sort for Python1023* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">1024* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic1025* Sorting and Information Theoretic Complexity", in Proceedings of the1026* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,1027* January 1993.1028*1029* @param a the array to be sorted1030* @throws ClassCastException if the array contains elements that are not1031* <i>mutually comparable</i> (for example, strings and integers)1032* @throws IllegalArgumentException (optional) if the natural1033* ordering of the array elements is found to violate the1034* {@link Comparable} contract1035*/1036public static void sort(Object[] a) {1037if (LegacyMergeSort.userRequested)1038legacyMergeSort(a);1039else1040ComparableTimSort.sort(a, 0, a.length, null, 0, 0);1041}10421043/** To be removed in a future release. */1044private static void legacyMergeSort(Object[] a) {1045Object[] aux = a.clone();1046mergeSort(aux, a, 0, a.length, 0);1047}10481049/**1050* Sorts the specified range of the specified array of objects into1051* ascending order, according to the1052* {@linkplain Comparable natural ordering} of its1053* elements. The range to be sorted extends from index1054* {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.1055* (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All1056* elements in this range must implement the {@link Comparable}1057* interface. Furthermore, all elements in this range must be <i>mutually1058* comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a1059* {@code ClassCastException} for any elements {@code e1} and1060* {@code e2} in the array).1061*1062* <p>This sort is guaranteed to be <i>stable</i>: equal elements will1063* not be reordered as a result of the sort.1064*1065* <p>Implementation note: This implementation is a stable, adaptive,1066* iterative mergesort that requires far fewer than n lg(n) comparisons1067* when the input array is partially sorted, while offering the1068* performance of a traditional mergesort when the input array is1069* randomly ordered. If the input array is nearly sorted, the1070* implementation requires approximately n comparisons. Temporary1071* storage requirements vary from a small constant for nearly sorted1072* input arrays to n/2 object references for randomly ordered input1073* arrays.1074*1075* <p>The implementation takes equal advantage of ascending and1076* descending order in its input array, and can take advantage of1077* ascending and descending order in different parts of the same1078* input array. It is well-suited to merging two or more sorted arrays:1079* simply concatenate the arrays and sort the resulting array.1080*1081* <p>The implementation was adapted from Tim Peters's list sort for Python1082* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">1083* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic1084* Sorting and Information Theoretic Complexity", in Proceedings of the1085* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,1086* January 1993.1087*1088* @param a the array to be sorted1089* @param fromIndex the index of the first element (inclusive) to be1090* sorted1091* @param toIndex the index of the last element (exclusive) to be sorted1092* @throws IllegalArgumentException if {@code fromIndex > toIndex} or1093* (optional) if the natural ordering of the array elements is1094* found to violate the {@link Comparable} contract1095* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or1096* {@code toIndex > a.length}1097* @throws ClassCastException if the array contains elements that are1098* not <i>mutually comparable</i> (for example, strings and1099* integers).1100*/1101public static void sort(Object[] a, int fromIndex, int toIndex) {1102rangeCheck(a.length, fromIndex, toIndex);1103if (LegacyMergeSort.userRequested)1104legacyMergeSort(a, fromIndex, toIndex);1105else1106ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);1107}11081109/** To be removed in a future release. */1110private static void legacyMergeSort(Object[] a,1111int fromIndex, int toIndex) {1112Object[] aux = copyOfRange(a, fromIndex, toIndex);1113mergeSort(aux, a, fromIndex, toIndex, -fromIndex);1114}11151116/**1117* Tuning parameter: list size at or below which insertion sort will be1118* used in preference to mergesort.1119* To be removed in a future release.1120*/1121private static final int INSERTIONSORT_THRESHOLD = 7;11221123/**1124* Src is the source array that starts at index 01125* Dest is the (possibly larger) array destination with a possible offset1126* low is the index in dest to start sorting1127* high is the end index in dest to end sorting1128* off is the offset to generate corresponding low, high in src1129* To be removed in a future release.1130*/1131@SuppressWarnings({"unchecked", "rawtypes"})1132private static void mergeSort(Object[] src,1133Object[] dest,1134int low,1135int high,1136int off) {1137int length = high - low;11381139// Insertion sort on smallest arrays1140if (length < INSERTIONSORT_THRESHOLD) {1141for (int i=low; i<high; i++)1142for (int j=i; j>low &&1143((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)1144swap(dest, j, j-1);1145return;1146}11471148// Recursively sort halves of dest into src1149int destLow = low;1150int destHigh = high;1151low += off;1152high += off;1153int mid = (low + high) >>> 1;1154mergeSort(dest, src, low, mid, -off);1155mergeSort(dest, src, mid, high, -off);11561157// If list is already sorted, just copy from src to dest. This is an1158// optimization that results in faster sorts for nearly ordered lists.1159if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {1160System.arraycopy(src, low, dest, destLow, length);1161return;1162}11631164// Merge sorted halves (now in src) into dest1165for(int i = destLow, p = low, q = mid; i < destHigh; i++) {1166if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)1167dest[i] = src[p++];1168else1169dest[i] = src[q++];1170}1171}11721173/**1174* Swaps x[a] with x[b].1175*/1176private static void swap(Object[] x, int a, int b) {1177Object t = x[a];1178x[a] = x[b];1179x[b] = t;1180}11811182/**1183* Sorts the specified array of objects according to the order induced by1184* the specified comparator. All elements in the array must be1185* <i>mutually comparable</i> by the specified comparator (that is,1186* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}1187* for any elements {@code e1} and {@code e2} in the array).1188*1189* <p>This sort is guaranteed to be <i>stable</i>: equal elements will1190* not be reordered as a result of the sort.1191*1192* <p>Implementation note: This implementation is a stable, adaptive,1193* iterative mergesort that requires far fewer than n lg(n) comparisons1194* when the input array is partially sorted, while offering the1195* performance of a traditional mergesort when the input array is1196* randomly ordered. If the input array is nearly sorted, the1197* implementation requires approximately n comparisons. Temporary1198* storage requirements vary from a small constant for nearly sorted1199* input arrays to n/2 object references for randomly ordered input1200* arrays.1201*1202* <p>The implementation takes equal advantage of ascending and1203* descending order in its input array, and can take advantage of1204* ascending and descending order in different parts of the same1205* input array. It is well-suited to merging two or more sorted arrays:1206* simply concatenate the arrays and sort the resulting array.1207*1208* <p>The implementation was adapted from Tim Peters's list sort for Python1209* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">1210* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic1211* Sorting and Information Theoretic Complexity", in Proceedings of the1212* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,1213* January 1993.1214*1215* @param <T> the class of the objects to be sorted1216* @param a the array to be sorted1217* @param c the comparator to determine the order of the array. A1218* {@code null} value indicates that the elements'1219* {@linkplain Comparable natural ordering} should be used.1220* @throws ClassCastException if the array contains elements that are1221* not <i>mutually comparable</i> using the specified comparator1222* @throws IllegalArgumentException (optional) if the comparator is1223* found to violate the {@link Comparator} contract1224*/1225public static <T> void sort(T[] a, Comparator<? super T> c) {1226if (c == null) {1227sort(a);1228} else {1229if (LegacyMergeSort.userRequested)1230legacyMergeSort(a, c);1231else1232TimSort.sort(a, 0, a.length, c, null, 0, 0);1233}1234}12351236/** To be removed in a future release. */1237private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {1238T[] aux = a.clone();1239if (c==null)1240mergeSort(aux, a, 0, a.length, 0);1241else1242mergeSort(aux, a, 0, a.length, 0, c);1243}12441245/**1246* Sorts the specified range of the specified array of objects according1247* to the order induced by the specified comparator. The range to be1248* sorted extends from index {@code fromIndex}, inclusive, to index1249* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the1250* range to be sorted is empty.) All elements in the range must be1251* <i>mutually comparable</i> by the specified comparator (that is,1252* {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}1253* for any elements {@code e1} and {@code e2} in the range).1254*1255* <p>This sort is guaranteed to be <i>stable</i>: equal elements will1256* not be reordered as a result of the sort.1257*1258* <p>Implementation note: This implementation is a stable, adaptive,1259* iterative mergesort that requires far fewer than n lg(n) comparisons1260* when the input array is partially sorted, while offering the1261* performance of a traditional mergesort when the input array is1262* randomly ordered. If the input array is nearly sorted, the1263* implementation requires approximately n comparisons. Temporary1264* storage requirements vary from a small constant for nearly sorted1265* input arrays to n/2 object references for randomly ordered input1266* arrays.1267*1268* <p>The implementation takes equal advantage of ascending and1269* descending order in its input array, and can take advantage of1270* ascending and descending order in different parts of the same1271* input array. It is well-suited to merging two or more sorted arrays:1272* simply concatenate the arrays and sort the resulting array.1273*1274* <p>The implementation was adapted from Tim Peters's list sort for Python1275* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">1276* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic1277* Sorting and Information Theoretic Complexity", in Proceedings of the1278* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,1279* January 1993.1280*1281* @param <T> the class of the objects to be sorted1282* @param a the array to be sorted1283* @param fromIndex the index of the first element (inclusive) to be1284* sorted1285* @param toIndex the index of the last element (exclusive) to be sorted1286* @param c the comparator to determine the order of the array. A1287* {@code null} value indicates that the elements'1288* {@linkplain Comparable natural ordering} should be used.1289* @throws ClassCastException if the array contains elements that are not1290* <i>mutually comparable</i> using the specified comparator.1291* @throws IllegalArgumentException if {@code fromIndex > toIndex} or1292* (optional) if the comparator is found to violate the1293* {@link Comparator} contract1294* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or1295* {@code toIndex > a.length}1296*/1297public static <T> void sort(T[] a, int fromIndex, int toIndex,1298Comparator<? super T> c) {1299if (c == null) {1300sort(a, fromIndex, toIndex);1301} else {1302rangeCheck(a.length, fromIndex, toIndex);1303if (LegacyMergeSort.userRequested)1304legacyMergeSort(a, fromIndex, toIndex, c);1305else1306TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);1307}1308}13091310/** To be removed in a future release. */1311private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,1312Comparator<? super T> c) {1313T[] aux = copyOfRange(a, fromIndex, toIndex);1314if (c==null)1315mergeSort(aux, a, fromIndex, toIndex, -fromIndex);1316else1317mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);1318}13191320/**1321* Src is the source array that starts at index 01322* Dest is the (possibly larger) array destination with a possible offset1323* low is the index in dest to start sorting1324* high is the end index in dest to end sorting1325* off is the offset into src corresponding to low in dest1326* To be removed in a future release.1327*/1328@SuppressWarnings({"rawtypes", "unchecked"})1329private static void mergeSort(Object[] src,1330Object[] dest,1331int low, int high, int off,1332Comparator c) {1333int length = high - low;13341335// Insertion sort on smallest arrays1336if (length < INSERTIONSORT_THRESHOLD) {1337for (int i=low; i<high; i++)1338for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)1339swap(dest, j, j-1);1340return;1341}13421343// Recursively sort halves of dest into src1344int destLow = low;1345int destHigh = high;1346low += off;1347high += off;1348int mid = (low + high) >>> 1;1349mergeSort(dest, src, low, mid, -off, c);1350mergeSort(dest, src, mid, high, -off, c);13511352// If list is already sorted, just copy from src to dest. This is an1353// optimization that results in faster sorts for nearly ordered lists.1354if (c.compare(src[mid-1], src[mid]) <= 0) {1355System.arraycopy(src, low, dest, destLow, length);1356return;1357}13581359// Merge sorted halves (now in src) into dest1360for(int i = destLow, p = low, q = mid; i < destHigh; i++) {1361if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)1362dest[i] = src[p++];1363else1364dest[i] = src[q++];1365}1366}13671368// Parallel prefix13691370/**1371* Cumulates, in parallel, each element of the given array in place,1372* using the supplied function. For example if the array initially1373* holds {@code [2, 1, 0, 3]} and the operation performs addition,1374* then upon return the array holds {@code [2, 3, 3, 6]}.1375* Parallel prefix computation is usually more efficient than1376* sequential loops for large arrays.1377*1378* @param <T> the class of the objects in the array1379* @param array the array, which is modified in-place by this method1380* @param op a side-effect-free, associative function to perform the1381* cumulation1382* @throws NullPointerException if the specified array or function is null1383* @since 1.81384*/1385public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {1386Objects.requireNonNull(op);1387if (array.length > 0)1388new ArrayPrefixHelpers.CumulateTask<>1389(null, op, array, 0, array.length).invoke();1390}13911392/**1393* Performs {@link #parallelPrefix(Object[], BinaryOperator)}1394* for the given subrange of the array.1395*1396* @param <T> the class of the objects in the array1397* @param array the array1398* @param fromIndex the index of the first element, inclusive1399* @param toIndex the index of the last element, exclusive1400* @param op a side-effect-free, associative function to perform the1401* cumulation1402* @throws IllegalArgumentException if {@code fromIndex > toIndex}1403* @throws ArrayIndexOutOfBoundsException1404* if {@code fromIndex < 0} or {@code toIndex > array.length}1405* @throws NullPointerException if the specified array or function is null1406* @since 1.81407*/1408public static <T> void parallelPrefix(T[] array, int fromIndex,1409int toIndex, BinaryOperator<T> op) {1410Objects.requireNonNull(op);1411rangeCheck(array.length, fromIndex, toIndex);1412if (fromIndex < toIndex)1413new ArrayPrefixHelpers.CumulateTask<>1414(null, op, array, fromIndex, toIndex).invoke();1415}14161417/**1418* Cumulates, in parallel, each element of the given array in place,1419* using the supplied function. For example if the array initially1420* holds {@code [2, 1, 0, 3]} and the operation performs addition,1421* then upon return the array holds {@code [2, 3, 3, 6]}.1422* Parallel prefix computation is usually more efficient than1423* sequential loops for large arrays.1424*1425* @param array the array, which is modified in-place by this method1426* @param op a side-effect-free, associative function to perform the1427* cumulation1428* @throws NullPointerException if the specified array or function is null1429* @since 1.81430*/1431public static void parallelPrefix(long[] array, LongBinaryOperator op) {1432Objects.requireNonNull(op);1433if (array.length > 0)1434new ArrayPrefixHelpers.LongCumulateTask1435(null, op, array, 0, array.length).invoke();1436}14371438/**1439* Performs {@link #parallelPrefix(long[], LongBinaryOperator)}1440* for the given subrange of the array.1441*1442* @param array the array1443* @param fromIndex the index of the first element, inclusive1444* @param toIndex the index of the last element, exclusive1445* @param op a side-effect-free, associative function to perform the1446* cumulation1447* @throws IllegalArgumentException if {@code fromIndex > toIndex}1448* @throws ArrayIndexOutOfBoundsException1449* if {@code fromIndex < 0} or {@code toIndex > array.length}1450* @throws NullPointerException if the specified array or function is null1451* @since 1.81452*/1453public static void parallelPrefix(long[] array, int fromIndex,1454int toIndex, LongBinaryOperator op) {1455Objects.requireNonNull(op);1456rangeCheck(array.length, fromIndex, toIndex);1457if (fromIndex < toIndex)1458new ArrayPrefixHelpers.LongCumulateTask1459(null, op, array, fromIndex, toIndex).invoke();1460}14611462/**1463* Cumulates, in parallel, each element of the given array in place,1464* using the supplied function. For example if the array initially1465* holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition,1466* then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}.1467* Parallel prefix computation is usually more efficient than1468* sequential loops for large arrays.1469*1470* <p> Because floating-point operations may not be strictly associative,1471* the returned result may not be identical to the value that would be1472* obtained if the operation was performed sequentially.1473*1474* @param array the array, which is modified in-place by this method1475* @param op a side-effect-free function to perform the cumulation1476* @throws NullPointerException if the specified array or function is null1477* @since 1.81478*/1479public static void parallelPrefix(double[] array, DoubleBinaryOperator op) {1480Objects.requireNonNull(op);1481if (array.length > 0)1482new ArrayPrefixHelpers.DoubleCumulateTask1483(null, op, array, 0, array.length).invoke();1484}14851486/**1487* Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}1488* for the given subrange of the array.1489*1490* @param array the array1491* @param fromIndex the index of the first element, inclusive1492* @param toIndex the index of the last element, exclusive1493* @param op a side-effect-free, associative function to perform the1494* cumulation1495* @throws IllegalArgumentException if {@code fromIndex > toIndex}1496* @throws ArrayIndexOutOfBoundsException1497* if {@code fromIndex < 0} or {@code toIndex > array.length}1498* @throws NullPointerException if the specified array or function is null1499* @since 1.81500*/1501public static void parallelPrefix(double[] array, int fromIndex,1502int toIndex, DoubleBinaryOperator op) {1503Objects.requireNonNull(op);1504rangeCheck(array.length, fromIndex, toIndex);1505if (fromIndex < toIndex)1506new ArrayPrefixHelpers.DoubleCumulateTask1507(null, op, array, fromIndex, toIndex).invoke();1508}15091510/**1511* Cumulates, in parallel, each element of the given array in place,1512* using the supplied function. For example if the array initially1513* holds {@code [2, 1, 0, 3]} and the operation performs addition,1514* then upon return the array holds {@code [2, 3, 3, 6]}.1515* Parallel prefix computation is usually more efficient than1516* sequential loops for large arrays.1517*1518* @param array the array, which is modified in-place by this method1519* @param op a side-effect-free, associative function to perform the1520* cumulation1521* @throws NullPointerException if the specified array or function is null1522* @since 1.81523*/1524public static void parallelPrefix(int[] array, IntBinaryOperator op) {1525Objects.requireNonNull(op);1526if (array.length > 0)1527new ArrayPrefixHelpers.IntCumulateTask1528(null, op, array, 0, array.length).invoke();1529}15301531/**1532* Performs {@link #parallelPrefix(int[], IntBinaryOperator)}1533* for the given subrange of the array.1534*1535* @param array the array1536* @param fromIndex the index of the first element, inclusive1537* @param toIndex the index of the last element, exclusive1538* @param op a side-effect-free, associative function to perform the1539* cumulation1540* @throws IllegalArgumentException if {@code fromIndex > toIndex}1541* @throws ArrayIndexOutOfBoundsException1542* if {@code fromIndex < 0} or {@code toIndex > array.length}1543* @throws NullPointerException if the specified array or function is null1544* @since 1.81545*/1546public static void parallelPrefix(int[] array, int fromIndex,1547int toIndex, IntBinaryOperator op) {1548Objects.requireNonNull(op);1549rangeCheck(array.length, fromIndex, toIndex);1550if (fromIndex < toIndex)1551new ArrayPrefixHelpers.IntCumulateTask1552(null, op, array, fromIndex, toIndex).invoke();1553}15541555// Searching15561557/**1558* Searches the specified array of longs for the specified value using the1559* binary search algorithm. The array must be sorted (as1560* by the {@link #sort(long[])} method) prior to making this call. If it1561* is not sorted, the results are undefined. If the array contains1562* multiple elements with the specified value, there is no guarantee which1563* one will be found.1564*1565* @param a the array to be searched1566* @param key the value to be searched for1567* @return index of the search key, if it is contained in the array;1568* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1569* <i>insertion point</i> is defined as the point at which the1570* key would be inserted into the array: the index of the first1571* element greater than the key, or {@code a.length} if all1572* elements in the array are less than the specified key. Note1573* that this guarantees that the return value will be >= 0 if1574* and only if the key is found.1575*/1576public static int binarySearch(long[] a, long key) {1577return binarySearch0(a, 0, a.length, key);1578}15791580/**1581* Searches a range of1582* the specified array of longs for the specified value using the1583* binary search algorithm.1584* The range must be sorted (as1585* by the {@link #sort(long[], int, int)} method)1586* prior to making this call. If it1587* is not sorted, the results are undefined. If the range contains1588* multiple elements with the specified value, there is no guarantee which1589* one will be found.1590*1591* @param a the array to be searched1592* @param fromIndex the index of the first element (inclusive) to be1593* searched1594* @param toIndex the index of the last element (exclusive) to be searched1595* @param key the value to be searched for1596* @return index of the search key, if it is contained in the array1597* within the specified range;1598* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1599* <i>insertion point</i> is defined as the point at which the1600* key would be inserted into the array: the index of the first1601* element in the range greater than the key,1602* or {@code toIndex} if all1603* elements in the range are less than the specified key. Note1604* that this guarantees that the return value will be >= 0 if1605* and only if the key is found.1606* @throws IllegalArgumentException1607* if {@code fromIndex > toIndex}1608* @throws ArrayIndexOutOfBoundsException1609* if {@code fromIndex < 0 or toIndex > a.length}1610* @since 1.61611*/1612public static int binarySearch(long[] a, int fromIndex, int toIndex,1613long key) {1614rangeCheck(a.length, fromIndex, toIndex);1615return binarySearch0(a, fromIndex, toIndex, key);1616}16171618// Like public version, but without range checks.1619private static int binarySearch0(long[] a, int fromIndex, int toIndex,1620long key) {1621int low = fromIndex;1622int high = toIndex - 1;16231624while (low <= high) {1625int mid = (low + high) >>> 1;1626long midVal = a[mid];16271628if (midVal < key)1629low = mid + 1;1630else if (midVal > key)1631high = mid - 1;1632else1633return mid; // key found1634}1635return -(low + 1); // key not found.1636}16371638/**1639* Searches the specified array of ints for the specified value using the1640* binary search algorithm. The array must be sorted (as1641* by the {@link #sort(int[])} method) prior to making this call. If it1642* is not sorted, the results are undefined. If the array contains1643* multiple elements with the specified value, there is no guarantee which1644* one will be found.1645*1646* @param a the array to be searched1647* @param key the value to be searched for1648* @return index of the search key, if it is contained in the array;1649* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1650* <i>insertion point</i> is defined as the point at which the1651* key would be inserted into the array: the index of the first1652* element greater than the key, or {@code a.length} if all1653* elements in the array are less than the specified key. Note1654* that this guarantees that the return value will be >= 0 if1655* and only if the key is found.1656*/1657public static int binarySearch(int[] a, int key) {1658return binarySearch0(a, 0, a.length, key);1659}16601661/**1662* Searches a range of1663* the specified array of ints for the specified value using the1664* binary search algorithm.1665* The range must be sorted (as1666* by the {@link #sort(int[], int, int)} method)1667* prior to making this call. If it1668* is not sorted, the results are undefined. If the range contains1669* multiple elements with the specified value, there is no guarantee which1670* one will be found.1671*1672* @param a the array to be searched1673* @param fromIndex the index of the first element (inclusive) to be1674* searched1675* @param toIndex the index of the last element (exclusive) to be searched1676* @param key the value to be searched for1677* @return index of the search key, if it is contained in the array1678* within the specified range;1679* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1680* <i>insertion point</i> is defined as the point at which the1681* key would be inserted into the array: the index of the first1682* element in the range greater than the key,1683* or {@code toIndex} if all1684* elements in the range are less than the specified key. Note1685* that this guarantees that the return value will be >= 0 if1686* and only if the key is found.1687* @throws IllegalArgumentException1688* if {@code fromIndex > toIndex}1689* @throws ArrayIndexOutOfBoundsException1690* if {@code fromIndex < 0 or toIndex > a.length}1691* @since 1.61692*/1693public static int binarySearch(int[] a, int fromIndex, int toIndex,1694int key) {1695rangeCheck(a.length, fromIndex, toIndex);1696return binarySearch0(a, fromIndex, toIndex, key);1697}16981699// Like public version, but without range checks.1700private static int binarySearch0(int[] a, int fromIndex, int toIndex,1701int key) {1702int low = fromIndex;1703int high = toIndex - 1;17041705while (low <= high) {1706int mid = (low + high) >>> 1;1707int midVal = a[mid];17081709if (midVal < key)1710low = mid + 1;1711else if (midVal > key)1712high = mid - 1;1713else1714return mid; // key found1715}1716return -(low + 1); // key not found.1717}17181719/**1720* Searches the specified array of shorts for the specified value using1721* the binary search algorithm. The array must be sorted1722* (as by the {@link #sort(short[])} method) prior to making this call. If1723* it is not sorted, the results are undefined. If the array contains1724* multiple elements with the specified value, there is no guarantee which1725* one will be found.1726*1727* @param a the array to be searched1728* @param key the value to be searched for1729* @return index of the search key, if it is contained in the array;1730* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1731* <i>insertion point</i> is defined as the point at which the1732* key would be inserted into the array: the index of the first1733* element greater than the key, or {@code a.length} if all1734* elements in the array are less than the specified key. Note1735* that this guarantees that the return value will be >= 0 if1736* and only if the key is found.1737*/1738public static int binarySearch(short[] a, short key) {1739return binarySearch0(a, 0, a.length, key);1740}17411742/**1743* Searches a range of1744* the specified array of shorts for the specified value using1745* the binary search algorithm.1746* The range must be sorted1747* (as by the {@link #sort(short[], int, int)} method)1748* prior to making this call. If1749* it is not sorted, the results are undefined. If the range contains1750* multiple elements with the specified value, there is no guarantee which1751* one will be found.1752*1753* @param a the array to be searched1754* @param fromIndex the index of the first element (inclusive) to be1755* searched1756* @param toIndex the index of the last element (exclusive) to be searched1757* @param key the value to be searched for1758* @return index of the search key, if it is contained in the array1759* within the specified range;1760* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1761* <i>insertion point</i> is defined as the point at which the1762* key would be inserted into the array: the index of the first1763* element in the range greater than the key,1764* or {@code toIndex} if all1765* elements in the range are less than the specified key. Note1766* that this guarantees that the return value will be >= 0 if1767* and only if the key is found.1768* @throws IllegalArgumentException1769* if {@code fromIndex > toIndex}1770* @throws ArrayIndexOutOfBoundsException1771* if {@code fromIndex < 0 or toIndex > a.length}1772* @since 1.61773*/1774public static int binarySearch(short[] a, int fromIndex, int toIndex,1775short key) {1776rangeCheck(a.length, fromIndex, toIndex);1777return binarySearch0(a, fromIndex, toIndex, key);1778}17791780// Like public version, but without range checks.1781private static int binarySearch0(short[] a, int fromIndex, int toIndex,1782short key) {1783int low = fromIndex;1784int high = toIndex - 1;17851786while (low <= high) {1787int mid = (low + high) >>> 1;1788short midVal = a[mid];17891790if (midVal < key)1791low = mid + 1;1792else if (midVal > key)1793high = mid - 1;1794else1795return mid; // key found1796}1797return -(low + 1); // key not found.1798}17991800/**1801* Searches the specified array of chars for the specified value using the1802* binary search algorithm. The array must be sorted (as1803* by the {@link #sort(char[])} method) prior to making this call. If it1804* is not sorted, the results are undefined. If the array contains1805* multiple elements with the specified value, there is no guarantee which1806* one will be found.1807*1808* @param a the array to be searched1809* @param key the value to be searched for1810* @return index of the search key, if it is contained in the array;1811* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1812* <i>insertion point</i> is defined as the point at which the1813* key would be inserted into the array: the index of the first1814* element greater than the key, or {@code a.length} if all1815* elements in the array are less than the specified key. Note1816* that this guarantees that the return value will be >= 0 if1817* and only if the key is found.1818*/1819public static int binarySearch(char[] a, char key) {1820return binarySearch0(a, 0, a.length, key);1821}18221823/**1824* Searches a range of1825* the specified array of chars for the specified value using the1826* binary search algorithm.1827* The range must be sorted (as1828* by the {@link #sort(char[], int, int)} method)1829* prior to making this call. If it1830* is not sorted, the results are undefined. If the range contains1831* multiple elements with the specified value, there is no guarantee which1832* one will be found.1833*1834* @param a the array to be searched1835* @param fromIndex the index of the first element (inclusive) to be1836* searched1837* @param toIndex the index of the last element (exclusive) to be searched1838* @param key the value to be searched for1839* @return index of the search key, if it is contained in the array1840* within the specified range;1841* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1842* <i>insertion point</i> is defined as the point at which the1843* key would be inserted into the array: the index of the first1844* element in the range greater than the key,1845* or {@code toIndex} if all1846* elements in the range are less than the specified key. Note1847* that this guarantees that the return value will be >= 0 if1848* and only if the key is found.1849* @throws IllegalArgumentException1850* if {@code fromIndex > toIndex}1851* @throws ArrayIndexOutOfBoundsException1852* if {@code fromIndex < 0 or toIndex > a.length}1853* @since 1.61854*/1855public static int binarySearch(char[] a, int fromIndex, int toIndex,1856char key) {1857rangeCheck(a.length, fromIndex, toIndex);1858return binarySearch0(a, fromIndex, toIndex, key);1859}18601861// Like public version, but without range checks.1862private static int binarySearch0(char[] a, int fromIndex, int toIndex,1863char key) {1864int low = fromIndex;1865int high = toIndex - 1;18661867while (low <= high) {1868int mid = (low + high) >>> 1;1869char midVal = a[mid];18701871if (midVal < key)1872low = mid + 1;1873else if (midVal > key)1874high = mid - 1;1875else1876return mid; // key found1877}1878return -(low + 1); // key not found.1879}18801881/**1882* Searches the specified array of bytes for the specified value using the1883* binary search algorithm. The array must be sorted (as1884* by the {@link #sort(byte[])} method) prior to making this call. If it1885* is not sorted, the results are undefined. If the array contains1886* multiple elements with the specified value, there is no guarantee which1887* one will be found.1888*1889* @param a the array to be searched1890* @param key the value to be searched for1891* @return index of the search key, if it is contained in the array;1892* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1893* <i>insertion point</i> is defined as the point at which the1894* key would be inserted into the array: the index of the first1895* element greater than the key, or {@code a.length} if all1896* elements in the array are less than the specified key. Note1897* that this guarantees that the return value will be >= 0 if1898* and only if the key is found.1899*/1900public static int binarySearch(byte[] a, byte key) {1901return binarySearch0(a, 0, a.length, key);1902}19031904/**1905* Searches a range of1906* the specified array of bytes for the specified value using the1907* binary search algorithm.1908* The range must be sorted (as1909* by the {@link #sort(byte[], int, int)} method)1910* prior to making this call. If it1911* is not sorted, the results are undefined. If the range contains1912* multiple elements with the specified value, there is no guarantee which1913* one will be found.1914*1915* @param a the array to be searched1916* @param fromIndex the index of the first element (inclusive) to be1917* searched1918* @param toIndex the index of the last element (exclusive) to be searched1919* @param key the value to be searched for1920* @return index of the search key, if it is contained in the array1921* within the specified range;1922* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1923* <i>insertion point</i> is defined as the point at which the1924* key would be inserted into the array: the index of the first1925* element in the range greater than the key,1926* or {@code toIndex} if all1927* elements in the range are less than the specified key. Note1928* that this guarantees that the return value will be >= 0 if1929* and only if the key is found.1930* @throws IllegalArgumentException1931* if {@code fromIndex > toIndex}1932* @throws ArrayIndexOutOfBoundsException1933* if {@code fromIndex < 0 or toIndex > a.length}1934* @since 1.61935*/1936public static int binarySearch(byte[] a, int fromIndex, int toIndex,1937byte key) {1938rangeCheck(a.length, fromIndex, toIndex);1939return binarySearch0(a, fromIndex, toIndex, key);1940}19411942// Like public version, but without range checks.1943private static int binarySearch0(byte[] a, int fromIndex, int toIndex,1944byte key) {1945int low = fromIndex;1946int high = toIndex - 1;19471948while (low <= high) {1949int mid = (low + high) >>> 1;1950byte midVal = a[mid];19511952if (midVal < key)1953low = mid + 1;1954else if (midVal > key)1955high = mid - 1;1956else1957return mid; // key found1958}1959return -(low + 1); // key not found.1960}19611962/**1963* Searches the specified array of doubles for the specified value using1964* the binary search algorithm. The array must be sorted1965* (as by the {@link #sort(double[])} method) prior to making this call.1966* If it is not sorted, the results are undefined. If the array contains1967* multiple elements with the specified value, there is no guarantee which1968* one will be found. This method considers all NaN values to be1969* equivalent and equal.1970*1971* @param a the array to be searched1972* @param key the value to be searched for1973* @return index of the search key, if it is contained in the array;1974* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The1975* <i>insertion point</i> is defined as the point at which the1976* key would be inserted into the array: the index of the first1977* element greater than the key, or {@code a.length} if all1978* elements in the array are less than the specified key. Note1979* that this guarantees that the return value will be >= 0 if1980* and only if the key is found.1981*/1982public static int binarySearch(double[] a, double key) {1983return binarySearch0(a, 0, a.length, key);1984}19851986/**1987* Searches a range of1988* the specified array of doubles for the specified value using1989* the binary search algorithm.1990* The range must be sorted1991* (as by the {@link #sort(double[], int, int)} method)1992* prior to making this call.1993* If it is not sorted, the results are undefined. If the range contains1994* multiple elements with the specified value, there is no guarantee which1995* one will be found. This method considers all NaN values to be1996* equivalent and equal.1997*1998* @param a the array to be searched1999* @param fromIndex the index of the first element (inclusive) to be2000* searched2001* @param toIndex the index of the last element (exclusive) to be searched2002* @param key the value to be searched for2003* @return index of the search key, if it is contained in the array2004* within the specified range;2005* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2006* <i>insertion point</i> is defined as the point at which the2007* key would be inserted into the array: the index of the first2008* element in the range greater than the key,2009* or {@code toIndex} if all2010* elements in the range are less than the specified key. Note2011* that this guarantees that the return value will be >= 0 if2012* and only if the key is found.2013* @throws IllegalArgumentException2014* if {@code fromIndex > toIndex}2015* @throws ArrayIndexOutOfBoundsException2016* if {@code fromIndex < 0 or toIndex > a.length}2017* @since 1.62018*/2019public static int binarySearch(double[] a, int fromIndex, int toIndex,2020double key) {2021rangeCheck(a.length, fromIndex, toIndex);2022return binarySearch0(a, fromIndex, toIndex, key);2023}20242025// Like public version, but without range checks.2026private static int binarySearch0(double[] a, int fromIndex, int toIndex,2027double key) {2028int low = fromIndex;2029int high = toIndex - 1;20302031while (low <= high) {2032int mid = (low + high) >>> 1;2033double midVal = a[mid];20342035if (midVal < key)2036low = mid + 1; // Neither val is NaN, thisVal is smaller2037else if (midVal > key)2038high = mid - 1; // Neither val is NaN, thisVal is larger2039else {2040long midBits = Double.doubleToLongBits(midVal);2041long keyBits = Double.doubleToLongBits(key);2042if (midBits == keyBits) // Values are equal2043return mid; // Key found2044else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)2045low = mid + 1;2046else // (0.0, -0.0) or (NaN, !NaN)2047high = mid - 1;2048}2049}2050return -(low + 1); // key not found.2051}20522053/**2054* Searches the specified array of floats for the specified value using2055* the binary search algorithm. The array must be sorted2056* (as by the {@link #sort(float[])} method) prior to making this call. If2057* it is not sorted, the results are undefined. If the array contains2058* multiple elements with the specified value, there is no guarantee which2059* one will be found. This method considers all NaN values to be2060* equivalent and equal.2061*2062* @param a the array to be searched2063* @param key the value to be searched for2064* @return index of the search key, if it is contained in the array;2065* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2066* <i>insertion point</i> is defined as the point at which the2067* key would be inserted into the array: the index of the first2068* element greater than the key, or {@code a.length} if all2069* elements in the array are less than the specified key. Note2070* that this guarantees that the return value will be >= 0 if2071* and only if the key is found.2072*/2073public static int binarySearch(float[] a, float key) {2074return binarySearch0(a, 0, a.length, key);2075}20762077/**2078* Searches a range of2079* the specified array of floats for the specified value using2080* the binary search algorithm.2081* The range must be sorted2082* (as by the {@link #sort(float[], int, int)} method)2083* prior to making this call. If2084* it is not sorted, the results are undefined. If the range contains2085* multiple elements with the specified value, there is no guarantee which2086* one will be found. This method considers all NaN values to be2087* equivalent and equal.2088*2089* @param a the array to be searched2090* @param fromIndex the index of the first element (inclusive) to be2091* searched2092* @param toIndex the index of the last element (exclusive) to be searched2093* @param key the value to be searched for2094* @return index of the search key, if it is contained in the array2095* within the specified range;2096* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2097* <i>insertion point</i> is defined as the point at which the2098* key would be inserted into the array: the index of the first2099* element in the range greater than the key,2100* or {@code toIndex} if all2101* elements in the range are less than the specified key. Note2102* that this guarantees that the return value will be >= 0 if2103* and only if the key is found.2104* @throws IllegalArgumentException2105* if {@code fromIndex > toIndex}2106* @throws ArrayIndexOutOfBoundsException2107* if {@code fromIndex < 0 or toIndex > a.length}2108* @since 1.62109*/2110public static int binarySearch(float[] a, int fromIndex, int toIndex,2111float key) {2112rangeCheck(a.length, fromIndex, toIndex);2113return binarySearch0(a, fromIndex, toIndex, key);2114}21152116// Like public version, but without range checks.2117private static int binarySearch0(float[] a, int fromIndex, int toIndex,2118float key) {2119int low = fromIndex;2120int high = toIndex - 1;21212122while (low <= high) {2123int mid = (low + high) >>> 1;2124float midVal = a[mid];21252126if (midVal < key)2127low = mid + 1; // Neither val is NaN, thisVal is smaller2128else if (midVal > key)2129high = mid - 1; // Neither val is NaN, thisVal is larger2130else {2131int midBits = Float.floatToIntBits(midVal);2132int keyBits = Float.floatToIntBits(key);2133if (midBits == keyBits) // Values are equal2134return mid; // Key found2135else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)2136low = mid + 1;2137else // (0.0, -0.0) or (NaN, !NaN)2138high = mid - 1;2139}2140}2141return -(low + 1); // key not found.2142}21432144/**2145* Searches the specified array for the specified object using the binary2146* search algorithm. The array must be sorted into ascending order2147* according to the2148* {@linkplain Comparable natural ordering}2149* of its elements (as by the2150* {@link #sort(Object[])} method) prior to making this call.2151* If it is not sorted, the results are undefined.2152* (If the array contains elements that are not mutually comparable (for2153* example, strings and integers), it <i>cannot</i> be sorted according2154* to the natural ordering of its elements, hence results are undefined.)2155* If the array contains multiple2156* elements equal to the specified object, there is no guarantee which2157* one will be found.2158*2159* @param a the array to be searched2160* @param key the value to be searched for2161* @return index of the search key, if it is contained in the array;2162* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2163* <i>insertion point</i> is defined as the point at which the2164* key would be inserted into the array: the index of the first2165* element greater than the key, or {@code a.length} if all2166* elements in the array are less than the specified key. Note2167* that this guarantees that the return value will be >= 0 if2168* and only if the key is found.2169* @throws ClassCastException if the search key is not comparable to the2170* elements of the array.2171*/2172public static int binarySearch(Object[] a, Object key) {2173return binarySearch0(a, 0, a.length, key);2174}21752176/**2177* Searches a range of2178* the specified array for the specified object using the binary2179* search algorithm.2180* The range must be sorted into ascending order2181* according to the2182* {@linkplain Comparable natural ordering}2183* of its elements (as by the2184* {@link #sort(Object[], int, int)} method) prior to making this2185* call. If it is not sorted, the results are undefined.2186* (If the range contains elements that are not mutually comparable (for2187* example, strings and integers), it <i>cannot</i> be sorted according2188* to the natural ordering of its elements, hence results are undefined.)2189* If the range contains multiple2190* elements equal to the specified object, there is no guarantee which2191* one will be found.2192*2193* @param a the array to be searched2194* @param fromIndex the index of the first element (inclusive) to be2195* searched2196* @param toIndex the index of the last element (exclusive) to be searched2197* @param key the value to be searched for2198* @return index of the search key, if it is contained in the array2199* within the specified range;2200* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2201* <i>insertion point</i> is defined as the point at which the2202* key would be inserted into the array: the index of the first2203* element in the range greater than the key,2204* or {@code toIndex} if all2205* elements in the range are less than the specified key. Note2206* that this guarantees that the return value will be >= 0 if2207* and only if the key is found.2208* @throws ClassCastException if the search key is not comparable to the2209* elements of the array within the specified range.2210* @throws IllegalArgumentException2211* if {@code fromIndex > toIndex}2212* @throws ArrayIndexOutOfBoundsException2213* if {@code fromIndex < 0 or toIndex > a.length}2214* @since 1.62215*/2216public static int binarySearch(Object[] a, int fromIndex, int toIndex,2217Object key) {2218rangeCheck(a.length, fromIndex, toIndex);2219return binarySearch0(a, fromIndex, toIndex, key);2220}22212222// Like public version, but without range checks.2223private static int binarySearch0(Object[] a, int fromIndex, int toIndex,2224Object key) {2225int low = fromIndex;2226int high = toIndex - 1;22272228while (low <= high) {2229int mid = (low + high) >>> 1;2230@SuppressWarnings("rawtypes")2231Comparable midVal = (Comparable)a[mid];2232@SuppressWarnings("unchecked")2233int cmp = midVal.compareTo(key);22342235if (cmp < 0)2236low = mid + 1;2237else if (cmp > 0)2238high = mid - 1;2239else2240return mid; // key found2241}2242return -(low + 1); // key not found.2243}22442245/**2246* Searches the specified array for the specified object using the binary2247* search algorithm. The array must be sorted into ascending order2248* according to the specified comparator (as by the2249* {@link #sort(Object[], Comparator) sort(T[], Comparator)}2250* method) prior to making this call. If it is2251* not sorted, the results are undefined.2252* If the array contains multiple2253* elements equal to the specified object, there is no guarantee which one2254* will be found.2255*2256* @param <T> the class of the objects in the array2257* @param a the array to be searched2258* @param key the value to be searched for2259* @param c the comparator by which the array is ordered. A2260* {@code null} value indicates that the elements'2261* {@linkplain Comparable natural ordering} should be used.2262* @return index of the search key, if it is contained in the array;2263* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2264* <i>insertion point</i> is defined as the point at which the2265* key would be inserted into the array: the index of the first2266* element greater than the key, or {@code a.length} if all2267* elements in the array are less than the specified key. Note2268* that this guarantees that the return value will be >= 0 if2269* and only if the key is found.2270* @throws ClassCastException if the array contains elements that are not2271* <i>mutually comparable</i> using the specified comparator,2272* or the search key is not comparable to the2273* elements of the array using this comparator.2274*/2275public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {2276return binarySearch0(a, 0, a.length, key, c);2277}22782279/**2280* Searches a range of2281* the specified array for the specified object using the binary2282* search algorithm.2283* The range must be sorted into ascending order2284* according to the specified comparator (as by the2285* {@link #sort(Object[], int, int, Comparator)2286* sort(T[], int, int, Comparator)}2287* method) prior to making this call.2288* If it is not sorted, the results are undefined.2289* If the range contains multiple elements equal to the specified object,2290* there is no guarantee which one will be found.2291*2292* @param <T> the class of the objects in the array2293* @param a the array to be searched2294* @param fromIndex the index of the first element (inclusive) to be2295* searched2296* @param toIndex the index of the last element (exclusive) to be searched2297* @param key the value to be searched for2298* @param c the comparator by which the array is ordered. A2299* {@code null} value indicates that the elements'2300* {@linkplain Comparable natural ordering} should be used.2301* @return index of the search key, if it is contained in the array2302* within the specified range;2303* otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The2304* <i>insertion point</i> is defined as the point at which the2305* key would be inserted into the array: the index of the first2306* element in the range greater than the key,2307* or {@code toIndex} if all2308* elements in the range are less than the specified key. Note2309* that this guarantees that the return value will be >= 0 if2310* and only if the key is found.2311* @throws ClassCastException if the range contains elements that are not2312* <i>mutually comparable</i> using the specified comparator,2313* or the search key is not comparable to the2314* elements in the range using this comparator.2315* @throws IllegalArgumentException2316* if {@code fromIndex > toIndex}2317* @throws ArrayIndexOutOfBoundsException2318* if {@code fromIndex < 0 or toIndex > a.length}2319* @since 1.62320*/2321public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,2322T key, Comparator<? super T> c) {2323rangeCheck(a.length, fromIndex, toIndex);2324return binarySearch0(a, fromIndex, toIndex, key, c);2325}23262327// Like public version, but without range checks.2328private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,2329T key, Comparator<? super T> c) {2330if (c == null) {2331return binarySearch0(a, fromIndex, toIndex, key);2332}2333int low = fromIndex;2334int high = toIndex - 1;23352336while (low <= high) {2337int mid = (low + high) >>> 1;2338T midVal = a[mid];2339int cmp = c.compare(midVal, key);2340if (cmp < 0)2341low = mid + 1;2342else if (cmp > 0)2343high = mid - 1;2344else2345return mid; // key found2346}2347return -(low + 1); // key not found.2348}23492350// Equality Testing23512352/**2353* Returns {@code true} if the two specified arrays of longs are2354* <i>equal</i> to one another. Two arrays are considered equal if both2355* arrays contain the same number of elements, and all corresponding pairs2356* of elements in the two arrays are equal. In other words, two arrays2357* are equal if they contain the same elements in the same order. Also,2358* two array references are considered equal if both are {@code null}.2359*2360* @param a one array to be tested for equality2361* @param a2 the other array to be tested for equality2362* @return {@code true} if the two arrays are equal2363*/2364public static boolean equals(long[] a, long[] a2) {2365if (a==a2)2366return true;2367if (a==null || a2==null)2368return false;23692370int length = a.length;2371if (a2.length != length)2372return false;23732374return ArraysSupport.mismatch(a, a2, length) < 0;2375}23762377/**2378* Returns true if the two specified arrays of longs, over the specified2379* ranges, are <i>equal</i> to one another.2380*2381* <p>Two arrays are considered equal if the number of elements covered by2382* each range is the same, and all corresponding pairs of elements over the2383* specified ranges in the two arrays are equal. In other words, two arrays2384* are equal if they contain, over the specified ranges, the same elements2385* in the same order.2386*2387* @param a the first array to be tested for equality2388* @param aFromIndex the index (inclusive) of the first element in the2389* first array to be tested2390* @param aToIndex the index (exclusive) of the last element in the2391* first array to be tested2392* @param b the second array to be tested for equality2393* @param bFromIndex the index (inclusive) of the first element in the2394* second array to be tested2395* @param bToIndex the index (exclusive) of the last element in the2396* second array to be tested2397* @return {@code true} if the two arrays, over the specified ranges, are2398* equal2399* @throws IllegalArgumentException2400* if {@code aFromIndex > aToIndex} or2401* if {@code bFromIndex > bToIndex}2402* @throws ArrayIndexOutOfBoundsException2403* if {@code aFromIndex < 0 or aToIndex > a.length} or2404* if {@code bFromIndex < 0 or bToIndex > b.length}2405* @throws NullPointerException2406* if either array is {@code null}2407* @since 92408*/2409public static boolean equals(long[] a, int aFromIndex, int aToIndex,2410long[] b, int bFromIndex, int bToIndex) {2411rangeCheck(a.length, aFromIndex, aToIndex);2412rangeCheck(b.length, bFromIndex, bToIndex);24132414int aLength = aToIndex - aFromIndex;2415int bLength = bToIndex - bFromIndex;2416if (aLength != bLength)2417return false;24182419return ArraysSupport.mismatch(a, aFromIndex,2420b, bFromIndex,2421aLength) < 0;2422}24232424/**2425* Returns {@code true} if the two specified arrays of ints are2426* <i>equal</i> to one another. Two arrays are considered equal if both2427* arrays contain the same number of elements, and all corresponding pairs2428* of elements in the two arrays are equal. In other words, two arrays2429* are equal if they contain the same elements in the same order. Also,2430* two array references are considered equal if both are {@code null}.2431*2432* @param a one array to be tested for equality2433* @param a2 the other array to be tested for equality2434* @return {@code true} if the two arrays are equal2435*/2436public static boolean equals(int[] a, int[] a2) {2437if (a==a2)2438return true;2439if (a==null || a2==null)2440return false;24412442int length = a.length;2443if (a2.length != length)2444return false;24452446return ArraysSupport.mismatch(a, a2, length) < 0;2447}24482449/**2450* Returns true if the two specified arrays of ints, over the specified2451* ranges, are <i>equal</i> to one another.2452*2453* <p>Two arrays are considered equal if the number of elements covered by2454* each range is the same, and all corresponding pairs of elements over the2455* specified ranges in the two arrays are equal. In other words, two arrays2456* are equal if they contain, over the specified ranges, the same elements2457* in the same order.2458*2459* @param a the first array to be tested for equality2460* @param aFromIndex the index (inclusive) of the first element in the2461* first array to be tested2462* @param aToIndex the index (exclusive) of the last element in the2463* first array to be tested2464* @param b the second array to be tested for equality2465* @param bFromIndex the index (inclusive) of the first element in the2466* second array to be tested2467* @param bToIndex the index (exclusive) of the last element in the2468* second array to be tested2469* @return {@code true} if the two arrays, over the specified ranges, are2470* equal2471* @throws IllegalArgumentException2472* if {@code aFromIndex > aToIndex} or2473* if {@code bFromIndex > bToIndex}2474* @throws ArrayIndexOutOfBoundsException2475* if {@code aFromIndex < 0 or aToIndex > a.length} or2476* if {@code bFromIndex < 0 or bToIndex > b.length}2477* @throws NullPointerException2478* if either array is {@code null}2479* @since 92480*/2481public static boolean equals(int[] a, int aFromIndex, int aToIndex,2482int[] b, int bFromIndex, int bToIndex) {2483rangeCheck(a.length, aFromIndex, aToIndex);2484rangeCheck(b.length, bFromIndex, bToIndex);24852486int aLength = aToIndex - aFromIndex;2487int bLength = bToIndex - bFromIndex;2488if (aLength != bLength)2489return false;24902491return ArraysSupport.mismatch(a, aFromIndex,2492b, bFromIndex,2493aLength) < 0;2494}24952496/**2497* Returns {@code true} if the two specified arrays of shorts are2498* <i>equal</i> to one another. Two arrays are considered equal if both2499* arrays contain the same number of elements, and all corresponding pairs2500* of elements in the two arrays are equal. In other words, two arrays2501* are equal if they contain the same elements in the same order. Also,2502* two array references are considered equal if both are {@code null}.2503*2504* @param a one array to be tested for equality2505* @param a2 the other array to be tested for equality2506* @return {@code true} if the two arrays are equal2507*/2508public static boolean equals(short[] a, short a2[]) {2509if (a==a2)2510return true;2511if (a==null || a2==null)2512return false;25132514int length = a.length;2515if (a2.length != length)2516return false;25172518return ArraysSupport.mismatch(a, a2, length) < 0;2519}25202521/**2522* Returns true if the two specified arrays of shorts, over the specified2523* ranges, are <i>equal</i> to one another.2524*2525* <p>Two arrays are considered equal if the number of elements covered by2526* each range is the same, and all corresponding pairs of elements over the2527* specified ranges in the two arrays are equal. In other words, two arrays2528* are equal if they contain, over the specified ranges, the same elements2529* in the same order.2530*2531* @param a the first array to be tested for equality2532* @param aFromIndex the index (inclusive) of the first element in the2533* first array to be tested2534* @param aToIndex the index (exclusive) of the last element in the2535* first array to be tested2536* @param b the second array to be tested for equality2537* @param bFromIndex the index (inclusive) of the first element in the2538* second array to be tested2539* @param bToIndex the index (exclusive) of the last element in the2540* second array to be tested2541* @return {@code true} if the two arrays, over the specified ranges, are2542* equal2543* @throws IllegalArgumentException2544* if {@code aFromIndex > aToIndex} or2545* if {@code bFromIndex > bToIndex}2546* @throws ArrayIndexOutOfBoundsException2547* if {@code aFromIndex < 0 or aToIndex > a.length} or2548* if {@code bFromIndex < 0 or bToIndex > b.length}2549* @throws NullPointerException2550* if either array is {@code null}2551* @since 92552*/2553public static boolean equals(short[] a, int aFromIndex, int aToIndex,2554short[] b, int bFromIndex, int bToIndex) {2555rangeCheck(a.length, aFromIndex, aToIndex);2556rangeCheck(b.length, bFromIndex, bToIndex);25572558int aLength = aToIndex - aFromIndex;2559int bLength = bToIndex - bFromIndex;2560if (aLength != bLength)2561return false;25622563return ArraysSupport.mismatch(a, aFromIndex,2564b, bFromIndex,2565aLength) < 0;2566}25672568/**2569* Returns {@code true} if the two specified arrays of chars are2570* <i>equal</i> to one another. Two arrays are considered equal if both2571* arrays contain the same number of elements, and all corresponding pairs2572* of elements in the two arrays are equal. In other words, two arrays2573* are equal if they contain the same elements in the same order. Also,2574* two array references are considered equal if both are {@code null}.2575*2576* @param a one array to be tested for equality2577* @param a2 the other array to be tested for equality2578* @return {@code true} if the two arrays are equal2579*/2580@IntrinsicCandidate2581public static boolean equals(char[] a, char[] a2) {2582if (a==a2)2583return true;2584if (a==null || a2==null)2585return false;25862587int length = a.length;2588if (a2.length != length)2589return false;25902591return ArraysSupport.mismatch(a, a2, length) < 0;2592}25932594/**2595* Returns true if the two specified arrays of chars, over the specified2596* ranges, are <i>equal</i> to one another.2597*2598* <p>Two arrays are considered equal if the number of elements covered by2599* each range is the same, and all corresponding pairs of elements over the2600* specified ranges in the two arrays are equal. In other words, two arrays2601* are equal if they contain, over the specified ranges, the same elements2602* in the same order.2603*2604* @param a the first array to be tested for equality2605* @param aFromIndex the index (inclusive) of the first element in the2606* first array to be tested2607* @param aToIndex the index (exclusive) of the last element in the2608* first array to be tested2609* @param b the second array to be tested for equality2610* @param bFromIndex the index (inclusive) of the first element in the2611* second array to be tested2612* @param bToIndex the index (exclusive) of the last element in the2613* second array to be tested2614* @return {@code true} if the two arrays, over the specified ranges, are2615* equal2616* @throws IllegalArgumentException2617* if {@code aFromIndex > aToIndex} or2618* if {@code bFromIndex > bToIndex}2619* @throws ArrayIndexOutOfBoundsException2620* if {@code aFromIndex < 0 or aToIndex > a.length} or2621* if {@code bFromIndex < 0 or bToIndex > b.length}2622* @throws NullPointerException2623* if either array is {@code null}2624* @since 92625*/2626public static boolean equals(char[] a, int aFromIndex, int aToIndex,2627char[] b, int bFromIndex, int bToIndex) {2628rangeCheck(a.length, aFromIndex, aToIndex);2629rangeCheck(b.length, bFromIndex, bToIndex);26302631int aLength = aToIndex - aFromIndex;2632int bLength = bToIndex - bFromIndex;2633if (aLength != bLength)2634return false;26352636return ArraysSupport.mismatch(a, aFromIndex,2637b, bFromIndex,2638aLength) < 0;2639}26402641/**2642* Returns {@code true} if the two specified arrays of bytes are2643* <i>equal</i> to one another. Two arrays are considered equal if both2644* arrays contain the same number of elements, and all corresponding pairs2645* of elements in the two arrays are equal. In other words, two arrays2646* are equal if they contain the same elements in the same order. Also,2647* two array references are considered equal if both are {@code null}.2648*2649* @param a one array to be tested for equality2650* @param a2 the other array to be tested for equality2651* @return {@code true} if the two arrays are equal2652*/2653@IntrinsicCandidate2654public static boolean equals(byte[] a, byte[] a2) {2655if (a==a2)2656return true;2657if (a==null || a2==null)2658return false;26592660int length = a.length;2661if (a2.length != length)2662return false;26632664return ArraysSupport.mismatch(a, a2, length) < 0;2665}26662667/**2668* Returns true if the two specified arrays of bytes, over the specified2669* ranges, are <i>equal</i> to one another.2670*2671* <p>Two arrays are considered equal if the number of elements covered by2672* each range is the same, and all corresponding pairs of elements over the2673* specified ranges in the two arrays are equal. In other words, two arrays2674* are equal if they contain, over the specified ranges, the same elements2675* in the same order.2676*2677* @param a the first array to be tested for equality2678* @param aFromIndex the index (inclusive) of the first element in the2679* first array to be tested2680* @param aToIndex the index (exclusive) of the last element in the2681* first array to be tested2682* @param b the second array to be tested for equality2683* @param bFromIndex the index (inclusive) of the first element in the2684* second array to be tested2685* @param bToIndex the index (exclusive) of the last element in the2686* second array to be tested2687* @return {@code true} if the two arrays, over the specified ranges, are2688* equal2689* @throws IllegalArgumentException2690* if {@code aFromIndex > aToIndex} or2691* if {@code bFromIndex > bToIndex}2692* @throws ArrayIndexOutOfBoundsException2693* if {@code aFromIndex < 0 or aToIndex > a.length} or2694* if {@code bFromIndex < 0 or bToIndex > b.length}2695* @throws NullPointerException2696* if either array is {@code null}2697* @since 92698*/2699public static boolean equals(byte[] a, int aFromIndex, int aToIndex,2700byte[] b, int bFromIndex, int bToIndex) {2701rangeCheck(a.length, aFromIndex, aToIndex);2702rangeCheck(b.length, bFromIndex, bToIndex);27032704int aLength = aToIndex - aFromIndex;2705int bLength = bToIndex - bFromIndex;2706if (aLength != bLength)2707return false;27082709return ArraysSupport.mismatch(a, aFromIndex,2710b, bFromIndex,2711aLength) < 0;2712}27132714/**2715* Returns {@code true} if the two specified arrays of booleans are2716* <i>equal</i> to one another. Two arrays are considered equal if both2717* arrays contain the same number of elements, and all corresponding pairs2718* of elements in the two arrays are equal. In other words, two arrays2719* are equal if they contain the same elements in the same order. Also,2720* two array references are considered equal if both are {@code null}.2721*2722* @param a one array to be tested for equality2723* @param a2 the other array to be tested for equality2724* @return {@code true} if the two arrays are equal2725*/2726public static boolean equals(boolean[] a, boolean[] a2) {2727if (a==a2)2728return true;2729if (a==null || a2==null)2730return false;27312732int length = a.length;2733if (a2.length != length)2734return false;27352736return ArraysSupport.mismatch(a, a2, length) < 0;2737}27382739/**2740* Returns true if the two specified arrays of booleans, over the specified2741* ranges, are <i>equal</i> to one another.2742*2743* <p>Two arrays are considered equal if the number of elements covered by2744* each range is the same, and all corresponding pairs of elements over the2745* specified ranges in the two arrays are equal. In other words, two arrays2746* are equal if they contain, over the specified ranges, the same elements2747* in the same order.2748*2749* @param a the first array to be tested for equality2750* @param aFromIndex the index (inclusive) of the first element in the2751* first array to be tested2752* @param aToIndex the index (exclusive) of the last element in the2753* first array to be tested2754* @param b the second array to be tested for equality2755* @param bFromIndex the index (inclusive) of the first element in the2756* second array to be tested2757* @param bToIndex the index (exclusive) of the last element in the2758* second array to be tested2759* @return {@code true} if the two arrays, over the specified ranges, are2760* equal2761* @throws IllegalArgumentException2762* if {@code aFromIndex > aToIndex} or2763* if {@code bFromIndex > bToIndex}2764* @throws ArrayIndexOutOfBoundsException2765* if {@code aFromIndex < 0 or aToIndex > a.length} or2766* if {@code bFromIndex < 0 or bToIndex > b.length}2767* @throws NullPointerException2768* if either array is {@code null}2769* @since 92770*/2771public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,2772boolean[] b, int bFromIndex, int bToIndex) {2773rangeCheck(a.length, aFromIndex, aToIndex);2774rangeCheck(b.length, bFromIndex, bToIndex);27752776int aLength = aToIndex - aFromIndex;2777int bLength = bToIndex - bFromIndex;2778if (aLength != bLength)2779return false;27802781return ArraysSupport.mismatch(a, aFromIndex,2782b, bFromIndex,2783aLength) < 0;2784}27852786/**2787* Returns {@code true} if the two specified arrays of doubles are2788* <i>equal</i> to one another. Two arrays are considered equal if both2789* arrays contain the same number of elements, and all corresponding pairs2790* of elements in the two arrays are equal. In other words, two arrays2791* are equal if they contain the same elements in the same order. Also,2792* two array references are considered equal if both are {@code null}.2793*2794* Two doubles {@code d1} and {@code d2} are considered equal if:2795* <pre> {@code new Double(d1).equals(new Double(d2))}</pre>2796* (Unlike the {@code ==} operator, this method considers2797* {@code NaN} equal to itself, and 0.0d unequal to -0.0d.)2798*2799* @param a one array to be tested for equality2800* @param a2 the other array to be tested for equality2801* @return {@code true} if the two arrays are equal2802* @see Double#equals(Object)2803*/2804public static boolean equals(double[] a, double[] a2) {2805if (a==a2)2806return true;2807if (a==null || a2==null)2808return false;28092810int length = a.length;2811if (a2.length != length)2812return false;28132814return ArraysSupport.mismatch(a, a2, length) < 0;2815}28162817/**2818* Returns true if the two specified arrays of doubles, over the specified2819* ranges, are <i>equal</i> to one another.2820*2821* <p>Two arrays are considered equal if the number of elements covered by2822* each range is the same, and all corresponding pairs of elements over the2823* specified ranges in the two arrays are equal. In other words, two arrays2824* are equal if they contain, over the specified ranges, the same elements2825* in the same order.2826*2827* <p>Two doubles {@code d1} and {@code d2} are considered equal if:2828* <pre> {@code new Double(d1).equals(new Double(d2))}</pre>2829* (Unlike the {@code ==} operator, this method considers2830* {@code NaN} equal to itself, and 0.0d unequal to -0.0d.)2831*2832* @param a the first array to be tested for equality2833* @param aFromIndex the index (inclusive) of the first element in the2834* first array to be tested2835* @param aToIndex the index (exclusive) of the last element in the2836* first array to be tested2837* @param b the second array to be tested for equality2838* @param bFromIndex the index (inclusive) of the first element in the2839* second array to be tested2840* @param bToIndex the index (exclusive) of the last element in the2841* second array to be tested2842* @return {@code true} if the two arrays, over the specified ranges, are2843* equal2844* @throws IllegalArgumentException2845* if {@code aFromIndex > aToIndex} or2846* if {@code bFromIndex > bToIndex}2847* @throws ArrayIndexOutOfBoundsException2848* if {@code aFromIndex < 0 or aToIndex > a.length} or2849* if {@code bFromIndex < 0 or bToIndex > b.length}2850* @throws NullPointerException2851* if either array is {@code null}2852* @see Double#equals(Object)2853* @since 92854*/2855public static boolean equals(double[] a, int aFromIndex, int aToIndex,2856double[] b, int bFromIndex, int bToIndex) {2857rangeCheck(a.length, aFromIndex, aToIndex);2858rangeCheck(b.length, bFromIndex, bToIndex);28592860int aLength = aToIndex - aFromIndex;2861int bLength = bToIndex - bFromIndex;2862if (aLength != bLength)2863return false;28642865return ArraysSupport.mismatch(a, aFromIndex,2866b, bFromIndex, aLength) < 0;2867}28682869/**2870* Returns {@code true} if the two specified arrays of floats are2871* <i>equal</i> to one another. Two arrays are considered equal if both2872* arrays contain the same number of elements, and all corresponding pairs2873* of elements in the two arrays are equal. In other words, two arrays2874* are equal if they contain the same elements in the same order. Also,2875* two array references are considered equal if both are {@code null}.2876*2877* Two floats {@code f1} and {@code f2} are considered equal if:2878* <pre> {@code new Float(f1).equals(new Float(f2))}</pre>2879* (Unlike the {@code ==} operator, this method considers2880* {@code NaN} equal to itself, and 0.0f unequal to -0.0f.)2881*2882* @param a one array to be tested for equality2883* @param a2 the other array to be tested for equality2884* @return {@code true} if the two arrays are equal2885* @see Float#equals(Object)2886*/2887public static boolean equals(float[] a, float[] a2) {2888if (a==a2)2889return true;2890if (a==null || a2==null)2891return false;28922893int length = a.length;2894if (a2.length != length)2895return false;28962897return ArraysSupport.mismatch(a, a2, length) < 0;2898}28992900/**2901* Returns true if the two specified arrays of floats, over the specified2902* ranges, are <i>equal</i> to one another.2903*2904* <p>Two arrays are considered equal if the number of elements covered by2905* each range is the same, and all corresponding pairs of elements over the2906* specified ranges in the two arrays are equal. In other words, two arrays2907* are equal if they contain, over the specified ranges, the same elements2908* in the same order.2909*2910* <p>Two floats {@code f1} and {@code f2} are considered equal if:2911* <pre> {@code new Float(f1).equals(new Float(f2))}</pre>2912* (Unlike the {@code ==} operator, this method considers2913* {@code NaN} equal to itself, and 0.0f unequal to -0.0f.)2914*2915* @param a the first array to be tested for equality2916* @param aFromIndex the index (inclusive) of the first element in the2917* first array to be tested2918* @param aToIndex the index (exclusive) of the last element in the2919* first array to be tested2920* @param b the second array to be tested for equality2921* @param bFromIndex the index (inclusive) of the first element in the2922* second array to be tested2923* @param bToIndex the index (exclusive) of the last element in the2924* second array to be tested2925* @return {@code true} if the two arrays, over the specified ranges, are2926* equal2927* @throws IllegalArgumentException2928* if {@code aFromIndex > aToIndex} or2929* if {@code bFromIndex > bToIndex}2930* @throws ArrayIndexOutOfBoundsException2931* if {@code aFromIndex < 0 or aToIndex > a.length} or2932* if {@code bFromIndex < 0 or bToIndex > b.length}2933* @throws NullPointerException2934* if either array is {@code null}2935* @see Float#equals(Object)2936* @since 92937*/2938public static boolean equals(float[] a, int aFromIndex, int aToIndex,2939float[] b, int bFromIndex, int bToIndex) {2940rangeCheck(a.length, aFromIndex, aToIndex);2941rangeCheck(b.length, bFromIndex, bToIndex);29422943int aLength = aToIndex - aFromIndex;2944int bLength = bToIndex - bFromIndex;2945if (aLength != bLength)2946return false;29472948return ArraysSupport.mismatch(a, aFromIndex,2949b, bFromIndex, aLength) < 0;2950}29512952/**2953* Returns {@code true} if the two specified arrays of Objects are2954* <i>equal</i> to one another. The two arrays are considered equal if2955* both arrays contain the same number of elements, and all corresponding2956* pairs of elements in the two arrays are equal. Two objects {@code e1}2957* and {@code e2} are considered <i>equal</i> if2958* {@code Objects.equals(e1, e2)}.2959* In other words, the two arrays are equal if2960* they contain the same elements in the same order. Also, two array2961* references are considered equal if both are {@code null}.2962*2963* @param a one array to be tested for equality2964* @param a2 the other array to be tested for equality2965* @return {@code true} if the two arrays are equal2966*/2967public static boolean equals(Object[] a, Object[] a2) {2968if (a==a2)2969return true;2970if (a==null || a2==null)2971return false;29722973int length = a.length;2974if (a2.length != length)2975return false;29762977for (int i=0; i<length; i++) {2978if (!Objects.equals(a[i], a2[i]))2979return false;2980}29812982return true;2983}29842985/**2986* Returns true if the two specified arrays of Objects, over the specified2987* ranges, are <i>equal</i> to one another.2988*2989* <p>Two arrays are considered equal if the number of elements covered by2990* each range is the same, and all corresponding pairs of elements over the2991* specified ranges in the two arrays are equal. In other words, two arrays2992* are equal if they contain, over the specified ranges, the same elements2993* in the same order.2994*2995* <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if2996* {@code Objects.equals(e1, e2)}.2997*2998* @param a the first array to be tested for equality2999* @param aFromIndex the index (inclusive) of the first element in the3000* first array to be tested3001* @param aToIndex the index (exclusive) of the last element in the3002* first array to be tested3003* @param b the second array to be tested for equality3004* @param bFromIndex the index (inclusive) of the first element in the3005* second array to be tested3006* @param bToIndex the index (exclusive) of the last element in the3007* second array to be tested3008* @return {@code true} if the two arrays, over the specified ranges, are3009* equal3010* @throws IllegalArgumentException3011* if {@code aFromIndex > aToIndex} or3012* if {@code bFromIndex > bToIndex}3013* @throws ArrayIndexOutOfBoundsException3014* if {@code aFromIndex < 0 or aToIndex > a.length} or3015* if {@code bFromIndex < 0 or bToIndex > b.length}3016* @throws NullPointerException3017* if either array is {@code null}3018* @since 93019*/3020public static boolean equals(Object[] a, int aFromIndex, int aToIndex,3021Object[] b, int bFromIndex, int bToIndex) {3022rangeCheck(a.length, aFromIndex, aToIndex);3023rangeCheck(b.length, bFromIndex, bToIndex);30243025int aLength = aToIndex - aFromIndex;3026int bLength = bToIndex - bFromIndex;3027if (aLength != bLength)3028return false;30293030for (int i = 0; i < aLength; i++) {3031if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))3032return false;3033}30343035return true;3036}30373038/**3039* Returns {@code true} if the two specified arrays of Objects are3040* <i>equal</i> to one another.3041*3042* <p>Two arrays are considered equal if both arrays contain the same number3043* of elements, and all corresponding pairs of elements in the two arrays3044* are equal. In other words, the two arrays are equal if they contain the3045* same elements in the same order. Also, two array references are3046* considered equal if both are {@code null}.3047*3048* <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,3049* given the specified comparator, {@code cmp.compare(e1, e2) == 0}.3050*3051* @param a one array to be tested for equality3052* @param a2 the other array to be tested for equality3053* @param cmp the comparator to compare array elements3054* @param <T> the type of array elements3055* @return {@code true} if the two arrays are equal3056* @throws NullPointerException if the comparator is {@code null}3057* @since 93058*/3059public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) {3060Objects.requireNonNull(cmp);3061if (a==a2)3062return true;3063if (a==null || a2==null)3064return false;30653066int length = a.length;3067if (a2.length != length)3068return false;30693070for (int i=0; i<length; i++) {3071if (cmp.compare(a[i], a2[i]) != 0)3072return false;3073}30743075return true;3076}30773078/**3079* Returns true if the two specified arrays of Objects, over the specified3080* ranges, are <i>equal</i> to one another.3081*3082* <p>Two arrays are considered equal if the number of elements covered by3083* each range is the same, and all corresponding pairs of elements over the3084* specified ranges in the two arrays are equal. In other words, two arrays3085* are equal if they contain, over the specified ranges, the same elements3086* in the same order.3087*3088* <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,3089* given the specified comparator, {@code cmp.compare(e1, e2) == 0}.3090*3091* @param a the first array to be tested for equality3092* @param aFromIndex the index (inclusive) of the first element in the3093* first array to be tested3094* @param aToIndex the index (exclusive) of the last element in the3095* first array to be tested3096* @param b the second array to be tested for equality3097* @param bFromIndex the index (inclusive) of the first element in the3098* second array to be tested3099* @param bToIndex the index (exclusive) of the last element in the3100* second array to be tested3101* @param cmp the comparator to compare array elements3102* @param <T> the type of array elements3103* @return {@code true} if the two arrays, over the specified ranges, are3104* equal3105* @throws IllegalArgumentException3106* if {@code aFromIndex > aToIndex} or3107* if {@code bFromIndex > bToIndex}3108* @throws ArrayIndexOutOfBoundsException3109* if {@code aFromIndex < 0 or aToIndex > a.length} or3110* if {@code bFromIndex < 0 or bToIndex > b.length}3111* @throws NullPointerException3112* if either array or the comparator is {@code null}3113* @since 93114*/3115public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,3116T[] b, int bFromIndex, int bToIndex,3117Comparator<? super T> cmp) {3118Objects.requireNonNull(cmp);3119rangeCheck(a.length, aFromIndex, aToIndex);3120rangeCheck(b.length, bFromIndex, bToIndex);31213122int aLength = aToIndex - aFromIndex;3123int bLength = bToIndex - bFromIndex;3124if (aLength != bLength)3125return false;31263127for (int i = 0; i < aLength; i++) {3128if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)3129return false;3130}31313132return true;3133}31343135// Filling31363137/**3138* Assigns the specified long value to each element of the specified array3139* of longs.3140*3141* @param a the array to be filled3142* @param val the value to be stored in all elements of the array3143*/3144public static void fill(long[] a, long val) {3145for (int i = 0, len = a.length; i < len; i++)3146a[i] = val;3147}31483149/**3150* Assigns the specified long value to each element of the specified3151* range of the specified array of longs. The range to be filled3152* extends from index {@code fromIndex}, inclusive, to index3153* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3154* range to be filled is empty.)3155*3156* @param a the array to be filled3157* @param fromIndex the index of the first element (inclusive) to be3158* filled with the specified value3159* @param toIndex the index of the last element (exclusive) to be3160* filled with the specified value3161* @param val the value to be stored in all elements of the array3162* @throws IllegalArgumentException if {@code fromIndex > toIndex}3163* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3164* {@code toIndex > a.length}3165*/3166public static void fill(long[] a, int fromIndex, int toIndex, long val) {3167rangeCheck(a.length, fromIndex, toIndex);3168for (int i = fromIndex; i < toIndex; i++)3169a[i] = val;3170}31713172/**3173* Assigns the specified int value to each element of the specified array3174* of ints.3175*3176* @param a the array to be filled3177* @param val the value to be stored in all elements of the array3178*/3179public static void fill(int[] a, int val) {3180for (int i = 0, len = a.length; i < len; i++)3181a[i] = val;3182}31833184/**3185* Assigns the specified int value to each element of the specified3186* range of the specified array of ints. The range to be filled3187* extends from index {@code fromIndex}, inclusive, to index3188* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3189* range to be filled is empty.)3190*3191* @param a the array to be filled3192* @param fromIndex the index of the first element (inclusive) to be3193* filled with the specified value3194* @param toIndex the index of the last element (exclusive) to be3195* filled with the specified value3196* @param val the value to be stored in all elements of the array3197* @throws IllegalArgumentException if {@code fromIndex > toIndex}3198* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3199* {@code toIndex > a.length}3200*/3201public static void fill(int[] a, int fromIndex, int toIndex, int val) {3202rangeCheck(a.length, fromIndex, toIndex);3203for (int i = fromIndex; i < toIndex; i++)3204a[i] = val;3205}32063207/**3208* Assigns the specified short value to each element of the specified array3209* of shorts.3210*3211* @param a the array to be filled3212* @param val the value to be stored in all elements of the array3213*/3214public static void fill(short[] a, short val) {3215for (int i = 0, len = a.length; i < len; i++)3216a[i] = val;3217}32183219/**3220* Assigns the specified short value to each element of the specified3221* range of the specified array of shorts. The range to be filled3222* extends from index {@code fromIndex}, inclusive, to index3223* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3224* range to be filled is empty.)3225*3226* @param a the array to be filled3227* @param fromIndex the index of the first element (inclusive) to be3228* filled with the specified value3229* @param toIndex the index of the last element (exclusive) to be3230* filled with the specified value3231* @param val the value to be stored in all elements of the array3232* @throws IllegalArgumentException if {@code fromIndex > toIndex}3233* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3234* {@code toIndex > a.length}3235*/3236public static void fill(short[] a, int fromIndex, int toIndex, short val) {3237rangeCheck(a.length, fromIndex, toIndex);3238for (int i = fromIndex; i < toIndex; i++)3239a[i] = val;3240}32413242/**3243* Assigns the specified char value to each element of the specified array3244* of chars.3245*3246* @param a the array to be filled3247* @param val the value to be stored in all elements of the array3248*/3249public static void fill(char[] a, char val) {3250for (int i = 0, len = a.length; i < len; i++)3251a[i] = val;3252}32533254/**3255* Assigns the specified char value to each element of the specified3256* range of the specified array of chars. The range to be filled3257* extends from index {@code fromIndex}, inclusive, to index3258* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3259* range to be filled is empty.)3260*3261* @param a the array to be filled3262* @param fromIndex the index of the first element (inclusive) to be3263* filled with the specified value3264* @param toIndex the index of the last element (exclusive) to be3265* filled with the specified value3266* @param val the value to be stored in all elements of the array3267* @throws IllegalArgumentException if {@code fromIndex > toIndex}3268* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3269* {@code toIndex > a.length}3270*/3271public static void fill(char[] a, int fromIndex, int toIndex, char val) {3272rangeCheck(a.length, fromIndex, toIndex);3273for (int i = fromIndex; i < toIndex; i++)3274a[i] = val;3275}32763277/**3278* Assigns the specified byte value to each element of the specified array3279* of bytes.3280*3281* @param a the array to be filled3282* @param val the value to be stored in all elements of the array3283*/3284public static void fill(byte[] a, byte val) {3285for (int i = 0, len = a.length; i < len; i++)3286a[i] = val;3287}32883289/**3290* Assigns the specified byte value to each element of the specified3291* range of the specified array of bytes. The range to be filled3292* extends from index {@code fromIndex}, inclusive, to index3293* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3294* range to be filled is empty.)3295*3296* @param a the array to be filled3297* @param fromIndex the index of the first element (inclusive) to be3298* filled with the specified value3299* @param toIndex the index of the last element (exclusive) to be3300* filled with the specified value3301* @param val the value to be stored in all elements of the array3302* @throws IllegalArgumentException if {@code fromIndex > toIndex}3303* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3304* {@code toIndex > a.length}3305*/3306public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {3307rangeCheck(a.length, fromIndex, toIndex);3308for (int i = fromIndex; i < toIndex; i++)3309a[i] = val;3310}33113312/**3313* Assigns the specified boolean value to each element of the specified3314* array of booleans.3315*3316* @param a the array to be filled3317* @param val the value to be stored in all elements of the array3318*/3319public static void fill(boolean[] a, boolean val) {3320for (int i = 0, len = a.length; i < len; i++)3321a[i] = val;3322}33233324/**3325* Assigns the specified boolean value to each element of the specified3326* range of the specified array of booleans. The range to be filled3327* extends from index {@code fromIndex}, inclusive, to index3328* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3329* range to be filled is empty.)3330*3331* @param a the array to be filled3332* @param fromIndex the index of the first element (inclusive) to be3333* filled with the specified value3334* @param toIndex the index of the last element (exclusive) to be3335* filled with the specified value3336* @param val the value to be stored in all elements of the array3337* @throws IllegalArgumentException if {@code fromIndex > toIndex}3338* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3339* {@code toIndex > a.length}3340*/3341public static void fill(boolean[] a, int fromIndex, int toIndex,3342boolean val) {3343rangeCheck(a.length, fromIndex, toIndex);3344for (int i = fromIndex; i < toIndex; i++)3345a[i] = val;3346}33473348/**3349* Assigns the specified double value to each element of the specified3350* array of doubles.3351*3352* @param a the array to be filled3353* @param val the value to be stored in all elements of the array3354*/3355public static void fill(double[] a, double val) {3356for (int i = 0, len = a.length; i < len; i++)3357a[i] = val;3358}33593360/**3361* Assigns the specified double value to each element of the specified3362* range of the specified array of doubles. The range to be filled3363* extends from index {@code fromIndex}, inclusive, to index3364* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3365* range to be filled is empty.)3366*3367* @param a the array to be filled3368* @param fromIndex the index of the first element (inclusive) to be3369* filled with the specified value3370* @param toIndex the index of the last element (exclusive) to be3371* filled with the specified value3372* @param val the value to be stored in all elements of the array3373* @throws IllegalArgumentException if {@code fromIndex > toIndex}3374* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3375* {@code toIndex > a.length}3376*/3377public static void fill(double[] a, int fromIndex, int toIndex,double val){3378rangeCheck(a.length, fromIndex, toIndex);3379for (int i = fromIndex; i < toIndex; i++)3380a[i] = val;3381}33823383/**3384* Assigns the specified float value to each element of the specified array3385* of floats.3386*3387* @param a the array to be filled3388* @param val the value to be stored in all elements of the array3389*/3390public static void fill(float[] a, float val) {3391for (int i = 0, len = a.length; i < len; i++)3392a[i] = val;3393}33943395/**3396* Assigns the specified float value to each element of the specified3397* range of the specified array of floats. The range to be filled3398* extends from index {@code fromIndex}, inclusive, to index3399* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3400* range to be filled is empty.)3401*3402* @param a the array to be filled3403* @param fromIndex the index of the first element (inclusive) to be3404* filled with the specified value3405* @param toIndex the index of the last element (exclusive) to be3406* filled with the specified value3407* @param val the value to be stored in all elements of the array3408* @throws IllegalArgumentException if {@code fromIndex > toIndex}3409* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3410* {@code toIndex > a.length}3411*/3412public static void fill(float[] a, int fromIndex, int toIndex, float val) {3413rangeCheck(a.length, fromIndex, toIndex);3414for (int i = fromIndex; i < toIndex; i++)3415a[i] = val;3416}34173418/**3419* Assigns the specified Object reference to each element of the specified3420* array of Objects.3421*3422* @param a the array to be filled3423* @param val the value to be stored in all elements of the array3424* @throws ArrayStoreException if the specified value is not of a3425* runtime type that can be stored in the specified array3426*/3427public static void fill(Object[] a, Object val) {3428for (int i = 0, len = a.length; i < len; i++)3429a[i] = val;3430}34313432/**3433* Assigns the specified Object reference to each element of the specified3434* range of the specified array of Objects. The range to be filled3435* extends from index {@code fromIndex}, inclusive, to index3436* {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the3437* range to be filled is empty.)3438*3439* @param a the array to be filled3440* @param fromIndex the index of the first element (inclusive) to be3441* filled with the specified value3442* @param toIndex the index of the last element (exclusive) to be3443* filled with the specified value3444* @param val the value to be stored in all elements of the array3445* @throws IllegalArgumentException if {@code fromIndex > toIndex}3446* @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or3447* {@code toIndex > a.length}3448* @throws ArrayStoreException if the specified value is not of a3449* runtime type that can be stored in the specified array3450*/3451public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {3452rangeCheck(a.length, fromIndex, toIndex);3453for (int i = fromIndex; i < toIndex; i++)3454a[i] = val;3455}34563457// Cloning34583459/**3460* Copies the specified array, truncating or padding with nulls (if necessary)3461* so the copy has the specified length. For all indices that are3462* valid in both the original array and the copy, the two arrays will3463* contain identical values. For any indices that are valid in the3464* copy but not the original, the copy will contain {@code null}.3465* Such indices will exist if and only if the specified length3466* is greater than that of the original array.3467* The resulting array is of exactly the same class as the original array.3468*3469* @param <T> the class of the objects in the array3470* @param original the array to be copied3471* @param newLength the length of the copy to be returned3472* @return a copy of the original array, truncated or padded with nulls3473* to obtain the specified length3474* @throws NegativeArraySizeException if {@code newLength} is negative3475* @throws NullPointerException if {@code original} is null3476* @since 1.63477*/3478@SuppressWarnings("unchecked")3479public static <T> T[] copyOf(T[] original, int newLength) {3480return (T[]) copyOf(original, newLength, original.getClass());3481}34823483/**3484* Copies the specified array, truncating or padding with nulls (if necessary)3485* so the copy has the specified length. For all indices that are3486* valid in both the original array and the copy, the two arrays will3487* contain identical values. For any indices that are valid in the3488* copy but not the original, the copy will contain {@code null}.3489* Such indices will exist if and only if the specified length3490* is greater than that of the original array.3491* The resulting array is of the class {@code newType}.3492*3493* @param <U> the class of the objects in the original array3494* @param <T> the class of the objects in the returned array3495* @param original the array to be copied3496* @param newLength the length of the copy to be returned3497* @param newType the class of the copy to be returned3498* @return a copy of the original array, truncated or padded with nulls3499* to obtain the specified length3500* @throws NegativeArraySizeException if {@code newLength} is negative3501* @throws NullPointerException if {@code original} is null3502* @throws ArrayStoreException if an element copied from3503* {@code original} is not of a runtime type that can be stored in3504* an array of class {@code newType}3505* @since 1.63506*/3507@IntrinsicCandidate3508public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {3509@SuppressWarnings("unchecked")3510T[] copy = ((Object)newType == (Object)Object[].class)3511? (T[]) new Object[newLength]3512: (T[]) Array.newInstance(newType.getComponentType(), newLength);3513System.arraycopy(original, 0, copy, 0,3514Math.min(original.length, newLength));3515return copy;3516}35173518/**3519* Copies the specified array, truncating or padding with zeros (if necessary)3520* so the copy has the specified length. For all indices that are3521* valid in both the original array and the copy, the two arrays will3522* contain identical values. For any indices that are valid in the3523* copy but not the original, the copy will contain {@code (byte)0}.3524* Such indices will exist if and only if the specified length3525* is greater than that of the original array.3526*3527* @param original the array to be copied3528* @param newLength the length of the copy to be returned3529* @return a copy of the original array, truncated or padded with zeros3530* to obtain the specified length3531* @throws NegativeArraySizeException if {@code newLength} is negative3532* @throws NullPointerException if {@code original} is null3533* @since 1.63534*/3535public static byte[] copyOf(byte[] original, int newLength) {3536byte[] copy = new byte[newLength];3537System.arraycopy(original, 0, copy, 0,3538Math.min(original.length, newLength));3539return copy;3540}35413542/**3543* Copies the specified array, truncating or padding with zeros (if necessary)3544* so the copy has the specified length. For all indices that are3545* valid in both the original array and the copy, the two arrays will3546* contain identical values. For any indices that are valid in the3547* copy but not the original, the copy will contain {@code (short)0}.3548* Such indices will exist if and only if the specified length3549* is greater than that of the original array.3550*3551* @param original the array to be copied3552* @param newLength the length of the copy to be returned3553* @return a copy of the original array, truncated or padded with zeros3554* to obtain the specified length3555* @throws NegativeArraySizeException if {@code newLength} is negative3556* @throws NullPointerException if {@code original} is null3557* @since 1.63558*/3559public static short[] copyOf(short[] original, int newLength) {3560short[] copy = new short[newLength];3561System.arraycopy(original, 0, copy, 0,3562Math.min(original.length, newLength));3563return copy;3564}35653566/**3567* Copies the specified array, truncating or padding with zeros (if necessary)3568* so the copy has the specified length. For all indices that are3569* valid in both the original array and the copy, the two arrays will3570* contain identical values. For any indices that are valid in the3571* copy but not the original, the copy will contain {@code 0}.3572* Such indices will exist if and only if the specified length3573* is greater than that of the original array.3574*3575* @param original the array to be copied3576* @param newLength the length of the copy to be returned3577* @return a copy of the original array, truncated or padded with zeros3578* to obtain the specified length3579* @throws NegativeArraySizeException if {@code newLength} is negative3580* @throws NullPointerException if {@code original} is null3581* @since 1.63582*/3583public static int[] copyOf(int[] original, int newLength) {3584int[] copy = new int[newLength];3585System.arraycopy(original, 0, copy, 0,3586Math.min(original.length, newLength));3587return copy;3588}35893590/**3591* Copies the specified array, truncating or padding with zeros (if necessary)3592* so the copy has the specified length. For all indices that are3593* valid in both the original array and the copy, the two arrays will3594* contain identical values. For any indices that are valid in the3595* copy but not the original, the copy will contain {@code 0L}.3596* Such indices will exist if and only if the specified length3597* is greater than that of the original array.3598*3599* @param original the array to be copied3600* @param newLength the length of the copy to be returned3601* @return a copy of the original array, truncated or padded with zeros3602* to obtain the specified length3603* @throws NegativeArraySizeException if {@code newLength} is negative3604* @throws NullPointerException if {@code original} is null3605* @since 1.63606*/3607public static long[] copyOf(long[] original, int newLength) {3608long[] copy = new long[newLength];3609System.arraycopy(original, 0, copy, 0,3610Math.min(original.length, newLength));3611return copy;3612}36133614/**3615* Copies the specified array, truncating or padding with null characters (if necessary)3616* so the copy has the specified length. For all indices that are valid3617* in both the original array and the copy, the two arrays will contain3618* identical values. For any indices that are valid in the copy but not3619* the original, the copy will contain {@code '\u005cu0000'}. Such indices3620* will exist if and only if the specified length is greater than that of3621* the original array.3622*3623* @param original the array to be copied3624* @param newLength the length of the copy to be returned3625* @return a copy of the original array, truncated or padded with null characters3626* to obtain the specified length3627* @throws NegativeArraySizeException if {@code newLength} is negative3628* @throws NullPointerException if {@code original} is null3629* @since 1.63630*/3631public static char[] copyOf(char[] original, int newLength) {3632char[] copy = new char[newLength];3633System.arraycopy(original, 0, copy, 0,3634Math.min(original.length, newLength));3635return copy;3636}36373638/**3639* Copies the specified array, truncating or padding with zeros (if necessary)3640* so the copy has the specified length. For all indices that are3641* valid in both the original array and the copy, the two arrays will3642* contain identical values. For any indices that are valid in the3643* copy but not the original, the copy will contain {@code 0f}.3644* Such indices will exist if and only if the specified length3645* is greater than that of the original array.3646*3647* @param original the array to be copied3648* @param newLength the length of the copy to be returned3649* @return a copy of the original array, truncated or padded with zeros3650* to obtain the specified length3651* @throws NegativeArraySizeException if {@code newLength} is negative3652* @throws NullPointerException if {@code original} is null3653* @since 1.63654*/3655public static float[] copyOf(float[] original, int newLength) {3656float[] copy = new float[newLength];3657System.arraycopy(original, 0, copy, 0,3658Math.min(original.length, newLength));3659return copy;3660}36613662/**3663* Copies the specified array, truncating or padding with zeros (if necessary)3664* so the copy has the specified length. For all indices that are3665* valid in both the original array and the copy, the two arrays will3666* contain identical values. For any indices that are valid in the3667* copy but not the original, the copy will contain {@code 0d}.3668* Such indices will exist if and only if the specified length3669* is greater than that of the original array.3670*3671* @param original the array to be copied3672* @param newLength the length of the copy to be returned3673* @return a copy of the original array, truncated or padded with zeros3674* to obtain the specified length3675* @throws NegativeArraySizeException if {@code newLength} is negative3676* @throws NullPointerException if {@code original} is null3677* @since 1.63678*/3679public static double[] copyOf(double[] original, int newLength) {3680double[] copy = new double[newLength];3681System.arraycopy(original, 0, copy, 0,3682Math.min(original.length, newLength));3683return copy;3684}36853686/**3687* Copies the specified array, truncating or padding with {@code false} (if necessary)3688* so the copy has the specified length. For all indices that are3689* valid in both the original array and the copy, the two arrays will3690* contain identical values. For any indices that are valid in the3691* copy but not the original, the copy will contain {@code false}.3692* Such indices will exist if and only if the specified length3693* is greater than that of the original array.3694*3695* @param original the array to be copied3696* @param newLength the length of the copy to be returned3697* @return a copy of the original array, truncated or padded with false elements3698* to obtain the specified length3699* @throws NegativeArraySizeException if {@code newLength} is negative3700* @throws NullPointerException if {@code original} is null3701* @since 1.63702*/3703public static boolean[] copyOf(boolean[] original, int newLength) {3704boolean[] copy = new boolean[newLength];3705System.arraycopy(original, 0, copy, 0,3706Math.min(original.length, newLength));3707return copy;3708}37093710/**3711* Copies the specified range of the specified array into a new array.3712* The initial index of the range ({@code from}) must lie between zero3713* and {@code original.length}, inclusive. The value at3714* {@code original[from]} is placed into the initial element of the copy3715* (unless {@code from == original.length} or {@code from == to}).3716* Values from subsequent elements in the original array are placed into3717* subsequent elements in the copy. The final index of the range3718* ({@code to}), which must be greater than or equal to {@code from},3719* may be greater than {@code original.length}, in which case3720* {@code null} is placed in all elements of the copy whose index is3721* greater than or equal to {@code original.length - from}. The length3722* of the returned array will be {@code to - from}.3723* <p>3724* The resulting array is of exactly the same class as the original array.3725*3726* @param <T> the class of the objects in the array3727* @param original the array from which a range is to be copied3728* @param from the initial index of the range to be copied, inclusive3729* @param to the final index of the range to be copied, exclusive.3730* (This index may lie outside the array.)3731* @return a new array containing the specified range from the original array,3732* truncated or padded with nulls to obtain the required length3733* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3734* or {@code from > original.length}3735* @throws IllegalArgumentException if {@code from > to}3736* @throws NullPointerException if {@code original} is null3737* @since 1.63738*/3739@SuppressWarnings("unchecked")3740public static <T> T[] copyOfRange(T[] original, int from, int to) {3741return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());3742}37433744/**3745* Copies the specified range of the specified array into a new array.3746* The initial index of the range ({@code from}) must lie between zero3747* and {@code original.length}, inclusive. The value at3748* {@code original[from]} is placed into the initial element of the copy3749* (unless {@code from == original.length} or {@code from == to}).3750* Values from subsequent elements in the original array are placed into3751* subsequent elements in the copy. The final index of the range3752* ({@code to}), which must be greater than or equal to {@code from},3753* may be greater than {@code original.length}, in which case3754* {@code null} is placed in all elements of the copy whose index is3755* greater than or equal to {@code original.length - from}. The length3756* of the returned array will be {@code to - from}.3757* The resulting array is of the class {@code newType}.3758*3759* @param <U> the class of the objects in the original array3760* @param <T> the class of the objects in the returned array3761* @param original the array from which a range is to be copied3762* @param from the initial index of the range to be copied, inclusive3763* @param to the final index of the range to be copied, exclusive.3764* (This index may lie outside the array.)3765* @param newType the class of the copy to be returned3766* @return a new array containing the specified range from the original array,3767* truncated or padded with nulls to obtain the required length3768* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3769* or {@code from > original.length}3770* @throws IllegalArgumentException if {@code from > to}3771* @throws NullPointerException if {@code original} is null3772* @throws ArrayStoreException if an element copied from3773* {@code original} is not of a runtime type that can be stored in3774* an array of class {@code newType}.3775* @since 1.63776*/3777@IntrinsicCandidate3778public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {3779int newLength = to - from;3780if (newLength < 0)3781throw new IllegalArgumentException(from + " > " + to);3782@SuppressWarnings("unchecked")3783T[] copy = ((Object)newType == (Object)Object[].class)3784? (T[]) new Object[newLength]3785: (T[]) Array.newInstance(newType.getComponentType(), newLength);3786System.arraycopy(original, from, copy, 0,3787Math.min(original.length - from, newLength));3788return copy;3789}37903791/**3792* Copies the specified range of the specified array into a new array.3793* The initial index of the range ({@code from}) must lie between zero3794* and {@code original.length}, inclusive. The value at3795* {@code original[from]} is placed into the initial element of the copy3796* (unless {@code from == original.length} or {@code from == to}).3797* Values from subsequent elements in the original array are placed into3798* subsequent elements in the copy. The final index of the range3799* ({@code to}), which must be greater than or equal to {@code from},3800* may be greater than {@code original.length}, in which case3801* {@code (byte)0} is placed in all elements of the copy whose index is3802* greater than or equal to {@code original.length - from}. The length3803* of the returned array will be {@code to - from}.3804*3805* @param original the array from which a range is to be copied3806* @param from the initial index of the range to be copied, inclusive3807* @param to the final index of the range to be copied, exclusive.3808* (This index may lie outside the array.)3809* @return a new array containing the specified range from the original array,3810* truncated or padded with zeros to obtain the required length3811* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3812* or {@code from > original.length}3813* @throws IllegalArgumentException if {@code from > to}3814* @throws NullPointerException if {@code original} is null3815* @since 1.63816*/3817public static byte[] copyOfRange(byte[] original, int from, int to) {3818int newLength = to - from;3819if (newLength < 0)3820throw new IllegalArgumentException(from + " > " + to);3821byte[] copy = new byte[newLength];3822System.arraycopy(original, from, copy, 0,3823Math.min(original.length - from, newLength));3824return copy;3825}38263827/**3828* Copies the specified range of the specified array into a new array.3829* The initial index of the range ({@code from}) must lie between zero3830* and {@code original.length}, inclusive. The value at3831* {@code original[from]} is placed into the initial element of the copy3832* (unless {@code from == original.length} or {@code from == to}).3833* Values from subsequent elements in the original array are placed into3834* subsequent elements in the copy. The final index of the range3835* ({@code to}), which must be greater than or equal to {@code from},3836* may be greater than {@code original.length}, in which case3837* {@code (short)0} is placed in all elements of the copy whose index is3838* greater than or equal to {@code original.length - from}. The length3839* of the returned array will be {@code to - from}.3840*3841* @param original the array from which a range is to be copied3842* @param from the initial index of the range to be copied, inclusive3843* @param to the final index of the range to be copied, exclusive.3844* (This index may lie outside the array.)3845* @return a new array containing the specified range from the original array,3846* truncated or padded with zeros to obtain the required length3847* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3848* or {@code from > original.length}3849* @throws IllegalArgumentException if {@code from > to}3850* @throws NullPointerException if {@code original} is null3851* @since 1.63852*/3853public static short[] copyOfRange(short[] original, int from, int to) {3854int newLength = to - from;3855if (newLength < 0)3856throw new IllegalArgumentException(from + " > " + to);3857short[] copy = new short[newLength];3858System.arraycopy(original, from, copy, 0,3859Math.min(original.length - from, newLength));3860return copy;3861}38623863/**3864* Copies the specified range of the specified array into a new array.3865* The initial index of the range ({@code from}) must lie between zero3866* and {@code original.length}, inclusive. The value at3867* {@code original[from]} is placed into the initial element of the copy3868* (unless {@code from == original.length} or {@code from == to}).3869* Values from subsequent elements in the original array are placed into3870* subsequent elements in the copy. The final index of the range3871* ({@code to}), which must be greater than or equal to {@code from},3872* may be greater than {@code original.length}, in which case3873* {@code 0} is placed in all elements of the copy whose index is3874* greater than or equal to {@code original.length - from}. The length3875* of the returned array will be {@code to - from}.3876*3877* @param original the array from which a range is to be copied3878* @param from the initial index of the range to be copied, inclusive3879* @param to the final index of the range to be copied, exclusive.3880* (This index may lie outside the array.)3881* @return a new array containing the specified range from the original array,3882* truncated or padded with zeros to obtain the required length3883* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3884* or {@code from > original.length}3885* @throws IllegalArgumentException if {@code from > to}3886* @throws NullPointerException if {@code original} is null3887* @since 1.63888*/3889public static int[] copyOfRange(int[] original, int from, int to) {3890int newLength = to - from;3891if (newLength < 0)3892throw new IllegalArgumentException(from + " > " + to);3893int[] copy = new int[newLength];3894System.arraycopy(original, from, copy, 0,3895Math.min(original.length - from, newLength));3896return copy;3897}38983899/**3900* Copies the specified range of the specified array into a new array.3901* The initial index of the range ({@code from}) must lie between zero3902* and {@code original.length}, inclusive. The value at3903* {@code original[from]} is placed into the initial element of the copy3904* (unless {@code from == original.length} or {@code from == to}).3905* Values from subsequent elements in the original array are placed into3906* subsequent elements in the copy. The final index of the range3907* ({@code to}), which must be greater than or equal to {@code from},3908* may be greater than {@code original.length}, in which case3909* {@code 0L} is placed in all elements of the copy whose index is3910* greater than or equal to {@code original.length - from}. The length3911* of the returned array will be {@code to - from}.3912*3913* @param original the array from which a range is to be copied3914* @param from the initial index of the range to be copied, inclusive3915* @param to the final index of the range to be copied, exclusive.3916* (This index may lie outside the array.)3917* @return a new array containing the specified range from the original array,3918* truncated or padded with zeros to obtain the required length3919* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3920* or {@code from > original.length}3921* @throws IllegalArgumentException if {@code from > to}3922* @throws NullPointerException if {@code original} is null3923* @since 1.63924*/3925public static long[] copyOfRange(long[] original, int from, int to) {3926int newLength = to - from;3927if (newLength < 0)3928throw new IllegalArgumentException(from + " > " + to);3929long[] copy = new long[newLength];3930System.arraycopy(original, from, copy, 0,3931Math.min(original.length - from, newLength));3932return copy;3933}39343935/**3936* Copies the specified range of the specified array into a new array.3937* The initial index of the range ({@code from}) must lie between zero3938* and {@code original.length}, inclusive. The value at3939* {@code original[from]} is placed into the initial element of the copy3940* (unless {@code from == original.length} or {@code from == to}).3941* Values from subsequent elements in the original array are placed into3942* subsequent elements in the copy. The final index of the range3943* ({@code to}), which must be greater than or equal to {@code from},3944* may be greater than {@code original.length}, in which case3945* {@code '\u005cu0000'} is placed in all elements of the copy whose index is3946* greater than or equal to {@code original.length - from}. The length3947* of the returned array will be {@code to - from}.3948*3949* @param original the array from which a range is to be copied3950* @param from the initial index of the range to be copied, inclusive3951* @param to the final index of the range to be copied, exclusive.3952* (This index may lie outside the array.)3953* @return a new array containing the specified range from the original array,3954* truncated or padded with null characters to obtain the required length3955* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3956* or {@code from > original.length}3957* @throws IllegalArgumentException if {@code from > to}3958* @throws NullPointerException if {@code original} is null3959* @since 1.63960*/3961public static char[] copyOfRange(char[] original, int from, int to) {3962int newLength = to - from;3963if (newLength < 0)3964throw new IllegalArgumentException(from + " > " + to);3965char[] copy = new char[newLength];3966System.arraycopy(original, from, copy, 0,3967Math.min(original.length - from, newLength));3968return copy;3969}39703971/**3972* Copies the specified range of the specified array into a new array.3973* The initial index of the range ({@code from}) must lie between zero3974* and {@code original.length}, inclusive. The value at3975* {@code original[from]} is placed into the initial element of the copy3976* (unless {@code from == original.length} or {@code from == to}).3977* Values from subsequent elements in the original array are placed into3978* subsequent elements in the copy. The final index of the range3979* ({@code to}), which must be greater than or equal to {@code from},3980* may be greater than {@code original.length}, in which case3981* {@code 0f} is placed in all elements of the copy whose index is3982* greater than or equal to {@code original.length - from}. The length3983* of the returned array will be {@code to - from}.3984*3985* @param original the array from which a range is to be copied3986* @param from the initial index of the range to be copied, inclusive3987* @param to the final index of the range to be copied, exclusive.3988* (This index may lie outside the array.)3989* @return a new array containing the specified range from the original array,3990* truncated or padded with zeros to obtain the required length3991* @throws ArrayIndexOutOfBoundsException if {@code from < 0}3992* or {@code from > original.length}3993* @throws IllegalArgumentException if {@code from > to}3994* @throws NullPointerException if {@code original} is null3995* @since 1.63996*/3997public static float[] copyOfRange(float[] original, int from, int to) {3998int newLength = to - from;3999if (newLength < 0)4000throw new IllegalArgumentException(from + " > " + to);4001float[] copy = new float[newLength];4002System.arraycopy(original, from, copy, 0,4003Math.min(original.length - from, newLength));4004return copy;4005}40064007/**4008* Copies the specified range of the specified array into a new array.4009* The initial index of the range ({@code from}) must lie between zero4010* and {@code original.length}, inclusive. The value at4011* {@code original[from]} is placed into the initial element of the copy4012* (unless {@code from == original.length} or {@code from == to}).4013* Values from subsequent elements in the original array are placed into4014* subsequent elements in the copy. The final index of the range4015* ({@code to}), which must be greater than or equal to {@code from},4016* may be greater than {@code original.length}, in which case4017* {@code 0d} is placed in all elements of the copy whose index is4018* greater than or equal to {@code original.length - from}. The length4019* of the returned array will be {@code to - from}.4020*4021* @param original the array from which a range is to be copied4022* @param from the initial index of the range to be copied, inclusive4023* @param to the final index of the range to be copied, exclusive.4024* (This index may lie outside the array.)4025* @return a new array containing the specified range from the original array,4026* truncated or padded with zeros to obtain the required length4027* @throws ArrayIndexOutOfBoundsException if {@code from < 0}4028* or {@code from > original.length}4029* @throws IllegalArgumentException if {@code from > to}4030* @throws NullPointerException if {@code original} is null4031* @since 1.64032*/4033public static double[] copyOfRange(double[] original, int from, int to) {4034int newLength = to - from;4035if (newLength < 0)4036throw new IllegalArgumentException(from + " > " + to);4037double[] copy = new double[newLength];4038System.arraycopy(original, from, copy, 0,4039Math.min(original.length - from, newLength));4040return copy;4041}40424043/**4044* Copies the specified range of the specified array into a new array.4045* The initial index of the range ({@code from}) must lie between zero4046* and {@code original.length}, inclusive. The value at4047* {@code original[from]} is placed into the initial element of the copy4048* (unless {@code from == original.length} or {@code from == to}).4049* Values from subsequent elements in the original array are placed into4050* subsequent elements in the copy. The final index of the range4051* ({@code to}), which must be greater than or equal to {@code from},4052* may be greater than {@code original.length}, in which case4053* {@code false} is placed in all elements of the copy whose index is4054* greater than or equal to {@code original.length - from}. The length4055* of the returned array will be {@code to - from}.4056*4057* @param original the array from which a range is to be copied4058* @param from the initial index of the range to be copied, inclusive4059* @param to the final index of the range to be copied, exclusive.4060* (This index may lie outside the array.)4061* @return a new array containing the specified range from the original array,4062* truncated or padded with false elements to obtain the required length4063* @throws ArrayIndexOutOfBoundsException if {@code from < 0}4064* or {@code from > original.length}4065* @throws IllegalArgumentException if {@code from > to}4066* @throws NullPointerException if {@code original} is null4067* @since 1.64068*/4069public static boolean[] copyOfRange(boolean[] original, int from, int to) {4070int newLength = to - from;4071if (newLength < 0)4072throw new IllegalArgumentException(from + " > " + to);4073boolean[] copy = new boolean[newLength];4074System.arraycopy(original, from, copy, 0,4075Math.min(original.length - from, newLength));4076return copy;4077}40784079// Misc40804081/**4082* Returns a fixed-size list backed by the specified array. Changes made to4083* the array will be visible in the returned list, and changes made to the4084* list will be visible in the array. The returned list is4085* {@link Serializable} and implements {@link RandomAccess}.4086*4087* <p>The returned list implements the optional {@code Collection} methods, except4088* those that would change the size of the returned list. Those methods leave4089* the list unchanged and throw {@link UnsupportedOperationException}.4090*4091* @apiNote4092* This method acts as bridge between array-based and collection-based4093* APIs, in combination with {@link Collection#toArray}.4094*4095* <p>This method provides a way to wrap an existing array:4096* <pre>{@code4097* Integer[] numbers = ...4098* ...4099* List<Integer> values = Arrays.asList(numbers);4100* }</pre>4101*4102* <p>This method also provides a convenient way to create a fixed-size4103* list initialized to contain several elements:4104* <pre>{@code4105* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");4106* }</pre>4107*4108* <p><em>The list returned by this method is modifiable.</em>4109* To create an unmodifiable list, use4110* {@link Collections#unmodifiableList Collections.unmodifiableList}4111* or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.4112*4113* @param <T> the class of the objects in the array4114* @param a the array by which the list will be backed4115* @return a list view of the specified array4116* @throws NullPointerException if the specified array is {@code null}4117*/4118@SafeVarargs4119@SuppressWarnings("varargs")4120public static <T> List<T> asList(T... a) {4121return new ArrayList<>(a);4122}41234124/**4125* @serial include4126*/4127private static class ArrayList<E> extends AbstractList<E>4128implements RandomAccess, java.io.Serializable4129{4130@java.io.Serial4131private static final long serialVersionUID = -2764017481108945198L;4132@SuppressWarnings("serial") // Conditionally serializable4133private final E[] a;41344135ArrayList(E[] array) {4136a = Objects.requireNonNull(array);4137}41384139@Override4140public int size() {4141return a.length;4142}41434144@Override4145public Object[] toArray() {4146return Arrays.copyOf(a, a.length, Object[].class);4147}41484149@Override4150@SuppressWarnings("unchecked")4151public <T> T[] toArray(T[] a) {4152int size = size();4153if (a.length < size)4154return Arrays.copyOf(this.a, size,4155(Class<? extends T[]>) a.getClass());4156System.arraycopy(this.a, 0, a, 0, size);4157if (a.length > size)4158a[size] = null;4159return a;4160}41614162@Override4163public E get(int index) {4164return a[index];4165}41664167@Override4168public E set(int index, E element) {4169E oldValue = a[index];4170a[index] = element;4171return oldValue;4172}41734174@Override4175public int indexOf(Object o) {4176E[] a = this.a;4177if (o == null) {4178for (int i = 0; i < a.length; i++)4179if (a[i] == null)4180return i;4181} else {4182for (int i = 0; i < a.length; i++)4183if (o.equals(a[i]))4184return i;4185}4186return -1;4187}41884189@Override4190public boolean contains(Object o) {4191return indexOf(o) >= 0;4192}41934194@Override4195public Spliterator<E> spliterator() {4196return Spliterators.spliterator(a, Spliterator.ORDERED);4197}41984199@Override4200public void forEach(Consumer<? super E> action) {4201Objects.requireNonNull(action);4202for (E e : a) {4203action.accept(e);4204}4205}42064207@Override4208public void replaceAll(UnaryOperator<E> operator) {4209Objects.requireNonNull(operator);4210E[] a = this.a;4211for (int i = 0; i < a.length; i++) {4212a[i] = operator.apply(a[i]);4213}4214}42154216@Override4217public void sort(Comparator<? super E> c) {4218Arrays.sort(a, c);4219}42204221@Override4222public Iterator<E> iterator() {4223return new ArrayItr<>(a);4224}4225}42264227private static class ArrayItr<E> implements Iterator<E> {4228private int cursor;4229private final E[] a;42304231ArrayItr(E[] a) {4232this.a = a;4233}42344235@Override4236public boolean hasNext() {4237return cursor < a.length;4238}42394240@Override4241public E next() {4242int i = cursor;4243if (i >= a.length) {4244throw new NoSuchElementException();4245}4246cursor = i + 1;4247return a[i];4248}4249}42504251/**4252* Returns a hash code based on the contents of the specified array.4253* For any two {@code long} arrays {@code a} and {@code b}4254* such that {@code Arrays.equals(a, b)}, it is also the case that4255* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4256*4257* <p>The value returned by this method is the same value that would be4258* obtained by invoking the {@link List#hashCode() hashCode}4259* method on a {@link List} containing a sequence of {@link Long}4260* instances representing the elements of {@code a} in the same order.4261* If {@code a} is {@code null}, this method returns 0.4262*4263* @param a the array whose hash value to compute4264* @return a content-based hash code for {@code a}4265* @since 1.54266*/4267public static int hashCode(long a[]) {4268if (a == null)4269return 0;42704271int result = 1;4272for (long element : a) {4273int elementHash = (int)(element ^ (element >>> 32));4274result = 31 * result + elementHash;4275}42764277return result;4278}42794280/**4281* Returns a hash code based on the contents of the specified array.4282* For any two non-null {@code int} arrays {@code a} and {@code b}4283* such that {@code Arrays.equals(a, b)}, it is also the case that4284* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4285*4286* <p>The value returned by this method is the same value that would be4287* obtained by invoking the {@link List#hashCode() hashCode}4288* method on a {@link List} containing a sequence of {@link Integer}4289* instances representing the elements of {@code a} in the same order.4290* If {@code a} is {@code null}, this method returns 0.4291*4292* @param a the array whose hash value to compute4293* @return a content-based hash code for {@code a}4294* @since 1.54295*/4296public static int hashCode(int a[]) {4297if (a == null)4298return 0;42994300int result = 1;4301for (int element : a)4302result = 31 * result + element;43034304return result;4305}43064307/**4308* Returns a hash code based on the contents of the specified array.4309* For any two {@code short} arrays {@code a} and {@code b}4310* such that {@code Arrays.equals(a, b)}, it is also the case that4311* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4312*4313* <p>The value returned by this method is the same value that would be4314* obtained by invoking the {@link List#hashCode() hashCode}4315* method on a {@link List} containing a sequence of {@link Short}4316* instances representing the elements of {@code a} in the same order.4317* If {@code a} is {@code null}, this method returns 0.4318*4319* @param a the array whose hash value to compute4320* @return a content-based hash code for {@code a}4321* @since 1.54322*/4323public static int hashCode(short a[]) {4324if (a == null)4325return 0;43264327int result = 1;4328for (short element : a)4329result = 31 * result + element;43304331return result;4332}43334334/**4335* Returns a hash code based on the contents of the specified array.4336* For any two {@code char} arrays {@code a} and {@code b}4337* such that {@code Arrays.equals(a, b)}, it is also the case that4338* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4339*4340* <p>The value returned by this method is the same value that would be4341* obtained by invoking the {@link List#hashCode() hashCode}4342* method on a {@link List} containing a sequence of {@link Character}4343* instances representing the elements of {@code a} in the same order.4344* If {@code a} is {@code null}, this method returns 0.4345*4346* @param a the array whose hash value to compute4347* @return a content-based hash code for {@code a}4348* @since 1.54349*/4350public static int hashCode(char a[]) {4351if (a == null)4352return 0;43534354int result = 1;4355for (char element : a)4356result = 31 * result + element;43574358return result;4359}43604361/**4362* Returns a hash code based on the contents of the specified array.4363* For any two {@code byte} arrays {@code a} and {@code b}4364* such that {@code Arrays.equals(a, b)}, it is also the case that4365* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4366*4367* <p>The value returned by this method is the same value that would be4368* obtained by invoking the {@link List#hashCode() hashCode}4369* method on a {@link List} containing a sequence of {@link Byte}4370* instances representing the elements of {@code a} in the same order.4371* If {@code a} is {@code null}, this method returns 0.4372*4373* @param a the array whose hash value to compute4374* @return a content-based hash code for {@code a}4375* @since 1.54376*/4377public static int hashCode(byte a[]) {4378if (a == null)4379return 0;43804381int result = 1;4382for (byte element : a)4383result = 31 * result + element;43844385return result;4386}43874388/**4389* Returns a hash code based on the contents of the specified array.4390* For any two {@code boolean} arrays {@code a} and {@code b}4391* such that {@code Arrays.equals(a, b)}, it is also the case that4392* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4393*4394* <p>The value returned by this method is the same value that would be4395* obtained by invoking the {@link List#hashCode() hashCode}4396* method on a {@link List} containing a sequence of {@link Boolean}4397* instances representing the elements of {@code a} in the same order.4398* If {@code a} is {@code null}, this method returns 0.4399*4400* @param a the array whose hash value to compute4401* @return a content-based hash code for {@code a}4402* @since 1.54403*/4404public static int hashCode(boolean a[]) {4405if (a == null)4406return 0;44074408int result = 1;4409for (boolean element : a)4410result = 31 * result + (element ? 1231 : 1237);44114412return result;4413}44144415/**4416* Returns a hash code based on the contents of the specified array.4417* For any two {@code float} arrays {@code a} and {@code b}4418* such that {@code Arrays.equals(a, b)}, it is also the case that4419* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4420*4421* <p>The value returned by this method is the same value that would be4422* obtained by invoking the {@link List#hashCode() hashCode}4423* method on a {@link List} containing a sequence of {@link Float}4424* instances representing the elements of {@code a} in the same order.4425* If {@code a} is {@code null}, this method returns 0.4426*4427* @param a the array whose hash value to compute4428* @return a content-based hash code for {@code a}4429* @since 1.54430*/4431public static int hashCode(float a[]) {4432if (a == null)4433return 0;44344435int result = 1;4436for (float element : a)4437result = 31 * result + Float.floatToIntBits(element);44384439return result;4440}44414442/**4443* Returns a hash code based on the contents of the specified array.4444* For any two {@code double} arrays {@code a} and {@code b}4445* such that {@code Arrays.equals(a, b)}, it is also the case that4446* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4447*4448* <p>The value returned by this method is the same value that would be4449* obtained by invoking the {@link List#hashCode() hashCode}4450* method on a {@link List} containing a sequence of {@link Double}4451* instances representing the elements of {@code a} in the same order.4452* If {@code a} is {@code null}, this method returns 0.4453*4454* @param a the array whose hash value to compute4455* @return a content-based hash code for {@code a}4456* @since 1.54457*/4458public static int hashCode(double a[]) {4459if (a == null)4460return 0;44614462int result = 1;4463for (double element : a) {4464long bits = Double.doubleToLongBits(element);4465result = 31 * result + (int)(bits ^ (bits >>> 32));4466}4467return result;4468}44694470/**4471* Returns a hash code based on the contents of the specified array. If4472* the array contains other arrays as elements, the hash code is based on4473* their identities rather than their contents. It is therefore4474* acceptable to invoke this method on an array that contains itself as an4475* element, either directly or indirectly through one or more levels of4476* arrays.4477*4478* <p>For any two arrays {@code a} and {@code b} such that4479* {@code Arrays.equals(a, b)}, it is also the case that4480* {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.4481*4482* <p>The value returned by this method is equal to the value that would4483* be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a}4484* is {@code null}, in which case {@code 0} is returned.4485*4486* @param a the array whose content-based hash code to compute4487* @return a content-based hash code for {@code a}4488* @see #deepHashCode(Object[])4489* @since 1.54490*/4491public static int hashCode(Object a[]) {4492if (a == null)4493return 0;44944495int result = 1;44964497for (Object element : a)4498result = 31 * result + (element == null ? 0 : element.hashCode());44994500return result;4501}45024503/**4504* Returns a hash code based on the "deep contents" of the specified4505* array. If the array contains other arrays as elements, the4506* hash code is based on their contents and so on, ad infinitum.4507* It is therefore unacceptable to invoke this method on an array that4508* contains itself as an element, either directly or indirectly through4509* one or more levels of arrays. The behavior of such an invocation is4510* undefined.4511*4512* <p>For any two arrays {@code a} and {@code b} such that4513* {@code Arrays.deepEquals(a, b)}, it is also the case that4514* {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.4515*4516* <p>The computation of the value returned by this method is similar to4517* that of the value returned by {@link List#hashCode()} on a list4518* containing the same elements as {@code a} in the same order, with one4519* difference: If an element {@code e} of {@code a} is itself an array,4520* its hash code is computed not by calling {@code e.hashCode()}, but as4521* by calling the appropriate overloading of {@code Arrays.hashCode(e)}4522* if {@code e} is an array of a primitive type, or as by calling4523* {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array4524* of a reference type. If {@code a} is {@code null}, this method4525* returns 0.4526*4527* @param a the array whose deep-content-based hash code to compute4528* @return a deep-content-based hash code for {@code a}4529* @see #hashCode(Object[])4530* @since 1.54531*/4532public static int deepHashCode(Object a[]) {4533if (a == null)4534return 0;45354536int result = 1;45374538for (Object element : a) {4539final int elementHash;4540final Class<?> cl;4541if (element == null)4542elementHash = 0;4543else if ((cl = element.getClass().getComponentType()) == null)4544elementHash = element.hashCode();4545else if (element instanceof Object[])4546elementHash = deepHashCode((Object[]) element);4547else4548elementHash = primitiveArrayHashCode(element, cl);45494550result = 31 * result + elementHash;4551}45524553return result;4554}45554556private static int primitiveArrayHashCode(Object a, Class<?> cl) {4557return4558(cl == byte.class) ? hashCode((byte[]) a) :4559(cl == int.class) ? hashCode((int[]) a) :4560(cl == long.class) ? hashCode((long[]) a) :4561(cl == char.class) ? hashCode((char[]) a) :4562(cl == short.class) ? hashCode((short[]) a) :4563(cl == boolean.class) ? hashCode((boolean[]) a) :4564(cl == double.class) ? hashCode((double[]) a) :4565// If new primitive types are ever added, this method must be4566// expanded or we will fail here with ClassCastException.4567hashCode((float[]) a);4568}45694570/**4571* Returns {@code true} if the two specified arrays are <i>deeply4572* equal</i> to one another. Unlike the {@link #equals(Object[],Object[])}4573* method, this method is appropriate for use with nested arrays of4574* arbitrary depth.4575*4576* <p>Two array references are considered deeply equal if both4577* are {@code null}, or if they refer to arrays that contain the same4578* number of elements and all corresponding pairs of elements in the two4579* arrays are deeply equal.4580*4581* <p>Two possibly {@code null} elements {@code e1} and {@code e2} are4582* deeply equal if any of the following conditions hold:4583* <ul>4584* <li> {@code e1} and {@code e2} are both arrays of object reference4585* types, and {@code Arrays.deepEquals(e1, e2) would return true}4586* <li> {@code e1} and {@code e2} are arrays of the same primitive4587* type, and the appropriate overloading of4588* {@code Arrays.equals(e1, e2)} would return true.4589* <li> {@code e1 == e2}4590* <li> {@code e1.equals(e2)} would return true.4591* </ul>4592* Note that this definition permits {@code null} elements at any depth.4593*4594* <p>If either of the specified arrays contain themselves as elements4595* either directly or indirectly through one or more levels of arrays,4596* the behavior of this method is undefined.4597*4598* @param a1 one array to be tested for equality4599* @param a2 the other array to be tested for equality4600* @return {@code true} if the two arrays are equal4601* @see #equals(Object[],Object[])4602* @see Objects#deepEquals(Object, Object)4603* @since 1.54604*/4605public static boolean deepEquals(Object[] a1, Object[] a2) {4606if (a1 == a2)4607return true;4608if (a1 == null || a2==null)4609return false;4610int length = a1.length;4611if (a2.length != length)4612return false;46134614for (int i = 0; i < length; i++) {4615Object e1 = a1[i];4616Object e2 = a2[i];46174618if (e1 == e2)4619continue;4620if (e1 == null)4621return false;46224623// Figure out whether the two elements are equal4624boolean eq = deepEquals0(e1, e2);46254626if (!eq)4627return false;4628}4629return true;4630}46314632static boolean deepEquals0(Object e1, Object e2) {4633assert e1 != null;4634boolean eq;4635if (e1 instanceof Object[] && e2 instanceof Object[])4636eq = deepEquals ((Object[]) e1, (Object[]) e2);4637else if (e1 instanceof byte[] && e2 instanceof byte[])4638eq = equals((byte[]) e1, (byte[]) e2);4639else if (e1 instanceof short[] && e2 instanceof short[])4640eq = equals((short[]) e1, (short[]) e2);4641else if (e1 instanceof int[] && e2 instanceof int[])4642eq = equals((int[]) e1, (int[]) e2);4643else if (e1 instanceof long[] && e2 instanceof long[])4644eq = equals((long[]) e1, (long[]) e2);4645else if (e1 instanceof char[] && e2 instanceof char[])4646eq = equals((char[]) e1, (char[]) e2);4647else if (e1 instanceof float[] && e2 instanceof float[])4648eq = equals((float[]) e1, (float[]) e2);4649else if (e1 instanceof double[] && e2 instanceof double[])4650eq = equals((double[]) e1, (double[]) e2);4651else if (e1 instanceof boolean[] && e2 instanceof boolean[])4652eq = equals((boolean[]) e1, (boolean[]) e2);4653else4654eq = e1.equals(e2);4655return eq;4656}46574658/**4659* Returns a string representation of the contents of the specified array.4660* The string representation consists of a list of the array's elements,4661* enclosed in square brackets ({@code "[]"}). Adjacent elements are4662* separated by the characters {@code ", "} (a comma followed by a4663* space). Elements are converted to strings as by4664* {@code String.valueOf(long)}. Returns {@code "null"} if {@code a}4665* is {@code null}.4666*4667* @param a the array whose string representation to return4668* @return a string representation of {@code a}4669* @since 1.54670*/4671public static String toString(long[] a) {4672if (a == null)4673return "null";4674int iMax = a.length - 1;4675if (iMax == -1)4676return "[]";46774678StringBuilder b = new StringBuilder();4679b.append('[');4680for (int i = 0; ; i++) {4681b.append(a[i]);4682if (i == iMax)4683return b.append(']').toString();4684b.append(", ");4685}4686}46874688/**4689* Returns a string representation of the contents of the specified array.4690* The string representation consists of a list of the array's elements,4691* enclosed in square brackets ({@code "[]"}). Adjacent elements are4692* separated by the characters {@code ", "} (a comma followed by a4693* space). Elements are converted to strings as by4694* {@code String.valueOf(int)}. Returns {@code "null"} if {@code a} is4695* {@code null}.4696*4697* @param a the array whose string representation to return4698* @return a string representation of {@code a}4699* @since 1.54700*/4701public static String toString(int[] a) {4702if (a == null)4703return "null";4704int iMax = a.length - 1;4705if (iMax == -1)4706return "[]";47074708StringBuilder b = new StringBuilder();4709b.append('[');4710for (int i = 0; ; i++) {4711b.append(a[i]);4712if (i == iMax)4713return b.append(']').toString();4714b.append(", ");4715}4716}47174718/**4719* Returns a string representation of the contents of the specified array.4720* The string representation consists of a list of the array's elements,4721* enclosed in square brackets ({@code "[]"}). Adjacent elements are4722* separated by the characters {@code ", "} (a comma followed by a4723* space). Elements are converted to strings as by4724* {@code String.valueOf(short)}. Returns {@code "null"} if {@code a}4725* is {@code null}.4726*4727* @param a the array whose string representation to return4728* @return a string representation of {@code a}4729* @since 1.54730*/4731public static String toString(short[] a) {4732if (a == null)4733return "null";4734int iMax = a.length - 1;4735if (iMax == -1)4736return "[]";47374738StringBuilder b = new StringBuilder();4739b.append('[');4740for (int i = 0; ; i++) {4741b.append(a[i]);4742if (i == iMax)4743return b.append(']').toString();4744b.append(", ");4745}4746}47474748/**4749* Returns a string representation of the contents of the specified array.4750* The string representation consists of a list of the array's elements,4751* enclosed in square brackets ({@code "[]"}). Adjacent elements are4752* separated by the characters {@code ", "} (a comma followed by a4753* space). Elements are converted to strings as by4754* {@code String.valueOf(char)}. Returns {@code "null"} if {@code a}4755* is {@code null}.4756*4757* @param a the array whose string representation to return4758* @return a string representation of {@code a}4759* @since 1.54760*/4761public static String toString(char[] a) {4762if (a == null)4763return "null";4764int iMax = a.length - 1;4765if (iMax == -1)4766return "[]";47674768StringBuilder b = new StringBuilder();4769b.append('[');4770for (int i = 0; ; i++) {4771b.append(a[i]);4772if (i == iMax)4773return b.append(']').toString();4774b.append(", ");4775}4776}47774778/**4779* Returns a string representation of the contents of the specified array.4780* The string representation consists of a list of the array's elements,4781* enclosed in square brackets ({@code "[]"}). Adjacent elements4782* are separated by the characters {@code ", "} (a comma followed4783* by a space). Elements are converted to strings as by4784* {@code String.valueOf(byte)}. Returns {@code "null"} if4785* {@code a} is {@code null}.4786*4787* @param a the array whose string representation to return4788* @return a string representation of {@code a}4789* @since 1.54790*/4791public static String toString(byte[] a) {4792if (a == null)4793return "null";4794int iMax = a.length - 1;4795if (iMax == -1)4796return "[]";47974798StringBuilder b = new StringBuilder();4799b.append('[');4800for (int i = 0; ; i++) {4801b.append(a[i]);4802if (i == iMax)4803return b.append(']').toString();4804b.append(", ");4805}4806}48074808/**4809* Returns a string representation of the contents of the specified array.4810* The string representation consists of a list of the array's elements,4811* enclosed in square brackets ({@code "[]"}). Adjacent elements are4812* separated by the characters {@code ", "} (a comma followed by a4813* space). Elements are converted to strings as by4814* {@code String.valueOf(boolean)}. Returns {@code "null"} if4815* {@code a} is {@code null}.4816*4817* @param a the array whose string representation to return4818* @return a string representation of {@code a}4819* @since 1.54820*/4821public static String toString(boolean[] a) {4822if (a == null)4823return "null";4824int iMax = a.length - 1;4825if (iMax == -1)4826return "[]";48274828StringBuilder b = new StringBuilder();4829b.append('[');4830for (int i = 0; ; i++) {4831b.append(a[i]);4832if (i == iMax)4833return b.append(']').toString();4834b.append(", ");4835}4836}48374838/**4839* Returns a string representation of the contents of the specified array.4840* The string representation consists of a list of the array's elements,4841* enclosed in square brackets ({@code "[]"}). Adjacent elements are4842* separated by the characters {@code ", "} (a comma followed by a4843* space). Elements are converted to strings as by4844* {@code String.valueOf(float)}. Returns {@code "null"} if {@code a}4845* is {@code null}.4846*4847* @param a the array whose string representation to return4848* @return a string representation of {@code a}4849* @since 1.54850*/4851public static String toString(float[] a) {4852if (a == null)4853return "null";48544855int iMax = a.length - 1;4856if (iMax == -1)4857return "[]";48584859StringBuilder b = new StringBuilder();4860b.append('[');4861for (int i = 0; ; i++) {4862b.append(a[i]);4863if (i == iMax)4864return b.append(']').toString();4865b.append(", ");4866}4867}48684869/**4870* Returns a string representation of the contents of the specified array.4871* The string representation consists of a list of the array's elements,4872* enclosed in square brackets ({@code "[]"}). Adjacent elements are4873* separated by the characters {@code ", "} (a comma followed by a4874* space). Elements are converted to strings as by4875* {@code String.valueOf(double)}. Returns {@code "null"} if {@code a}4876* is {@code null}.4877*4878* @param a the array whose string representation to return4879* @return a string representation of {@code a}4880* @since 1.54881*/4882public static String toString(double[] a) {4883if (a == null)4884return "null";4885int iMax = a.length - 1;4886if (iMax == -1)4887return "[]";48884889StringBuilder b = new StringBuilder();4890b.append('[');4891for (int i = 0; ; i++) {4892b.append(a[i]);4893if (i == iMax)4894return b.append(']').toString();4895b.append(", ");4896}4897}48984899/**4900* Returns a string representation of the contents of the specified array.4901* If the array contains other arrays as elements, they are converted to4902* strings by the {@link Object#toString} method inherited from4903* {@code Object}, which describes their <i>identities</i> rather than4904* their contents.4905*4906* <p>The value returned by this method is equal to the value that would4907* be returned by {@code Arrays.asList(a).toString()}, unless {@code a}4908* is {@code null}, in which case {@code "null"} is returned.4909*4910* @param a the array whose string representation to return4911* @return a string representation of {@code a}4912* @see #deepToString(Object[])4913* @since 1.54914*/4915public static String toString(Object[] a) {4916if (a == null)4917return "null";49184919int iMax = a.length - 1;4920if (iMax == -1)4921return "[]";49224923StringBuilder b = new StringBuilder();4924b.append('[');4925for (int i = 0; ; i++) {4926b.append(String.valueOf(a[i]));4927if (i == iMax)4928return b.append(']').toString();4929b.append(", ");4930}4931}49324933/**4934* Returns a string representation of the "deep contents" of the specified4935* array. If the array contains other arrays as elements, the string4936* representation contains their contents and so on. This method is4937* designed for converting multidimensional arrays to strings.4938*4939* <p>The string representation consists of a list of the array's4940* elements, enclosed in square brackets ({@code "[]"}). Adjacent4941* elements are separated by the characters {@code ", "} (a comma4942* followed by a space). Elements are converted to strings as by4943* {@code String.valueOf(Object)}, unless they are themselves4944* arrays.4945*4946* <p>If an element {@code e} is an array of a primitive type, it is4947* converted to a string as by invoking the appropriate overloading of4948* {@code Arrays.toString(e)}. If an element {@code e} is an array of a4949* reference type, it is converted to a string as by invoking4950* this method recursively.4951*4952* <p>To avoid infinite recursion, if the specified array contains itself4953* as an element, or contains an indirect reference to itself through one4954* or more levels of arrays, the self-reference is converted to the string4955* {@code "[...]"}. For example, an array containing only a reference4956* to itself would be rendered as {@code "[[...]]"}.4957*4958* <p>This method returns {@code "null"} if the specified array4959* is {@code null}.4960*4961* @param a the array whose string representation to return4962* @return a string representation of {@code a}4963* @see #toString(Object[])4964* @since 1.54965*/4966public static String deepToString(Object[] a) {4967if (a == null)4968return "null";49694970int bufLen = 20 * a.length;4971if (a.length != 0 && bufLen <= 0)4972bufLen = Integer.MAX_VALUE;4973StringBuilder buf = new StringBuilder(bufLen);4974deepToString(a, buf, new HashSet<>());4975return buf.toString();4976}49774978private static void deepToString(Object[] a, StringBuilder buf,4979Set<Object[]> dejaVu) {4980if (a == null) {4981buf.append("null");4982return;4983}4984int iMax = a.length - 1;4985if (iMax == -1) {4986buf.append("[]");4987return;4988}49894990dejaVu.add(a);4991buf.append('[');4992for (int i = 0; ; i++) {49934994Object element = a[i];4995if (element == null) {4996buf.append("null");4997} else {4998Class<?> eClass = element.getClass();49995000if (eClass.isArray()) {5001if (eClass == byte[].class)5002buf.append(toString((byte[]) element));5003else if (eClass == short[].class)5004buf.append(toString((short[]) element));5005else if (eClass == int[].class)5006buf.append(toString((int[]) element));5007else if (eClass == long[].class)5008buf.append(toString((long[]) element));5009else if (eClass == char[].class)5010buf.append(toString((char[]) element));5011else if (eClass == float[].class)5012buf.append(toString((float[]) element));5013else if (eClass == double[].class)5014buf.append(toString((double[]) element));5015else if (eClass == boolean[].class)5016buf.append(toString((boolean[]) element));5017else { // element is an array of object references5018if (dejaVu.contains(element))5019buf.append("[...]");5020else5021deepToString((Object[])element, buf, dejaVu);5022}5023} else { // element is non-null and not an array5024buf.append(element.toString());5025}5026}5027if (i == iMax)5028break;5029buf.append(", ");5030}5031buf.append(']');5032dejaVu.remove(a);5033}503450355036/**5037* Set all elements of the specified array, using the provided5038* generator function to compute each element.5039*5040* <p>If the generator function throws an exception, it is relayed to5041* the caller and the array is left in an indeterminate state.5042*5043* @apiNote5044* Setting a subrange of an array, using a generator function to compute5045* each element, can be written as follows:5046* <pre>{@code5047* IntStream.range(startInclusive, endExclusive)5048* .forEach(i -> array[i] = generator.apply(i));5049* }</pre>5050*5051* @param <T> type of elements of the array5052* @param array array to be initialized5053* @param generator a function accepting an index and producing the desired5054* value for that position5055* @throws NullPointerException if the generator is null5056* @since 1.85057*/5058public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {5059Objects.requireNonNull(generator);5060for (int i = 0; i < array.length; i++)5061array[i] = generator.apply(i);5062}50635064/**5065* Set all elements of the specified array, in parallel, using the5066* provided generator function to compute each element.5067*5068* <p>If the generator function throws an exception, an unchecked exception5069* is thrown from {@code parallelSetAll} and the array is left in an5070* indeterminate state.5071*5072* @apiNote5073* Setting a subrange of an array, in parallel, using a generator function5074* to compute each element, can be written as follows:5075* <pre>{@code5076* IntStream.range(startInclusive, endExclusive)5077* .parallel()5078* .forEach(i -> array[i] = generator.apply(i));5079* }</pre>5080*5081* @param <T> type of elements of the array5082* @param array array to be initialized5083* @param generator a function accepting an index and producing the desired5084* value for that position5085* @throws NullPointerException if the generator is null5086* @since 1.85087*/5088public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {5089Objects.requireNonNull(generator);5090IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });5091}50925093/**5094* Set all elements of the specified array, using the provided5095* generator function to compute each element.5096*5097* <p>If the generator function throws an exception, it is relayed to5098* the caller and the array is left in an indeterminate state.5099*5100* @apiNote5101* Setting a subrange of an array, using a generator function to compute5102* each element, can be written as follows:5103* <pre>{@code5104* IntStream.range(startInclusive, endExclusive)5105* .forEach(i -> array[i] = generator.applyAsInt(i));5106* }</pre>5107*5108* @param array array to be initialized5109* @param generator a function accepting an index and producing the desired5110* value for that position5111* @throws NullPointerException if the generator is null5112* @since 1.85113*/5114public static void setAll(int[] array, IntUnaryOperator generator) {5115Objects.requireNonNull(generator);5116for (int i = 0; i < array.length; i++)5117array[i] = generator.applyAsInt(i);5118}51195120/**5121* Set all elements of the specified array, in parallel, using the5122* provided generator function to compute each element.5123*5124* <p>If the generator function throws an exception, an unchecked exception5125* is thrown from {@code parallelSetAll} and the array is left in an5126* indeterminate state.5127*5128* @apiNote5129* Setting a subrange of an array, in parallel, using a generator function5130* to compute each element, can be written as follows:5131* <pre>{@code5132* IntStream.range(startInclusive, endExclusive)5133* .parallel()5134* .forEach(i -> array[i] = generator.applyAsInt(i));5135* }</pre>5136*5137* @param array array to be initialized5138* @param generator a function accepting an index and producing the desired5139* value for that position5140* @throws NullPointerException if the generator is null5141* @since 1.85142*/5143public static void parallelSetAll(int[] array, IntUnaryOperator generator) {5144Objects.requireNonNull(generator);5145IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });5146}51475148/**5149* Set all elements of the specified array, using the provided5150* generator function to compute each element.5151*5152* <p>If the generator function throws an exception, it is relayed to5153* the caller and the array is left in an indeterminate state.5154*5155* @apiNote5156* Setting a subrange of an array, using a generator function to compute5157* each element, can be written as follows:5158* <pre>{@code5159* IntStream.range(startInclusive, endExclusive)5160* .forEach(i -> array[i] = generator.applyAsLong(i));5161* }</pre>5162*5163* @param array array to be initialized5164* @param generator a function accepting an index and producing the desired5165* value for that position5166* @throws NullPointerException if the generator is null5167* @since 1.85168*/5169public static void setAll(long[] array, IntToLongFunction generator) {5170Objects.requireNonNull(generator);5171for (int i = 0; i < array.length; i++)5172array[i] = generator.applyAsLong(i);5173}51745175/**5176* Set all elements of the specified array, in parallel, using the5177* provided generator function to compute each element.5178*5179* <p>If the generator function throws an exception, an unchecked exception5180* is thrown from {@code parallelSetAll} and the array is left in an5181* indeterminate state.5182*5183* @apiNote5184* Setting a subrange of an array, in parallel, using a generator function5185* to compute each element, can be written as follows:5186* <pre>{@code5187* IntStream.range(startInclusive, endExclusive)5188* .parallel()5189* .forEach(i -> array[i] = generator.applyAsLong(i));5190* }</pre>5191*5192* @param array array to be initialized5193* @param generator a function accepting an index and producing the desired5194* value for that position5195* @throws NullPointerException if the generator is null5196* @since 1.85197*/5198public static void parallelSetAll(long[] array, IntToLongFunction generator) {5199Objects.requireNonNull(generator);5200IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });5201}52025203/**5204* Set all elements of the specified array, using the provided5205* generator function to compute each element.5206*5207* <p>If the generator function throws an exception, it is relayed to5208* the caller and the array is left in an indeterminate state.5209*5210* @apiNote5211* Setting a subrange of an array, using a generator function to compute5212* each element, can be written as follows:5213* <pre>{@code5214* IntStream.range(startInclusive, endExclusive)5215* .forEach(i -> array[i] = generator.applyAsDouble(i));5216* }</pre>5217*5218* @param array array to be initialized5219* @param generator a function accepting an index and producing the desired5220* value for that position5221* @throws NullPointerException if the generator is null5222* @since 1.85223*/5224public static void setAll(double[] array, IntToDoubleFunction generator) {5225Objects.requireNonNull(generator);5226for (int i = 0; i < array.length; i++)5227array[i] = generator.applyAsDouble(i);5228}52295230/**5231* Set all elements of the specified array, in parallel, using the5232* provided generator function to compute each element.5233*5234* <p>If the generator function throws an exception, an unchecked exception5235* is thrown from {@code parallelSetAll} and the array is left in an5236* indeterminate state.5237*5238* @apiNote5239* Setting a subrange of an array, in parallel, using a generator function5240* to compute each element, can be written as follows:5241* <pre>{@code5242* IntStream.range(startInclusive, endExclusive)5243* .parallel()5244* .forEach(i -> array[i] = generator.applyAsDouble(i));5245* }</pre>5246*5247* @param array array to be initialized5248* @param generator a function accepting an index and producing the desired5249* value for that position5250* @throws NullPointerException if the generator is null5251* @since 1.85252*/5253public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {5254Objects.requireNonNull(generator);5255IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });5256}52575258/**5259* Returns a {@link Spliterator} covering all of the specified array.5260*5261* <p>The spliterator reports {@link Spliterator#SIZED},5262* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5263* {@link Spliterator#IMMUTABLE}.5264*5265* @param <T> type of elements5266* @param array the array, assumed to be unmodified during use5267* @return a spliterator for the array elements5268* @since 1.85269*/5270public static <T> Spliterator<T> spliterator(T[] array) {5271return Spliterators.spliterator(array,5272Spliterator.ORDERED | Spliterator.IMMUTABLE);5273}52745275/**5276* Returns a {@link Spliterator} covering the specified range of the5277* specified array.5278*5279* <p>The spliterator reports {@link Spliterator#SIZED},5280* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5281* {@link Spliterator#IMMUTABLE}.5282*5283* @param <T> type of elements5284* @param array the array, assumed to be unmodified during use5285* @param startInclusive the first index to cover, inclusive5286* @param endExclusive index immediately past the last index to cover5287* @return a spliterator for the array elements5288* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5289* negative, {@code endExclusive} is less than5290* {@code startInclusive}, or {@code endExclusive} is greater than5291* the array size5292* @since 1.85293*/5294public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {5295return Spliterators.spliterator(array, startInclusive, endExclusive,5296Spliterator.ORDERED | Spliterator.IMMUTABLE);5297}52985299/**5300* Returns a {@link Spliterator.OfInt} covering all of the specified array.5301*5302* <p>The spliterator reports {@link Spliterator#SIZED},5303* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5304* {@link Spliterator#IMMUTABLE}.5305*5306* @param array the array, assumed to be unmodified during use5307* @return a spliterator for the array elements5308* @since 1.85309*/5310public static Spliterator.OfInt spliterator(int[] array) {5311return Spliterators.spliterator(array,5312Spliterator.ORDERED | Spliterator.IMMUTABLE);5313}53145315/**5316* Returns a {@link Spliterator.OfInt} covering the specified range of the5317* specified array.5318*5319* <p>The spliterator reports {@link Spliterator#SIZED},5320* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5321* {@link Spliterator#IMMUTABLE}.5322*5323* @param array the array, assumed to be unmodified during use5324* @param startInclusive the first index to cover, inclusive5325* @param endExclusive index immediately past the last index to cover5326* @return a spliterator for the array elements5327* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5328* negative, {@code endExclusive} is less than5329* {@code startInclusive}, or {@code endExclusive} is greater than5330* the array size5331* @since 1.85332*/5333public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {5334return Spliterators.spliterator(array, startInclusive, endExclusive,5335Spliterator.ORDERED | Spliterator.IMMUTABLE);5336}53375338/**5339* Returns a {@link Spliterator.OfLong} covering all of the specified array.5340*5341* <p>The spliterator reports {@link Spliterator#SIZED},5342* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5343* {@link Spliterator#IMMUTABLE}.5344*5345* @param array the array, assumed to be unmodified during use5346* @return the spliterator for the array elements5347* @since 1.85348*/5349public static Spliterator.OfLong spliterator(long[] array) {5350return Spliterators.spliterator(array,5351Spliterator.ORDERED | Spliterator.IMMUTABLE);5352}53535354/**5355* Returns a {@link Spliterator.OfLong} covering the specified range of the5356* specified array.5357*5358* <p>The spliterator reports {@link Spliterator#SIZED},5359* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5360* {@link Spliterator#IMMUTABLE}.5361*5362* @param array the array, assumed to be unmodified during use5363* @param startInclusive the first index to cover, inclusive5364* @param endExclusive index immediately past the last index to cover5365* @return a spliterator for the array elements5366* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5367* negative, {@code endExclusive} is less than5368* {@code startInclusive}, or {@code endExclusive} is greater than5369* the array size5370* @since 1.85371*/5372public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {5373return Spliterators.spliterator(array, startInclusive, endExclusive,5374Spliterator.ORDERED | Spliterator.IMMUTABLE);5375}53765377/**5378* Returns a {@link Spliterator.OfDouble} covering all of the specified5379* array.5380*5381* <p>The spliterator reports {@link Spliterator#SIZED},5382* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5383* {@link Spliterator#IMMUTABLE}.5384*5385* @param array the array, assumed to be unmodified during use5386* @return a spliterator for the array elements5387* @since 1.85388*/5389public static Spliterator.OfDouble spliterator(double[] array) {5390return Spliterators.spliterator(array,5391Spliterator.ORDERED | Spliterator.IMMUTABLE);5392}53935394/**5395* Returns a {@link Spliterator.OfDouble} covering the specified range of5396* the specified array.5397*5398* <p>The spliterator reports {@link Spliterator#SIZED},5399* {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and5400* {@link Spliterator#IMMUTABLE}.5401*5402* @param array the array, assumed to be unmodified during use5403* @param startInclusive the first index to cover, inclusive5404* @param endExclusive index immediately past the last index to cover5405* @return a spliterator for the array elements5406* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5407* negative, {@code endExclusive} is less than5408* {@code startInclusive}, or {@code endExclusive} is greater than5409* the array size5410* @since 1.85411*/5412public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {5413return Spliterators.spliterator(array, startInclusive, endExclusive,5414Spliterator.ORDERED | Spliterator.IMMUTABLE);5415}54165417/**5418* Returns a sequential {@link Stream} with the specified array as its5419* source.5420*5421* @param <T> The type of the array elements5422* @param array The array, assumed to be unmodified during use5423* @return a {@code Stream} for the array5424* @since 1.85425*/5426public static <T> Stream<T> stream(T[] array) {5427return stream(array, 0, array.length);5428}54295430/**5431* Returns a sequential {@link Stream} with the specified range of the5432* specified array as its source.5433*5434* @param <T> the type of the array elements5435* @param array the array, assumed to be unmodified during use5436* @param startInclusive the first index to cover, inclusive5437* @param endExclusive index immediately past the last index to cover5438* @return a {@code Stream} for the array range5439* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5440* negative, {@code endExclusive} is less than5441* {@code startInclusive}, or {@code endExclusive} is greater than5442* the array size5443* @since 1.85444*/5445public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {5446return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);5447}54485449/**5450* Returns a sequential {@link IntStream} with the specified array as its5451* source.5452*5453* @param array the array, assumed to be unmodified during use5454* @return an {@code IntStream} for the array5455* @since 1.85456*/5457public static IntStream stream(int[] array) {5458return stream(array, 0, array.length);5459}54605461/**5462* Returns a sequential {@link IntStream} with the specified range of the5463* specified array as its source.5464*5465* @param array the array, assumed to be unmodified during use5466* @param startInclusive the first index to cover, inclusive5467* @param endExclusive index immediately past the last index to cover5468* @return an {@code IntStream} for the array range5469* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5470* negative, {@code endExclusive} is less than5471* {@code startInclusive}, or {@code endExclusive} is greater than5472* the array size5473* @since 1.85474*/5475public static IntStream stream(int[] array, int startInclusive, int endExclusive) {5476return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);5477}54785479/**5480* Returns a sequential {@link LongStream} with the specified array as its5481* source.5482*5483* @param array the array, assumed to be unmodified during use5484* @return a {@code LongStream} for the array5485* @since 1.85486*/5487public static LongStream stream(long[] array) {5488return stream(array, 0, array.length);5489}54905491/**5492* Returns a sequential {@link LongStream} with the specified range of the5493* specified array as its source.5494*5495* @param array the array, assumed to be unmodified during use5496* @param startInclusive the first index to cover, inclusive5497* @param endExclusive index immediately past the last index to cover5498* @return a {@code LongStream} for the array range5499* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5500* negative, {@code endExclusive} is less than5501* {@code startInclusive}, or {@code endExclusive} is greater than5502* the array size5503* @since 1.85504*/5505public static LongStream stream(long[] array, int startInclusive, int endExclusive) {5506return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);5507}55085509/**5510* Returns a sequential {@link DoubleStream} with the specified array as its5511* source.5512*5513* @param array the array, assumed to be unmodified during use5514* @return a {@code DoubleStream} for the array5515* @since 1.85516*/5517public static DoubleStream stream(double[] array) {5518return stream(array, 0, array.length);5519}55205521/**5522* Returns a sequential {@link DoubleStream} with the specified range of the5523* specified array as its source.5524*5525* @param array the array, assumed to be unmodified during use5526* @param startInclusive the first index to cover, inclusive5527* @param endExclusive index immediately past the last index to cover5528* @return a {@code DoubleStream} for the array range5529* @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is5530* negative, {@code endExclusive} is less than5531* {@code startInclusive}, or {@code endExclusive} is greater than5532* the array size5533* @since 1.85534*/5535public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {5536return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);5537}553855395540// Comparison methods55415542// Compare boolean55435544/**5545* Compares two {@code boolean} arrays lexicographically.5546*5547* <p>If the two arrays share a common prefix then the lexicographic5548* comparison is the result of comparing two elements, as if by5549* {@link Boolean#compare(boolean, boolean)}, at an index within the5550* respective arrays that is the prefix length.5551* Otherwise, one array is a proper prefix of the other and, lexicographic5552* comparison is the result of comparing the two array lengths.5553* (See {@link #mismatch(boolean[], boolean[])} for the definition of a5554* common and proper prefix.)5555*5556* <p>A {@code null} array reference is considered lexicographically less5557* than a non-{@code null} array reference. Two {@code null} array5558* references are considered equal.5559*5560* <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},5561* more specifically the following holds for arrays {@code a} and {@code b}:5562* <pre>{@code5563* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)5564* }</pre>5565*5566* @apiNote5567* <p>This method behaves as if (for non-{@code null} array references):5568* <pre>{@code5569* int i = Arrays.mismatch(a, b);5570* if (i >= 0 && i < Math.min(a.length, b.length))5571* return Boolean.compare(a[i], b[i]);5572* return a.length - b.length;5573* }</pre>5574*5575* @param a the first array to compare5576* @param b the second array to compare5577* @return the value {@code 0} if the first and second array are equal and5578* contain the same elements in the same order;5579* a value less than {@code 0} if the first array is5580* lexicographically less than the second array; and5581* a value greater than {@code 0} if the first array is5582* lexicographically greater than the second array5583* @since 95584*/5585public static int compare(boolean[] a, boolean[] b) {5586if (a == b)5587return 0;5588if (a == null || b == null)5589return a == null ? -1 : 1;55905591int i = ArraysSupport.mismatch(a, b,5592Math.min(a.length, b.length));5593if (i >= 0) {5594return Boolean.compare(a[i], b[i]);5595}55965597return a.length - b.length;5598}55995600/**5601* Compares two {@code boolean} arrays lexicographically over the specified5602* ranges.5603*5604* <p>If the two arrays, over the specified ranges, share a common prefix5605* then the lexicographic comparison is the result of comparing two5606* elements, as if by {@link Boolean#compare(boolean, boolean)}, at a5607* relative index within the respective arrays that is the length of the5608* prefix.5609* Otherwise, one array is a proper prefix of the other and, lexicographic5610* comparison is the result of comparing the two range lengths.5611* (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the5612* definition of a common and proper prefix.)5613*5614* <p>The comparison is consistent with5615* {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more5616* specifically the following holds for arrays {@code a} and {@code b} with5617* specified ranges [{@code aFromIndex}, {@code atoIndex}) and5618* [{@code bFromIndex}, {@code btoIndex}) respectively:5619* <pre>{@code5620* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==5621* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)5622* }</pre>5623*5624* @apiNote5625* <p>This method behaves as if:5626* <pre>{@code5627* int i = Arrays.mismatch(a, aFromIndex, aToIndex,5628* b, bFromIndex, bToIndex);5629* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))5630* return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);5631* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);5632* }</pre>5633*5634* @param a the first array to compare5635* @param aFromIndex the index (inclusive) of the first element in the5636* first array to be compared5637* @param aToIndex the index (exclusive) of the last element in the5638* first array to be compared5639* @param b the second array to compare5640* @param bFromIndex the index (inclusive) of the first element in the5641* second array to be compared5642* @param bToIndex the index (exclusive) of the last element in the5643* second array to be compared5644* @return the value {@code 0} if, over the specified ranges, the first and5645* second array are equal and contain the same elements in the same5646* order;5647* a value less than {@code 0} if, over the specified ranges, the5648* first array is lexicographically less than the second array; and5649* a value greater than {@code 0} if, over the specified ranges, the5650* first array is lexicographically greater than the second array5651* @throws IllegalArgumentException5652* if {@code aFromIndex > aToIndex} or5653* if {@code bFromIndex > bToIndex}5654* @throws ArrayIndexOutOfBoundsException5655* if {@code aFromIndex < 0 or aToIndex > a.length} or5656* if {@code bFromIndex < 0 or bToIndex > b.length}5657* @throws NullPointerException5658* if either array is {@code null}5659* @since 95660*/5661public static int compare(boolean[] a, int aFromIndex, int aToIndex,5662boolean[] b, int bFromIndex, int bToIndex) {5663rangeCheck(a.length, aFromIndex, aToIndex);5664rangeCheck(b.length, bFromIndex, bToIndex);56655666int aLength = aToIndex - aFromIndex;5667int bLength = bToIndex - bFromIndex;5668int i = ArraysSupport.mismatch(a, aFromIndex,5669b, bFromIndex,5670Math.min(aLength, bLength));5671if (i >= 0) {5672return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);5673}56745675return aLength - bLength;5676}56775678// Compare byte56795680/**5681* Compares two {@code byte} arrays lexicographically.5682*5683* <p>If the two arrays share a common prefix then the lexicographic5684* comparison is the result of comparing two elements, as if by5685* {@link Byte#compare(byte, byte)}, at an index within the respective5686* arrays that is the prefix length.5687* Otherwise, one array is a proper prefix of the other and, lexicographic5688* comparison is the result of comparing the two array lengths.5689* (See {@link #mismatch(byte[], byte[])} for the definition of a common and5690* proper prefix.)5691*5692* <p>A {@code null} array reference is considered lexicographically less5693* than a non-{@code null} array reference. Two {@code null} array5694* references are considered equal.5695*5696* <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},5697* more specifically the following holds for arrays {@code a} and {@code b}:5698* <pre>{@code5699* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)5700* }</pre>5701*5702* @apiNote5703* <p>This method behaves as if (for non-{@code null} array references):5704* <pre>{@code5705* int i = Arrays.mismatch(a, b);5706* if (i >= 0 && i < Math.min(a.length, b.length))5707* return Byte.compare(a[i], b[i]);5708* return a.length - b.length;5709* }</pre>5710*5711* @param a the first array to compare5712* @param b the second array to compare5713* @return the value {@code 0} if the first and second array are equal and5714* contain the same elements in the same order;5715* a value less than {@code 0} if the first array is5716* lexicographically less than the second array; and5717* a value greater than {@code 0} if the first array is5718* lexicographically greater than the second array5719* @since 95720*/5721public static int compare(byte[] a, byte[] b) {5722if (a == b)5723return 0;5724if (a == null || b == null)5725return a == null ? -1 : 1;57265727int i = ArraysSupport.mismatch(a, b,5728Math.min(a.length, b.length));5729if (i >= 0) {5730return Byte.compare(a[i], b[i]);5731}57325733return a.length - b.length;5734}57355736/**5737* Compares two {@code byte} arrays lexicographically over the specified5738* ranges.5739*5740* <p>If the two arrays, over the specified ranges, share a common prefix5741* then the lexicographic comparison is the result of comparing two5742* elements, as if by {@link Byte#compare(byte, byte)}, at a relative index5743* within the respective arrays that is the length of the prefix.5744* Otherwise, one array is a proper prefix of the other and, lexicographic5745* comparison is the result of comparing the two range lengths.5746* (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the5747* definition of a common and proper prefix.)5748*5749* <p>The comparison is consistent with5750* {@link #equals(byte[], int, int, byte[], int, int) equals}, more5751* specifically the following holds for arrays {@code a} and {@code b} with5752* specified ranges [{@code aFromIndex}, {@code atoIndex}) and5753* [{@code bFromIndex}, {@code btoIndex}) respectively:5754* <pre>{@code5755* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==5756* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)5757* }</pre>5758*5759* @apiNote5760* <p>This method behaves as if:5761* <pre>{@code5762* int i = Arrays.mismatch(a, aFromIndex, aToIndex,5763* b, bFromIndex, bToIndex);5764* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))5765* return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);5766* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);5767* }</pre>5768*5769* @param a the first array to compare5770* @param aFromIndex the index (inclusive) of the first element in the5771* first array to be compared5772* @param aToIndex the index (exclusive) of the last element in the5773* first array to be compared5774* @param b the second array to compare5775* @param bFromIndex the index (inclusive) of the first element in the5776* second array to be compared5777* @param bToIndex the index (exclusive) of the last element in the5778* second array to be compared5779* @return the value {@code 0} if, over the specified ranges, the first and5780* second array are equal and contain the same elements in the same5781* order;5782* a value less than {@code 0} if, over the specified ranges, the5783* first array is lexicographically less than the second array; and5784* a value greater than {@code 0} if, over the specified ranges, the5785* first array is lexicographically greater than the second array5786* @throws IllegalArgumentException5787* if {@code aFromIndex > aToIndex} or5788* if {@code bFromIndex > bToIndex}5789* @throws ArrayIndexOutOfBoundsException5790* if {@code aFromIndex < 0 or aToIndex > a.length} or5791* if {@code bFromIndex < 0 or bToIndex > b.length}5792* @throws NullPointerException5793* if either array is {@code null}5794* @since 95795*/5796public static int compare(byte[] a, int aFromIndex, int aToIndex,5797byte[] b, int bFromIndex, int bToIndex) {5798rangeCheck(a.length, aFromIndex, aToIndex);5799rangeCheck(b.length, bFromIndex, bToIndex);58005801int aLength = aToIndex - aFromIndex;5802int bLength = bToIndex - bFromIndex;5803int i = ArraysSupport.mismatch(a, aFromIndex,5804b, bFromIndex,5805Math.min(aLength, bLength));5806if (i >= 0) {5807return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);5808}58095810return aLength - bLength;5811}58125813/**5814* Compares two {@code byte} arrays lexicographically, numerically treating5815* elements as unsigned.5816*5817* <p>If the two arrays share a common prefix then the lexicographic5818* comparison is the result of comparing two elements, as if by5819* {@link Byte#compareUnsigned(byte, byte)}, at an index within the5820* respective arrays that is the prefix length.5821* Otherwise, one array is a proper prefix of the other and, lexicographic5822* comparison is the result of comparing the two array lengths.5823* (See {@link #mismatch(byte[], byte[])} for the definition of a common5824* and proper prefix.)5825*5826* <p>A {@code null} array reference is considered lexicographically less5827* than a non-{@code null} array reference. Two {@code null} array5828* references are considered equal.5829*5830* @apiNote5831* <p>This method behaves as if (for non-{@code null} array references):5832* <pre>{@code5833* int i = Arrays.mismatch(a, b);5834* if (i >= 0 && i < Math.min(a.length, b.length))5835* return Byte.compareUnsigned(a[i], b[i]);5836* return a.length - b.length;5837* }</pre>5838*5839* @param a the first array to compare5840* @param b the second array to compare5841* @return the value {@code 0} if the first and second array are5842* equal and contain the same elements in the same order;5843* a value less than {@code 0} if the first array is5844* lexicographically less than the second array; and5845* a value greater than {@code 0} if the first array is5846* lexicographically greater than the second array5847* @since 95848*/5849public static int compareUnsigned(byte[] a, byte[] b) {5850if (a == b)5851return 0;5852if (a == null || b == null)5853return a == null ? -1 : 1;58545855int i = ArraysSupport.mismatch(a, b,5856Math.min(a.length, b.length));5857if (i >= 0) {5858return Byte.compareUnsigned(a[i], b[i]);5859}58605861return a.length - b.length;5862}586358645865/**5866* Compares two {@code byte} arrays lexicographically over the specified5867* ranges, numerically treating elements as unsigned.5868*5869* <p>If the two arrays, over the specified ranges, share a common prefix5870* then the lexicographic comparison is the result of comparing two5871* elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a5872* relative index within the respective arrays that is the length of the5873* prefix.5874* Otherwise, one array is a proper prefix of the other and, lexicographic5875* comparison is the result of comparing the two range lengths.5876* (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the5877* definition of a common and proper prefix.)5878*5879* @apiNote5880* <p>This method behaves as if:5881* <pre>{@code5882* int i = Arrays.mismatch(a, aFromIndex, aToIndex,5883* b, bFromIndex, bToIndex);5884* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))5885* return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);5886* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);5887* }</pre>5888*5889* @param a the first array to compare5890* @param aFromIndex the index (inclusive) of the first element in the5891* first array to be compared5892* @param aToIndex the index (exclusive) of the last element in the5893* first array to be compared5894* @param b the second array to compare5895* @param bFromIndex the index (inclusive) of the first element in the5896* second array to be compared5897* @param bToIndex the index (exclusive) of the last element in the5898* second array to be compared5899* @return the value {@code 0} if, over the specified ranges, the first and5900* second array are equal and contain the same elements in the same5901* order;5902* a value less than {@code 0} if, over the specified ranges, the5903* first array is lexicographically less than the second array; and5904* a value greater than {@code 0} if, over the specified ranges, the5905* first array is lexicographically greater than the second array5906* @throws IllegalArgumentException5907* if {@code aFromIndex > aToIndex} or5908* if {@code bFromIndex > bToIndex}5909* @throws ArrayIndexOutOfBoundsException5910* if {@code aFromIndex < 0 or aToIndex > a.length} or5911* if {@code bFromIndex < 0 or bToIndex > b.length}5912* @throws NullPointerException5913* if either array is null5914* @since 95915*/5916public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,5917byte[] b, int bFromIndex, int bToIndex) {5918rangeCheck(a.length, aFromIndex, aToIndex);5919rangeCheck(b.length, bFromIndex, bToIndex);59205921int aLength = aToIndex - aFromIndex;5922int bLength = bToIndex - bFromIndex;5923int i = ArraysSupport.mismatch(a, aFromIndex,5924b, bFromIndex,5925Math.min(aLength, bLength));5926if (i >= 0) {5927return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);5928}59295930return aLength - bLength;5931}59325933// Compare short59345935/**5936* Compares two {@code short} arrays lexicographically.5937*5938* <p>If the two arrays share a common prefix then the lexicographic5939* comparison is the result of comparing two elements, as if by5940* {@link Short#compare(short, short)}, at an index within the respective5941* arrays that is the prefix length.5942* Otherwise, one array is a proper prefix of the other and, lexicographic5943* comparison is the result of comparing the two array lengths.5944* (See {@link #mismatch(short[], short[])} for the definition of a common5945* and proper prefix.)5946*5947* <p>A {@code null} array reference is considered lexicographically less5948* than a non-{@code null} array reference. Two {@code null} array5949* references are considered equal.5950*5951* <p>The comparison is consistent with {@link #equals(short[], short[]) equals},5952* more specifically the following holds for arrays {@code a} and {@code b}:5953* <pre>{@code5954* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)5955* }</pre>5956*5957* @apiNote5958* <p>This method behaves as if (for non-{@code null} array references):5959* <pre>{@code5960* int i = Arrays.mismatch(a, b);5961* if (i >= 0 && i < Math.min(a.length, b.length))5962* return Short.compare(a[i], b[i]);5963* return a.length - b.length;5964* }</pre>5965*5966* @param a the first array to compare5967* @param b the second array to compare5968* @return the value {@code 0} if the first and second array are equal and5969* contain the same elements in the same order;5970* a value less than {@code 0} if the first array is5971* lexicographically less than the second array; and5972* a value greater than {@code 0} if the first array is5973* lexicographically greater than the second array5974* @since 95975*/5976public static int compare(short[] a, short[] b) {5977if (a == b)5978return 0;5979if (a == null || b == null)5980return a == null ? -1 : 1;59815982int i = ArraysSupport.mismatch(a, b,5983Math.min(a.length, b.length));5984if (i >= 0) {5985return Short.compare(a[i], b[i]);5986}59875988return a.length - b.length;5989}59905991/**5992* Compares two {@code short} arrays lexicographically over the specified5993* ranges.5994*5995* <p>If the two arrays, over the specified ranges, share a common prefix5996* then the lexicographic comparison is the result of comparing two5997* elements, as if by {@link Short#compare(short, short)}, at a relative5998* index within the respective arrays that is the length of the prefix.5999* Otherwise, one array is a proper prefix of the other and, lexicographic6000* comparison is the result of comparing the two range lengths.6001* (See {@link #mismatch(short[], int, int, short[], int, int)} for the6002* definition of a common and proper prefix.)6003*6004* <p>The comparison is consistent with6005* {@link #equals(short[], int, int, short[], int, int) equals}, more6006* specifically the following holds for arrays {@code a} and {@code b} with6007* specified ranges [{@code aFromIndex}, {@code atoIndex}) and6008* [{@code bFromIndex}, {@code btoIndex}) respectively:6009* <pre>{@code6010* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==6011* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)6012* }</pre>6013*6014* @apiNote6015* <p>This method behaves as if:6016* <pre>{@code6017* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6018* b, bFromIndex, bToIndex);6019* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6020* return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);6021* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6022* }</pre>6023*6024* @param a the first array to compare6025* @param aFromIndex the index (inclusive) of the first element in the6026* first array to be compared6027* @param aToIndex the index (exclusive) of the last element in the6028* first array to be compared6029* @param b the second array to compare6030* @param bFromIndex the index (inclusive) of the first element in the6031* second array to be compared6032* @param bToIndex the index (exclusive) of the last element in the6033* second array to be compared6034* @return the value {@code 0} if, over the specified ranges, the first and6035* second array are equal and contain the same elements in the same6036* order;6037* a value less than {@code 0} if, over the specified ranges, the6038* first array is lexicographically less than the second array; and6039* a value greater than {@code 0} if, over the specified ranges, the6040* first array is lexicographically greater than the second array6041* @throws IllegalArgumentException6042* if {@code aFromIndex > aToIndex} or6043* if {@code bFromIndex > bToIndex}6044* @throws ArrayIndexOutOfBoundsException6045* if {@code aFromIndex < 0 or aToIndex > a.length} or6046* if {@code bFromIndex < 0 or bToIndex > b.length}6047* @throws NullPointerException6048* if either array is {@code null}6049* @since 96050*/6051public static int compare(short[] a, int aFromIndex, int aToIndex,6052short[] b, int bFromIndex, int bToIndex) {6053rangeCheck(a.length, aFromIndex, aToIndex);6054rangeCheck(b.length, bFromIndex, bToIndex);60556056int aLength = aToIndex - aFromIndex;6057int bLength = bToIndex - bFromIndex;6058int i = ArraysSupport.mismatch(a, aFromIndex,6059b, bFromIndex,6060Math.min(aLength, bLength));6061if (i >= 0) {6062return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);6063}60646065return aLength - bLength;6066}60676068/**6069* Compares two {@code short} arrays lexicographically, numerically treating6070* elements as unsigned.6071*6072* <p>If the two arrays share a common prefix then the lexicographic6073* comparison is the result of comparing two elements, as if by6074* {@link Short#compareUnsigned(short, short)}, at an index within the6075* respective arrays that is the prefix length.6076* Otherwise, one array is a proper prefix of the other and, lexicographic6077* comparison is the result of comparing the two array lengths.6078* (See {@link #mismatch(short[], short[])} for the definition of a common6079* and proper prefix.)6080*6081* <p>A {@code null} array reference is considered lexicographically less6082* than a non-{@code null} array reference. Two {@code null} array6083* references are considered equal.6084*6085* @apiNote6086* <p>This method behaves as if (for non-{@code null} array references):6087* <pre>{@code6088* int i = Arrays.mismatch(a, b);6089* if (i >= 0 && i < Math.min(a.length, b.length))6090* return Short.compareUnsigned(a[i], b[i]);6091* return a.length - b.length;6092* }</pre>6093*6094* @param a the first array to compare6095* @param b the second array to compare6096* @return the value {@code 0} if the first and second array are6097* equal and contain the same elements in the same order;6098* a value less than {@code 0} if the first array is6099* lexicographically less than the second array; and6100* a value greater than {@code 0} if the first array is6101* lexicographically greater than the second array6102* @since 96103*/6104public static int compareUnsigned(short[] a, short[] b) {6105if (a == b)6106return 0;6107if (a == null || b == null)6108return a == null ? -1 : 1;61096110int i = ArraysSupport.mismatch(a, b,6111Math.min(a.length, b.length));6112if (i >= 0) {6113return Short.compareUnsigned(a[i], b[i]);6114}61156116return a.length - b.length;6117}61186119/**6120* Compares two {@code short} arrays lexicographically over the specified6121* ranges, numerically treating elements as unsigned.6122*6123* <p>If the two arrays, over the specified ranges, share a common prefix6124* then the lexicographic comparison is the result of comparing two6125* elements, as if by {@link Short#compareUnsigned(short, short)}, at a6126* relative index within the respective arrays that is the length of the6127* prefix.6128* Otherwise, one array is a proper prefix of the other and, lexicographic6129* comparison is the result of comparing the two range lengths.6130* (See {@link #mismatch(short[], int, int, short[], int, int)} for the6131* definition of a common and proper prefix.)6132*6133* @apiNote6134* <p>This method behaves as if:6135* <pre>{@code6136* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6137* b, bFromIndex, bToIndex);6138* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6139* return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6140* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6141* }</pre>6142*6143* @param a the first array to compare6144* @param aFromIndex the index (inclusive) of the first element in the6145* first array to be compared6146* @param aToIndex the index (exclusive) of the last element in the6147* first array to be compared6148* @param b the second array to compare6149* @param bFromIndex the index (inclusive) of the first element in the6150* second array to be compared6151* @param bToIndex the index (exclusive) of the last element in the6152* second array to be compared6153* @return the value {@code 0} if, over the specified ranges, the first and6154* second array are equal and contain the same elements in the same6155* order;6156* a value less than {@code 0} if, over the specified ranges, the6157* first array is lexicographically less than the second array; and6158* a value greater than {@code 0} if, over the specified ranges, the6159* first array is lexicographically greater than the second array6160* @throws IllegalArgumentException6161* if {@code aFromIndex > aToIndex} or6162* if {@code bFromIndex > bToIndex}6163* @throws ArrayIndexOutOfBoundsException6164* if {@code aFromIndex < 0 or aToIndex > a.length} or6165* if {@code bFromIndex < 0 or bToIndex > b.length}6166* @throws NullPointerException6167* if either array is null6168* @since 96169*/6170public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,6171short[] b, int bFromIndex, int bToIndex) {6172rangeCheck(a.length, aFromIndex, aToIndex);6173rangeCheck(b.length, bFromIndex, bToIndex);61746175int aLength = aToIndex - aFromIndex;6176int bLength = bToIndex - bFromIndex;6177int i = ArraysSupport.mismatch(a, aFromIndex,6178b, bFromIndex,6179Math.min(aLength, bLength));6180if (i >= 0) {6181return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6182}61836184return aLength - bLength;6185}61866187// Compare char61886189/**6190* Compares two {@code char} arrays lexicographically.6191*6192* <p>If the two arrays share a common prefix then the lexicographic6193* comparison is the result of comparing two elements, as if by6194* {@link Character#compare(char, char)}, at an index within the respective6195* arrays that is the prefix length.6196* Otherwise, one array is a proper prefix of the other and, lexicographic6197* comparison is the result of comparing the two array lengths.6198* (See {@link #mismatch(char[], char[])} for the definition of a common and6199* proper prefix.)6200*6201* <p>A {@code null} array reference is considered lexicographically less6202* than a non-{@code null} array reference. Two {@code null} array6203* references are considered equal.6204*6205* <p>The comparison is consistent with {@link #equals(char[], char[]) equals},6206* more specifically the following holds for arrays {@code a} and {@code b}:6207* <pre>{@code6208* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)6209* }</pre>6210*6211* @apiNote6212* <p>This method behaves as if (for non-{@code null} array references):6213* <pre>{@code6214* int i = Arrays.mismatch(a, b);6215* if (i >= 0 && i < Math.min(a.length, b.length))6216* return Character.compare(a[i], b[i]);6217* return a.length - b.length;6218* }</pre>6219*6220* @param a the first array to compare6221* @param b the second array to compare6222* @return the value {@code 0} if the first and second array are equal and6223* contain the same elements in the same order;6224* a value less than {@code 0} if the first array is6225* lexicographically less than the second array; and6226* a value greater than {@code 0} if the first array is6227* lexicographically greater than the second array6228* @since 96229*/6230public static int compare(char[] a, char[] b) {6231if (a == b)6232return 0;6233if (a == null || b == null)6234return a == null ? -1 : 1;62356236int i = ArraysSupport.mismatch(a, b,6237Math.min(a.length, b.length));6238if (i >= 0) {6239return Character.compare(a[i], b[i]);6240}62416242return a.length - b.length;6243}62446245/**6246* Compares two {@code char} arrays lexicographically over the specified6247* ranges.6248*6249* <p>If the two arrays, over the specified ranges, share a common prefix6250* then the lexicographic comparison is the result of comparing two6251* elements, as if by {@link Character#compare(char, char)}, at a relative6252* index within the respective arrays that is the length of the prefix.6253* Otherwise, one array is a proper prefix of the other and, lexicographic6254* comparison is the result of comparing the two range lengths.6255* (See {@link #mismatch(char[], int, int, char[], int, int)} for the6256* definition of a common and proper prefix.)6257*6258* <p>The comparison is consistent with6259* {@link #equals(char[], int, int, char[], int, int) equals}, more6260* specifically the following holds for arrays {@code a} and {@code b} with6261* specified ranges [{@code aFromIndex}, {@code atoIndex}) and6262* [{@code bFromIndex}, {@code btoIndex}) respectively:6263* <pre>{@code6264* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==6265* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)6266* }</pre>6267*6268* @apiNote6269* <p>This method behaves as if:6270* <pre>{@code6271* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6272* b, bFromIndex, bToIndex);6273* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6274* return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);6275* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6276* }</pre>6277*6278* @param a the first array to compare6279* @param aFromIndex the index (inclusive) of the first element in the6280* first array to be compared6281* @param aToIndex the index (exclusive) of the last element in the6282* first array to be compared6283* @param b the second array to compare6284* @param bFromIndex the index (inclusive) of the first element in the6285* second array to be compared6286* @param bToIndex the index (exclusive) of the last element in the6287* second array to be compared6288* @return the value {@code 0} if, over the specified ranges, the first and6289* second array are equal and contain the same elements in the same6290* order;6291* a value less than {@code 0} if, over the specified ranges, the6292* first array is lexicographically less than the second array; and6293* a value greater than {@code 0} if, over the specified ranges, the6294* first array is lexicographically greater than the second array6295* @throws IllegalArgumentException6296* if {@code aFromIndex > aToIndex} or6297* if {@code bFromIndex > bToIndex}6298* @throws ArrayIndexOutOfBoundsException6299* if {@code aFromIndex < 0 or aToIndex > a.length} or6300* if {@code bFromIndex < 0 or bToIndex > b.length}6301* @throws NullPointerException6302* if either array is {@code null}6303* @since 96304*/6305public static int compare(char[] a, int aFromIndex, int aToIndex,6306char[] b, int bFromIndex, int bToIndex) {6307rangeCheck(a.length, aFromIndex, aToIndex);6308rangeCheck(b.length, bFromIndex, bToIndex);63096310int aLength = aToIndex - aFromIndex;6311int bLength = bToIndex - bFromIndex;6312int i = ArraysSupport.mismatch(a, aFromIndex,6313b, bFromIndex,6314Math.min(aLength, bLength));6315if (i >= 0) {6316return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);6317}63186319return aLength - bLength;6320}63216322// Compare int63236324/**6325* Compares two {@code int} arrays lexicographically.6326*6327* <p>If the two arrays share a common prefix then the lexicographic6328* comparison is the result of comparing two elements, as if by6329* {@link Integer#compare(int, int)}, at an index within the respective6330* arrays that is the prefix length.6331* Otherwise, one array is a proper prefix of the other and, lexicographic6332* comparison is the result of comparing the two array lengths.6333* (See {@link #mismatch(int[], int[])} for the definition of a common and6334* proper prefix.)6335*6336* <p>A {@code null} array reference is considered lexicographically less6337* than a non-{@code null} array reference. Two {@code null} array6338* references are considered equal.6339*6340* <p>The comparison is consistent with {@link #equals(int[], int[]) equals},6341* more specifically the following holds for arrays {@code a} and {@code b}:6342* <pre>{@code6343* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)6344* }</pre>6345*6346* @apiNote6347* <p>This method behaves as if (for non-{@code null} array references):6348* <pre>{@code6349* int i = Arrays.mismatch(a, b);6350* if (i >= 0 && i < Math.min(a.length, b.length))6351* return Integer.compare(a[i], b[i]);6352* return a.length - b.length;6353* }</pre>6354*6355* @param a the first array to compare6356* @param b the second array to compare6357* @return the value {@code 0} if the first and second array are equal and6358* contain the same elements in the same order;6359* a value less than {@code 0} if the first array is6360* lexicographically less than the second array; and6361* a value greater than {@code 0} if the first array is6362* lexicographically greater than the second array6363* @since 96364*/6365public static int compare(int[] a, int[] b) {6366if (a == b)6367return 0;6368if (a == null || b == null)6369return a == null ? -1 : 1;63706371int i = ArraysSupport.mismatch(a, b,6372Math.min(a.length, b.length));6373if (i >= 0) {6374return Integer.compare(a[i], b[i]);6375}63766377return a.length - b.length;6378}63796380/**6381* Compares two {@code int} arrays lexicographically over the specified6382* ranges.6383*6384* <p>If the two arrays, over the specified ranges, share a common prefix6385* then the lexicographic comparison is the result of comparing two6386* elements, as if by {@link Integer#compare(int, int)}, at a relative index6387* within the respective arrays that is the length of the prefix.6388* Otherwise, one array is a proper prefix of the other and, lexicographic6389* comparison is the result of comparing the two range lengths.6390* (See {@link #mismatch(int[], int, int, int[], int, int)} for the6391* definition of a common and proper prefix.)6392*6393* <p>The comparison is consistent with6394* {@link #equals(int[], int, int, int[], int, int) equals}, more6395* specifically the following holds for arrays {@code a} and {@code b} with6396* specified ranges [{@code aFromIndex}, {@code atoIndex}) and6397* [{@code bFromIndex}, {@code btoIndex}) respectively:6398* <pre>{@code6399* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==6400* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)6401* }</pre>6402*6403* @apiNote6404* <p>This method behaves as if:6405* <pre>{@code6406* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6407* b, bFromIndex, bToIndex);6408* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6409* return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);6410* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6411* }</pre>6412*6413* @param a the first array to compare6414* @param aFromIndex the index (inclusive) of the first element in the6415* first array to be compared6416* @param aToIndex the index (exclusive) of the last element in the6417* first array to be compared6418* @param b the second array to compare6419* @param bFromIndex the index (inclusive) of the first element in the6420* second array to be compared6421* @param bToIndex the index (exclusive) of the last element in the6422* second array to be compared6423* @return the value {@code 0} if, over the specified ranges, the first and6424* second array are equal and contain the same elements in the same6425* order;6426* a value less than {@code 0} if, over the specified ranges, the6427* first array is lexicographically less than the second array; and6428* a value greater than {@code 0} if, over the specified ranges, the6429* first array is lexicographically greater than the second array6430* @throws IllegalArgumentException6431* if {@code aFromIndex > aToIndex} or6432* if {@code bFromIndex > bToIndex}6433* @throws ArrayIndexOutOfBoundsException6434* if {@code aFromIndex < 0 or aToIndex > a.length} or6435* if {@code bFromIndex < 0 or bToIndex > b.length}6436* @throws NullPointerException6437* if either array is {@code null}6438* @since 96439*/6440public static int compare(int[] a, int aFromIndex, int aToIndex,6441int[] b, int bFromIndex, int bToIndex) {6442rangeCheck(a.length, aFromIndex, aToIndex);6443rangeCheck(b.length, bFromIndex, bToIndex);64446445int aLength = aToIndex - aFromIndex;6446int bLength = bToIndex - bFromIndex;6447int i = ArraysSupport.mismatch(a, aFromIndex,6448b, bFromIndex,6449Math.min(aLength, bLength));6450if (i >= 0) {6451return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);6452}64536454return aLength - bLength;6455}64566457/**6458* Compares two {@code int} arrays lexicographically, numerically treating6459* elements as unsigned.6460*6461* <p>If the two arrays share a common prefix then the lexicographic6462* comparison is the result of comparing two elements, as if by6463* {@link Integer#compareUnsigned(int, int)}, at an index within the6464* respective arrays that is the prefix length.6465* Otherwise, one array is a proper prefix of the other and, lexicographic6466* comparison is the result of comparing the two array lengths.6467* (See {@link #mismatch(int[], int[])} for the definition of a common6468* and proper prefix.)6469*6470* <p>A {@code null} array reference is considered lexicographically less6471* than a non-{@code null} array reference. Two {@code null} array6472* references are considered equal.6473*6474* @apiNote6475* <p>This method behaves as if (for non-{@code null} array references):6476* <pre>{@code6477* int i = Arrays.mismatch(a, b);6478* if (i >= 0 && i < Math.min(a.length, b.length))6479* return Integer.compareUnsigned(a[i], b[i]);6480* return a.length - b.length;6481* }</pre>6482*6483* @param a the first array to compare6484* @param b the second array to compare6485* @return the value {@code 0} if the first and second array are6486* equal and contain the same elements in the same order;6487* a value less than {@code 0} if the first array is6488* lexicographically less than the second array; and6489* a value greater than {@code 0} if the first array is6490* lexicographically greater than the second array6491* @since 96492*/6493public static int compareUnsigned(int[] a, int[] b) {6494if (a == b)6495return 0;6496if (a == null || b == null)6497return a == null ? -1 : 1;64986499int i = ArraysSupport.mismatch(a, b,6500Math.min(a.length, b.length));6501if (i >= 0) {6502return Integer.compareUnsigned(a[i], b[i]);6503}65046505return a.length - b.length;6506}65076508/**6509* Compares two {@code int} arrays lexicographically over the specified6510* ranges, numerically treating elements as unsigned.6511*6512* <p>If the two arrays, over the specified ranges, share a common prefix6513* then the lexicographic comparison is the result of comparing two6514* elements, as if by {@link Integer#compareUnsigned(int, int)}, at a6515* relative index within the respective arrays that is the length of the6516* prefix.6517* Otherwise, one array is a proper prefix of the other and, lexicographic6518* comparison is the result of comparing the two range lengths.6519* (See {@link #mismatch(int[], int, int, int[], int, int)} for the6520* definition of a common and proper prefix.)6521*6522* @apiNote6523* <p>This method behaves as if:6524* <pre>{@code6525* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6526* b, bFromIndex, bToIndex);6527* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6528* return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6529* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6530* }</pre>6531*6532* @param a the first array to compare6533* @param aFromIndex the index (inclusive) of the first element in the6534* first array to be compared6535* @param aToIndex the index (exclusive) of the last element in the6536* first array to be compared6537* @param b the second array to compare6538* @param bFromIndex the index (inclusive) of the first element in the6539* second array to be compared6540* @param bToIndex the index (exclusive) of the last element in the6541* second array to be compared6542* @return the value {@code 0} if, over the specified ranges, the first and6543* second array are equal and contain the same elements in the same6544* order;6545* a value less than {@code 0} if, over the specified ranges, the6546* first array is lexicographically less than the second array; and6547* a value greater than {@code 0} if, over the specified ranges, the6548* first array is lexicographically greater than the second array6549* @throws IllegalArgumentException6550* if {@code aFromIndex > aToIndex} or6551* if {@code bFromIndex > bToIndex}6552* @throws ArrayIndexOutOfBoundsException6553* if {@code aFromIndex < 0 or aToIndex > a.length} or6554* if {@code bFromIndex < 0 or bToIndex > b.length}6555* @throws NullPointerException6556* if either array is null6557* @since 96558*/6559public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,6560int[] b, int bFromIndex, int bToIndex) {6561rangeCheck(a.length, aFromIndex, aToIndex);6562rangeCheck(b.length, bFromIndex, bToIndex);65636564int aLength = aToIndex - aFromIndex;6565int bLength = bToIndex - bFromIndex;6566int i = ArraysSupport.mismatch(a, aFromIndex,6567b, bFromIndex,6568Math.min(aLength, bLength));6569if (i >= 0) {6570return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6571}65726573return aLength - bLength;6574}65756576// Compare long65776578/**6579* Compares two {@code long} arrays lexicographically.6580*6581* <p>If the two arrays share a common prefix then the lexicographic6582* comparison is the result of comparing two elements, as if by6583* {@link Long#compare(long, long)}, at an index within the respective6584* arrays that is the prefix length.6585* Otherwise, one array is a proper prefix of the other and, lexicographic6586* comparison is the result of comparing the two array lengths.6587* (See {@link #mismatch(long[], long[])} for the definition of a common and6588* proper prefix.)6589*6590* <p>A {@code null} array reference is considered lexicographically less6591* than a non-{@code null} array reference. Two {@code null} array6592* references are considered equal.6593*6594* <p>The comparison is consistent with {@link #equals(long[], long[]) equals},6595* more specifically the following holds for arrays {@code a} and {@code b}:6596* <pre>{@code6597* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)6598* }</pre>6599*6600* @apiNote6601* <p>This method behaves as if (for non-{@code null} array references):6602* <pre>{@code6603* int i = Arrays.mismatch(a, b);6604* if (i >= 0 && i < Math.min(a.length, b.length))6605* return Long.compare(a[i], b[i]);6606* return a.length - b.length;6607* }</pre>6608*6609* @param a the first array to compare6610* @param b the second array to compare6611* @return the value {@code 0} if the first and second array are equal and6612* contain the same elements in the same order;6613* a value less than {@code 0} if the first array is6614* lexicographically less than the second array; and6615* a value greater than {@code 0} if the first array is6616* lexicographically greater than the second array6617* @since 96618*/6619public static int compare(long[] a, long[] b) {6620if (a == b)6621return 0;6622if (a == null || b == null)6623return a == null ? -1 : 1;66246625int i = ArraysSupport.mismatch(a, b,6626Math.min(a.length, b.length));6627if (i >= 0) {6628return Long.compare(a[i], b[i]);6629}66306631return a.length - b.length;6632}66336634/**6635* Compares two {@code long} arrays lexicographically over the specified6636* ranges.6637*6638* <p>If the two arrays, over the specified ranges, share a common prefix6639* then the lexicographic comparison is the result of comparing two6640* elements, as if by {@link Long#compare(long, long)}, at a relative index6641* within the respective arrays that is the length of the prefix.6642* Otherwise, one array is a proper prefix of the other and, lexicographic6643* comparison is the result of comparing the two range lengths.6644* (See {@link #mismatch(long[], int, int, long[], int, int)} for the6645* definition of a common and proper prefix.)6646*6647* <p>The comparison is consistent with6648* {@link #equals(long[], int, int, long[], int, int) equals}, more6649* specifically the following holds for arrays {@code a} and {@code b} with6650* specified ranges [{@code aFromIndex}, {@code atoIndex}) and6651* [{@code bFromIndex}, {@code btoIndex}) respectively:6652* <pre>{@code6653* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==6654* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)6655* }</pre>6656*6657* @apiNote6658* <p>This method behaves as if:6659* <pre>{@code6660* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6661* b, bFromIndex, bToIndex);6662* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6663* return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);6664* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6665* }</pre>6666*6667* @param a the first array to compare6668* @param aFromIndex the index (inclusive) of the first element in the6669* first array to be compared6670* @param aToIndex the index (exclusive) of the last element in the6671* first array to be compared6672* @param b the second array to compare6673* @param bFromIndex the index (inclusive) of the first element in the6674* second array to be compared6675* @param bToIndex the index (exclusive) of the last element in the6676* second array to be compared6677* @return the value {@code 0} if, over the specified ranges, the first and6678* second array are equal and contain the same elements in the same6679* order;6680* a value less than {@code 0} if, over the specified ranges, the6681* first array is lexicographically less than the second array; and6682* a value greater than {@code 0} if, over the specified ranges, the6683* first array is lexicographically greater than the second array6684* @throws IllegalArgumentException6685* if {@code aFromIndex > aToIndex} or6686* if {@code bFromIndex > bToIndex}6687* @throws ArrayIndexOutOfBoundsException6688* if {@code aFromIndex < 0 or aToIndex > a.length} or6689* if {@code bFromIndex < 0 or bToIndex > b.length}6690* @throws NullPointerException6691* if either array is {@code null}6692* @since 96693*/6694public static int compare(long[] a, int aFromIndex, int aToIndex,6695long[] b, int bFromIndex, int bToIndex) {6696rangeCheck(a.length, aFromIndex, aToIndex);6697rangeCheck(b.length, bFromIndex, bToIndex);66986699int aLength = aToIndex - aFromIndex;6700int bLength = bToIndex - bFromIndex;6701int i = ArraysSupport.mismatch(a, aFromIndex,6702b, bFromIndex,6703Math.min(aLength, bLength));6704if (i >= 0) {6705return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);6706}67076708return aLength - bLength;6709}67106711/**6712* Compares two {@code long} arrays lexicographically, numerically treating6713* elements as unsigned.6714*6715* <p>If the two arrays share a common prefix then the lexicographic6716* comparison is the result of comparing two elements, as if by6717* {@link Long#compareUnsigned(long, long)}, at an index within the6718* respective arrays that is the prefix length.6719* Otherwise, one array is a proper prefix of the other and, lexicographic6720* comparison is the result of comparing the two array lengths.6721* (See {@link #mismatch(long[], long[])} for the definition of a common6722* and proper prefix.)6723*6724* <p>A {@code null} array reference is considered lexicographically less6725* than a non-{@code null} array reference. Two {@code null} array6726* references are considered equal.6727*6728* @apiNote6729* <p>This method behaves as if (for non-{@code null} array references):6730* <pre>{@code6731* int i = Arrays.mismatch(a, b);6732* if (i >= 0 && i < Math.min(a.length, b.length))6733* return Long.compareUnsigned(a[i], b[i]);6734* return a.length - b.length;6735* }</pre>6736*6737* @param a the first array to compare6738* @param b the second array to compare6739* @return the value {@code 0} if the first and second array are6740* equal and contain the same elements in the same order;6741* a value less than {@code 0} if the first array is6742* lexicographically less than the second array; and6743* a value greater than {@code 0} if the first array is6744* lexicographically greater than the second array6745* @since 96746*/6747public static int compareUnsigned(long[] a, long[] b) {6748if (a == b)6749return 0;6750if (a == null || b == null)6751return a == null ? -1 : 1;67526753int i = ArraysSupport.mismatch(a, b,6754Math.min(a.length, b.length));6755if (i >= 0) {6756return Long.compareUnsigned(a[i], b[i]);6757}67586759return a.length - b.length;6760}67616762/**6763* Compares two {@code long} arrays lexicographically over the specified6764* ranges, numerically treating elements as unsigned.6765*6766* <p>If the two arrays, over the specified ranges, share a common prefix6767* then the lexicographic comparison is the result of comparing two6768* elements, as if by {@link Long#compareUnsigned(long, long)}, at a6769* relative index within the respective arrays that is the length of the6770* prefix.6771* Otherwise, one array is a proper prefix of the other and, lexicographic6772* comparison is the result of comparing the two range lengths.6773* (See {@link #mismatch(long[], int, int, long[], int, int)} for the6774* definition of a common and proper prefix.)6775*6776* @apiNote6777* <p>This method behaves as if:6778* <pre>{@code6779* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6780* b, bFromIndex, bToIndex);6781* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6782* return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6783* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6784* }</pre>6785*6786* @param a the first array to compare6787* @param aFromIndex the index (inclusive) of the first element in the6788* first array to be compared6789* @param aToIndex the index (exclusive) of the last element in the6790* first array to be compared6791* @param b the second array to compare6792* @param bFromIndex the index (inclusive) of the first element in the6793* second array to be compared6794* @param bToIndex the index (exclusive) of the last element in the6795* second array to be compared6796* @return the value {@code 0} if, over the specified ranges, the first and6797* second array are equal and contain the same elements in the same6798* order;6799* a value less than {@code 0} if, over the specified ranges, the6800* first array is lexicographically less than the second array; and6801* a value greater than {@code 0} if, over the specified ranges, the6802* first array is lexicographically greater than the second array6803* @throws IllegalArgumentException6804* if {@code aFromIndex > aToIndex} or6805* if {@code bFromIndex > bToIndex}6806* @throws ArrayIndexOutOfBoundsException6807* if {@code aFromIndex < 0 or aToIndex > a.length} or6808* if {@code bFromIndex < 0 or bToIndex > b.length}6809* @throws NullPointerException6810* if either array is null6811* @since 96812*/6813public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,6814long[] b, int bFromIndex, int bToIndex) {6815rangeCheck(a.length, aFromIndex, aToIndex);6816rangeCheck(b.length, bFromIndex, bToIndex);68176818int aLength = aToIndex - aFromIndex;6819int bLength = bToIndex - bFromIndex;6820int i = ArraysSupport.mismatch(a, aFromIndex,6821b, bFromIndex,6822Math.min(aLength, bLength));6823if (i >= 0) {6824return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);6825}68266827return aLength - bLength;6828}68296830// Compare float68316832/**6833* Compares two {@code float} arrays lexicographically.6834*6835* <p>If the two arrays share a common prefix then the lexicographic6836* comparison is the result of comparing two elements, as if by6837* {@link Float#compare(float, float)}, at an index within the respective6838* arrays that is the prefix length.6839* Otherwise, one array is a proper prefix of the other and, lexicographic6840* comparison is the result of comparing the two array lengths.6841* (See {@link #mismatch(float[], float[])} for the definition of a common6842* and proper prefix.)6843*6844* <p>A {@code null} array reference is considered lexicographically less6845* than a non-{@code null} array reference. Two {@code null} array6846* references are considered equal.6847*6848* <p>The comparison is consistent with {@link #equals(float[], float[]) equals},6849* more specifically the following holds for arrays {@code a} and {@code b}:6850* <pre>{@code6851* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)6852* }</pre>6853*6854* @apiNote6855* <p>This method behaves as if (for non-{@code null} array references):6856* <pre>{@code6857* int i = Arrays.mismatch(a, b);6858* if (i >= 0 && i < Math.min(a.length, b.length))6859* return Float.compare(a[i], b[i]);6860* return a.length - b.length;6861* }</pre>6862*6863* @param a the first array to compare6864* @param b the second array to compare6865* @return the value {@code 0} if the first and second array are equal and6866* contain the same elements in the same order;6867* a value less than {@code 0} if the first array is6868* lexicographically less than the second array; and6869* a value greater than {@code 0} if the first array is6870* lexicographically greater than the second array6871* @since 96872*/6873public static int compare(float[] a, float[] b) {6874if (a == b)6875return 0;6876if (a == null || b == null)6877return a == null ? -1 : 1;68786879int i = ArraysSupport.mismatch(a, b,6880Math.min(a.length, b.length));6881if (i >= 0) {6882return Float.compare(a[i], b[i]);6883}68846885return a.length - b.length;6886}68876888/**6889* Compares two {@code float} arrays lexicographically over the specified6890* ranges.6891*6892* <p>If the two arrays, over the specified ranges, share a common prefix6893* then the lexicographic comparison is the result of comparing two6894* elements, as if by {@link Float#compare(float, float)}, at a relative6895* index within the respective arrays that is the length of the prefix.6896* Otherwise, one array is a proper prefix of the other and, lexicographic6897* comparison is the result of comparing the two range lengths.6898* (See {@link #mismatch(float[], int, int, float[], int, int)} for the6899* definition of a common and proper prefix.)6900*6901* <p>The comparison is consistent with6902* {@link #equals(float[], int, int, float[], int, int) equals}, more6903* specifically the following holds for arrays {@code a} and {@code b} with6904* specified ranges [{@code aFromIndex}, {@code atoIndex}) and6905* [{@code bFromIndex}, {@code btoIndex}) respectively:6906* <pre>{@code6907* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==6908* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)6909* }</pre>6910*6911* @apiNote6912* <p>This method behaves as if:6913* <pre>{@code6914* int i = Arrays.mismatch(a, aFromIndex, aToIndex,6915* b, bFromIndex, bToIndex);6916* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))6917* return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);6918* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);6919* }</pre>6920*6921* @param a the first array to compare6922* @param aFromIndex the index (inclusive) of the first element in the6923* first array to be compared6924* @param aToIndex the index (exclusive) of the last element in the6925* first array to be compared6926* @param b the second array to compare6927* @param bFromIndex the index (inclusive) of the first element in the6928* second array to be compared6929* @param bToIndex the index (exclusive) of the last element in the6930* second array to be compared6931* @return the value {@code 0} if, over the specified ranges, the first and6932* second array are equal and contain the same elements in the same6933* order;6934* a value less than {@code 0} if, over the specified ranges, the6935* first array is lexicographically less than the second array; and6936* a value greater than {@code 0} if, over the specified ranges, the6937* first array is lexicographically greater than the second array6938* @throws IllegalArgumentException6939* if {@code aFromIndex > aToIndex} or6940* if {@code bFromIndex > bToIndex}6941* @throws ArrayIndexOutOfBoundsException6942* if {@code aFromIndex < 0 or aToIndex > a.length} or6943* if {@code bFromIndex < 0 or bToIndex > b.length}6944* @throws NullPointerException6945* if either array is {@code null}6946* @since 96947*/6948public static int compare(float[] a, int aFromIndex, int aToIndex,6949float[] b, int bFromIndex, int bToIndex) {6950rangeCheck(a.length, aFromIndex, aToIndex);6951rangeCheck(b.length, bFromIndex, bToIndex);69526953int aLength = aToIndex - aFromIndex;6954int bLength = bToIndex - bFromIndex;6955int i = ArraysSupport.mismatch(a, aFromIndex,6956b, bFromIndex,6957Math.min(aLength, bLength));6958if (i >= 0) {6959return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);6960}69616962return aLength - bLength;6963}69646965// Compare double69666967/**6968* Compares two {@code double} arrays lexicographically.6969*6970* <p>If the two arrays share a common prefix then the lexicographic6971* comparison is the result of comparing two elements, as if by6972* {@link Double#compare(double, double)}, at an index within the respective6973* arrays that is the prefix length.6974* Otherwise, one array is a proper prefix of the other and, lexicographic6975* comparison is the result of comparing the two array lengths.6976* (See {@link #mismatch(double[], double[])} for the definition of a common6977* and proper prefix.)6978*6979* <p>A {@code null} array reference is considered lexicographically less6980* than a non-{@code null} array reference. Two {@code null} array6981* references are considered equal.6982*6983* <p>The comparison is consistent with {@link #equals(double[], double[]) equals},6984* more specifically the following holds for arrays {@code a} and {@code b}:6985* <pre>{@code6986* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)6987* }</pre>6988*6989* @apiNote6990* <p>This method behaves as if (for non-{@code null} array references):6991* <pre>{@code6992* int i = Arrays.mismatch(a, b);6993* if (i >= 0 && i < Math.min(a.length, b.length))6994* return Double.compare(a[i], b[i]);6995* return a.length - b.length;6996* }</pre>6997*6998* @param a the first array to compare6999* @param b the second array to compare7000* @return the value {@code 0} if the first and second array are equal and7001* contain the same elements in the same order;7002* a value less than {@code 0} if the first array is7003* lexicographically less than the second array; and7004* a value greater than {@code 0} if the first array is7005* lexicographically greater than the second array7006* @since 97007*/7008public static int compare(double[] a, double[] b) {7009if (a == b)7010return 0;7011if (a == null || b == null)7012return a == null ? -1 : 1;70137014int i = ArraysSupport.mismatch(a, b,7015Math.min(a.length, b.length));7016if (i >= 0) {7017return Double.compare(a[i], b[i]);7018}70197020return a.length - b.length;7021}70227023/**7024* Compares two {@code double} arrays lexicographically over the specified7025* ranges.7026*7027* <p>If the two arrays, over the specified ranges, share a common prefix7028* then the lexicographic comparison is the result of comparing two7029* elements, as if by {@link Double#compare(double, double)}, at a relative7030* index within the respective arrays that is the length of the prefix.7031* Otherwise, one array is a proper prefix of the other and, lexicographic7032* comparison is the result of comparing the two range lengths.7033* (See {@link #mismatch(double[], int, int, double[], int, int)} for the7034* definition of a common and proper prefix.)7035*7036* <p>The comparison is consistent with7037* {@link #equals(double[], int, int, double[], int, int) equals}, more7038* specifically the following holds for arrays {@code a} and {@code b} with7039* specified ranges [{@code aFromIndex}, {@code atoIndex}) and7040* [{@code bFromIndex}, {@code btoIndex}) respectively:7041* <pre>{@code7042* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==7043* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)7044* }</pre>7045*7046* @apiNote7047* <p>This method behaves as if:7048* <pre>{@code7049* int i = Arrays.mismatch(a, aFromIndex, aToIndex,7050* b, bFromIndex, bToIndex);7051* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7052* return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);7053* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);7054* }</pre>7055*7056* @param a the first array to compare7057* @param aFromIndex the index (inclusive) of the first element in the7058* first array to be compared7059* @param aToIndex the index (exclusive) of the last element in the7060* first array to be compared7061* @param b the second array to compare7062* @param bFromIndex the index (inclusive) of the first element in the7063* second array to be compared7064* @param bToIndex the index (exclusive) of the last element in the7065* second array to be compared7066* @return the value {@code 0} if, over the specified ranges, the first and7067* second array are equal and contain the same elements in the same7068* order;7069* a value less than {@code 0} if, over the specified ranges, the7070* first array is lexicographically less than the second array; and7071* a value greater than {@code 0} if, over the specified ranges, the7072* first array is lexicographically greater than the second array7073* @throws IllegalArgumentException7074* if {@code aFromIndex > aToIndex} or7075* if {@code bFromIndex > bToIndex}7076* @throws ArrayIndexOutOfBoundsException7077* if {@code aFromIndex < 0 or aToIndex > a.length} or7078* if {@code bFromIndex < 0 or bToIndex > b.length}7079* @throws NullPointerException7080* if either array is {@code null}7081* @since 97082*/7083public static int compare(double[] a, int aFromIndex, int aToIndex,7084double[] b, int bFromIndex, int bToIndex) {7085rangeCheck(a.length, aFromIndex, aToIndex);7086rangeCheck(b.length, bFromIndex, bToIndex);70877088int aLength = aToIndex - aFromIndex;7089int bLength = bToIndex - bFromIndex;7090int i = ArraysSupport.mismatch(a, aFromIndex,7091b, bFromIndex,7092Math.min(aLength, bLength));7093if (i >= 0) {7094return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);7095}70967097return aLength - bLength;7098}70997100// Compare objects71017102/**7103* Compares two {@code Object} arrays, within comparable elements,7104* lexicographically.7105*7106* <p>If the two arrays share a common prefix then the lexicographic7107* comparison is the result of comparing two elements of type {@code T} at7108* an index {@code i} within the respective arrays that is the prefix7109* length, as if by:7110* <pre>{@code7111* Comparator.nullsFirst(Comparator.<T>naturalOrder()).7112* compare(a[i], b[i])7113* }</pre>7114* Otherwise, one array is a proper prefix of the other and, lexicographic7115* comparison is the result of comparing the two array lengths.7116* (See {@link #mismatch(Object[], Object[])} for the definition of a common7117* and proper prefix.)7118*7119* <p>A {@code null} array reference is considered lexicographically less7120* than a non-{@code null} array reference. Two {@code null} array7121* references are considered equal.7122* A {@code null} array element is considered lexicographically less than a7123* non-{@code null} array element. Two {@code null} array elements are7124* considered equal.7125*7126* <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},7127* more specifically the following holds for arrays {@code a} and {@code b}:7128* <pre>{@code7129* Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)7130* }</pre>7131*7132* @apiNote7133* <p>This method behaves as if (for non-{@code null} array references7134* and elements):7135* <pre>{@code7136* int i = Arrays.mismatch(a, b);7137* if (i >= 0 && i < Math.min(a.length, b.length))7138* return a[i].compareTo(b[i]);7139* return a.length - b.length;7140* }</pre>7141*7142* @param a the first array to compare7143* @param b the second array to compare7144* @param <T> the type of comparable array elements7145* @return the value {@code 0} if the first and second array are equal and7146* contain the same elements in the same order;7147* a value less than {@code 0} if the first array is7148* lexicographically less than the second array; and7149* a value greater than {@code 0} if the first array is7150* lexicographically greater than the second array7151* @since 97152*/7153public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {7154if (a == b)7155return 0;7156// A null array is less than a non-null array7157if (a == null || b == null)7158return a == null ? -1 : 1;71597160int length = Math.min(a.length, b.length);7161for (int i = 0; i < length; i++) {7162T oa = a[i];7163T ob = b[i];7164if (oa != ob) {7165// A null element is less than a non-null element7166if (oa == null || ob == null)7167return oa == null ? -1 : 1;7168int v = oa.compareTo(ob);7169if (v != 0) {7170return v;7171}7172}7173}71747175return a.length - b.length;7176}71777178/**7179* Compares two {@code Object} arrays lexicographically over the specified7180* ranges.7181*7182* <p>If the two arrays, over the specified ranges, share a common prefix7183* then the lexicographic comparison is the result of comparing two7184* elements of type {@code T} at a relative index {@code i} within the7185* respective arrays that is the prefix length, as if by:7186* <pre>{@code7187* Comparator.nullsFirst(Comparator.<T>naturalOrder()).7188* compare(a[aFromIndex + i, b[bFromIndex + i])7189* }</pre>7190* Otherwise, one array is a proper prefix of the other and, lexicographic7191* comparison is the result of comparing the two range lengths.7192* (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the7193* definition of a common and proper prefix.)7194*7195* <p>The comparison is consistent with7196* {@link #equals(Object[], int, int, Object[], int, int) equals}, more7197* specifically the following holds for arrays {@code a} and {@code b} with7198* specified ranges [{@code aFromIndex}, {@code atoIndex}) and7199* [{@code bFromIndex}, {@code btoIndex}) respectively:7200* <pre>{@code7201* Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==7202* (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)7203* }</pre>7204*7205* @apiNote7206* <p>This method behaves as if (for non-{@code null} array elements):7207* <pre>{@code7208* int i = Arrays.mismatch(a, aFromIndex, aToIndex,7209* b, bFromIndex, bToIndex);7210* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7211* return a[aFromIndex + i].compareTo(b[bFromIndex + i]);7212* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);7213* }</pre>7214*7215* @param a the first array to compare7216* @param aFromIndex the index (inclusive) of the first element in the7217* first array to be compared7218* @param aToIndex the index (exclusive) of the last element in the7219* first array to be compared7220* @param b the second array to compare7221* @param bFromIndex the index (inclusive) of the first element in the7222* second array to be compared7223* @param bToIndex the index (exclusive) of the last element in the7224* second array to be compared7225* @param <T> the type of comparable array elements7226* @return the value {@code 0} if, over the specified ranges, the first and7227* second array are equal and contain the same elements in the same7228* order;7229* a value less than {@code 0} if, over the specified ranges, the7230* first array is lexicographically less than the second array; and7231* a value greater than {@code 0} if, over the specified ranges, the7232* first array is lexicographically greater than the second array7233* @throws IllegalArgumentException7234* if {@code aFromIndex > aToIndex} or7235* if {@code bFromIndex > bToIndex}7236* @throws ArrayIndexOutOfBoundsException7237* if {@code aFromIndex < 0 or aToIndex > a.length} or7238* if {@code bFromIndex < 0 or bToIndex > b.length}7239* @throws NullPointerException7240* if either array is {@code null}7241* @since 97242*/7243public static <T extends Comparable<? super T>> int compare(7244T[] a, int aFromIndex, int aToIndex,7245T[] b, int bFromIndex, int bToIndex) {7246rangeCheck(a.length, aFromIndex, aToIndex);7247rangeCheck(b.length, bFromIndex, bToIndex);72487249int aLength = aToIndex - aFromIndex;7250int bLength = bToIndex - bFromIndex;7251int length = Math.min(aLength, bLength);7252for (int i = 0; i < length; i++) {7253T oa = a[aFromIndex++];7254T ob = b[bFromIndex++];7255if (oa != ob) {7256if (oa == null || ob == null)7257return oa == null ? -1 : 1;7258int v = oa.compareTo(ob);7259if (v != 0) {7260return v;7261}7262}7263}72647265return aLength - bLength;7266}72677268/**7269* Compares two {@code Object} arrays lexicographically using a specified7270* comparator.7271*7272* <p>If the two arrays share a common prefix then the lexicographic7273* comparison is the result of comparing with the specified comparator two7274* elements at an index within the respective arrays that is the prefix7275* length.7276* Otherwise, one array is a proper prefix of the other and, lexicographic7277* comparison is the result of comparing the two array lengths.7278* (See {@link #mismatch(Object[], Object[])} for the definition of a common7279* and proper prefix.)7280*7281* <p>A {@code null} array reference is considered lexicographically less7282* than a non-{@code null} array reference. Two {@code null} array7283* references are considered equal.7284*7285* @apiNote7286* <p>This method behaves as if (for non-{@code null} array references):7287* <pre>{@code7288* int i = Arrays.mismatch(a, b, cmp);7289* if (i >= 0 && i < Math.min(a.length, b.length))7290* return cmp.compare(a[i], b[i]);7291* return a.length - b.length;7292* }</pre>7293*7294* @param a the first array to compare7295* @param b the second array to compare7296* @param cmp the comparator to compare array elements7297* @param <T> the type of array elements7298* @return the value {@code 0} if the first and second array are equal and7299* contain the same elements in the same order;7300* a value less than {@code 0} if the first array is7301* lexicographically less than the second array; and7302* a value greater than {@code 0} if the first array is7303* lexicographically greater than the second array7304* @throws NullPointerException if the comparator is {@code null}7305* @since 97306*/7307public static <T> int compare(T[] a, T[] b,7308Comparator<? super T> cmp) {7309Objects.requireNonNull(cmp);7310if (a == b)7311return 0;7312if (a == null || b == null)7313return a == null ? -1 : 1;73147315int length = Math.min(a.length, b.length);7316for (int i = 0; i < length; i++) {7317T oa = a[i];7318T ob = b[i];7319if (oa != ob) {7320// Null-value comparison is deferred to the comparator7321int v = cmp.compare(oa, ob);7322if (v != 0) {7323return v;7324}7325}7326}73277328return a.length - b.length;7329}73307331/**7332* Compares two {@code Object} arrays lexicographically over the specified7333* ranges.7334*7335* <p>If the two arrays, over the specified ranges, share a common prefix7336* then the lexicographic comparison is the result of comparing with the7337* specified comparator two elements at a relative index within the7338* respective arrays that is the prefix length.7339* Otherwise, one array is a proper prefix of the other and, lexicographic7340* comparison is the result of comparing the two range lengths.7341* (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the7342* definition of a common and proper prefix.)7343*7344* @apiNote7345* <p>This method behaves as if (for non-{@code null} array elements):7346* <pre>{@code7347* int i = Arrays.mismatch(a, aFromIndex, aToIndex,7348* b, bFromIndex, bToIndex, cmp);7349* if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7350* return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);7351* return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);7352* }</pre>7353*7354* @param a the first array to compare7355* @param aFromIndex the index (inclusive) of the first element in the7356* first array to be compared7357* @param aToIndex the index (exclusive) of the last element in the7358* first array to be compared7359* @param b the second array to compare7360* @param bFromIndex the index (inclusive) of the first element in the7361* second array to be compared7362* @param bToIndex the index (exclusive) of the last element in the7363* second array to be compared7364* @param cmp the comparator to compare array elements7365* @param <T> the type of array elements7366* @return the value {@code 0} if, over the specified ranges, the first and7367* second array are equal and contain the same elements in the same7368* order;7369* a value less than {@code 0} if, over the specified ranges, the7370* first array is lexicographically less than the second array; and7371* a value greater than {@code 0} if, over the specified ranges, the7372* first array is lexicographically greater than the second array7373* @throws IllegalArgumentException7374* if {@code aFromIndex > aToIndex} or7375* if {@code bFromIndex > bToIndex}7376* @throws ArrayIndexOutOfBoundsException7377* if {@code aFromIndex < 0 or aToIndex > a.length} or7378* if {@code bFromIndex < 0 or bToIndex > b.length}7379* @throws NullPointerException7380* if either array or the comparator is {@code null}7381* @since 97382*/7383public static <T> int compare(7384T[] a, int aFromIndex, int aToIndex,7385T[] b, int bFromIndex, int bToIndex,7386Comparator<? super T> cmp) {7387Objects.requireNonNull(cmp);7388rangeCheck(a.length, aFromIndex, aToIndex);7389rangeCheck(b.length, bFromIndex, bToIndex);73907391int aLength = aToIndex - aFromIndex;7392int bLength = bToIndex - bFromIndex;7393int length = Math.min(aLength, bLength);7394for (int i = 0; i < length; i++) {7395T oa = a[aFromIndex++];7396T ob = b[bFromIndex++];7397if (oa != ob) {7398// Null-value comparison is deferred to the comparator7399int v = cmp.compare(oa, ob);7400if (v != 0) {7401return v;7402}7403}7404}74057406return aLength - bLength;7407}740874097410// Mismatch methods74117412// Mismatch boolean74137414/**7415* Finds and returns the index of the first mismatch between two7416* {@code boolean} arrays, otherwise return -1 if no mismatch is found. The7417* index will be in the range of 0 (inclusive) up to the length (inclusive)7418* of the smaller array.7419*7420* <p>If the two arrays share a common prefix then the returned index is the7421* length of the common prefix and it follows that there is a mismatch7422* between the two elements at that index within the respective arrays.7423* If one array is a proper prefix of the other then the returned index is7424* the length of the smaller array and it follows that the index is only7425* valid for the larger array.7426* Otherwise, there is no mismatch.7427*7428* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common7429* prefix of length {@code pl} if the following expression is true:7430* <pre>{@code7431* pl >= 0 &&7432* pl < Math.min(a.length, b.length) &&7433* Arrays.equals(a, 0, pl, b, 0, pl) &&7434* a[pl] != b[pl]7435* }</pre>7436* Note that a common prefix length of {@code 0} indicates that the first7437* elements from each array mismatch.7438*7439* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper7440* prefix if the following expression is true:7441* <pre>{@code7442* a.length != b.length &&7443* Arrays.equals(a, 0, Math.min(a.length, b.length),7444* b, 0, Math.min(a.length, b.length))7445* }</pre>7446*7447* @param a the first array to be tested for a mismatch7448* @param b the second array to be tested for a mismatch7449* @return the index of the first mismatch between the two arrays,7450* otherwise {@code -1}.7451* @throws NullPointerException7452* if either array is {@code null}7453* @since 97454*/7455public static int mismatch(boolean[] a, boolean[] b) {7456int length = Math.min(a.length, b.length); // Check null array refs7457if (a == b)7458return -1;74597460int i = ArraysSupport.mismatch(a, b, length);7461return (i < 0 && a.length != b.length) ? length : i;7462}74637464/**7465* Finds and returns the relative index of the first mismatch between two7466* {@code boolean} arrays over the specified ranges, otherwise return -1 if7467* no mismatch is found. The index will be in the range of 0 (inclusive) up7468* to the length (inclusive) of the smaller range.7469*7470* <p>If the two arrays, over the specified ranges, share a common prefix7471* then the returned relative index is the length of the common prefix and7472* it follows that there is a mismatch between the two elements at that7473* relative index within the respective arrays.7474* If one array is a proper prefix of the other, over the specified ranges,7475* then the returned relative index is the length of the smaller range and7476* it follows that the relative index is only valid for the array with the7477* larger range.7478* Otherwise, there is no mismatch.7479*7480* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7481* ranges [{@code aFromIndex}, {@code atoIndex}) and7482* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common7483* prefix of length {@code pl} if the following expression is true:7484* <pre>{@code7485* pl >= 0 &&7486* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&7487* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&7488* a[aFromIndex + pl] != b[bFromIndex + pl]7489* }</pre>7490* Note that a common prefix length of {@code 0} indicates that the first7491* elements from each array mismatch.7492*7493* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7494* ranges [{@code aFromIndex}, {@code atoIndex}) and7495* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper7496* prefix if the following expression is true:7497* <pre>{@code7498* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&7499* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),7500* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7501* }</pre>7502*7503* @param a the first array to be tested for a mismatch7504* @param aFromIndex the index (inclusive) of the first element in the7505* first array to be tested7506* @param aToIndex the index (exclusive) of the last element in the7507* first array to be tested7508* @param b the second array to be tested for a mismatch7509* @param bFromIndex the index (inclusive) of the first element in the7510* second array to be tested7511* @param bToIndex the index (exclusive) of the last element in the7512* second array to be tested7513* @return the relative index of the first mismatch between the two arrays7514* over the specified ranges, otherwise {@code -1}.7515* @throws IllegalArgumentException7516* if {@code aFromIndex > aToIndex} or7517* if {@code bFromIndex > bToIndex}7518* @throws ArrayIndexOutOfBoundsException7519* if {@code aFromIndex < 0 or aToIndex > a.length} or7520* if {@code bFromIndex < 0 or bToIndex > b.length}7521* @throws NullPointerException7522* if either array is {@code null}7523* @since 97524*/7525public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,7526boolean[] b, int bFromIndex, int bToIndex) {7527rangeCheck(a.length, aFromIndex, aToIndex);7528rangeCheck(b.length, bFromIndex, bToIndex);75297530int aLength = aToIndex - aFromIndex;7531int bLength = bToIndex - bFromIndex;7532int length = Math.min(aLength, bLength);7533int i = ArraysSupport.mismatch(a, aFromIndex,7534b, bFromIndex,7535length);7536return (i < 0 && aLength != bLength) ? length : i;7537}75387539// Mismatch byte75407541/**7542* Finds and returns the index of the first mismatch between two {@code byte}7543* arrays, otherwise return -1 if no mismatch is found. The index will be7544* in the range of 0 (inclusive) up to the length (inclusive) of the smaller7545* array.7546*7547* <p>If the two arrays share a common prefix then the returned index is the7548* length of the common prefix and it follows that there is a mismatch7549* between the two elements at that index within the respective arrays.7550* If one array is a proper prefix of the other then the returned index is7551* the length of the smaller array and it follows that the index is only7552* valid for the larger array.7553* Otherwise, there is no mismatch.7554*7555* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common7556* prefix of length {@code pl} if the following expression is true:7557* <pre>{@code7558* pl >= 0 &&7559* pl < Math.min(a.length, b.length) &&7560* Arrays.equals(a, 0, pl, b, 0, pl) &&7561* a[pl] != b[pl]7562* }</pre>7563* Note that a common prefix length of {@code 0} indicates that the first7564* elements from each array mismatch.7565*7566* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper7567* prefix if the following expression is true:7568* <pre>{@code7569* a.length != b.length &&7570* Arrays.equals(a, 0, Math.min(a.length, b.length),7571* b, 0, Math.min(a.length, b.length))7572* }</pre>7573*7574* @param a the first array to be tested for a mismatch7575* @param b the second array to be tested for a mismatch7576* @return the index of the first mismatch between the two arrays,7577* otherwise {@code -1}.7578* @throws NullPointerException7579* if either array is {@code null}7580* @since 97581*/7582public static int mismatch(byte[] a, byte[] b) {7583int length = Math.min(a.length, b.length); // Check null array refs7584if (a == b)7585return -1;75867587int i = ArraysSupport.mismatch(a, b, length);7588return (i < 0 && a.length != b.length) ? length : i;7589}75907591/**7592* Finds and returns the relative index of the first mismatch between two7593* {@code byte} arrays over the specified ranges, otherwise return -1 if no7594* mismatch is found. The index will be in the range of 0 (inclusive) up to7595* the length (inclusive) of the smaller range.7596*7597* <p>If the two arrays, over the specified ranges, share a common prefix7598* then the returned relative index is the length of the common prefix and7599* it follows that there is a mismatch between the two elements at that7600* relative index within the respective arrays.7601* If one array is a proper prefix of the other, over the specified ranges,7602* then the returned relative index is the length of the smaller range and7603* it follows that the relative index is only valid for the array with the7604* larger range.7605* Otherwise, there is no mismatch.7606*7607* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7608* ranges [{@code aFromIndex}, {@code atoIndex}) and7609* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common7610* prefix of length {@code pl} if the following expression is true:7611* <pre>{@code7612* pl >= 0 &&7613* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&7614* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&7615* a[aFromIndex + pl] != b[bFromIndex + pl]7616* }</pre>7617* Note that a common prefix length of {@code 0} indicates that the first7618* elements from each array mismatch.7619*7620* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7621* ranges [{@code aFromIndex}, {@code atoIndex}) and7622* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper7623* prefix if the following expression is true:7624* <pre>{@code7625* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&7626* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),7627* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7628* }</pre>7629*7630* @param a the first array to be tested for a mismatch7631* @param aFromIndex the index (inclusive) of the first element in the7632* first array to be tested7633* @param aToIndex the index (exclusive) of the last element in the7634* first array to be tested7635* @param b the second array to be tested for a mismatch7636* @param bFromIndex the index (inclusive) of the first element in the7637* second array to be tested7638* @param bToIndex the index (exclusive) of the last element in the7639* second array to be tested7640* @return the relative index of the first mismatch between the two arrays7641* over the specified ranges, otherwise {@code -1}.7642* @throws IllegalArgumentException7643* if {@code aFromIndex > aToIndex} or7644* if {@code bFromIndex > bToIndex}7645* @throws ArrayIndexOutOfBoundsException7646* if {@code aFromIndex < 0 or aToIndex > a.length} or7647* if {@code bFromIndex < 0 or bToIndex > b.length}7648* @throws NullPointerException7649* if either array is {@code null}7650* @since 97651*/7652public static int mismatch(byte[] a, int aFromIndex, int aToIndex,7653byte[] b, int bFromIndex, int bToIndex) {7654rangeCheck(a.length, aFromIndex, aToIndex);7655rangeCheck(b.length, bFromIndex, bToIndex);76567657int aLength = aToIndex - aFromIndex;7658int bLength = bToIndex - bFromIndex;7659int length = Math.min(aLength, bLength);7660int i = ArraysSupport.mismatch(a, aFromIndex,7661b, bFromIndex,7662length);7663return (i < 0 && aLength != bLength) ? length : i;7664}76657666// Mismatch char76677668/**7669* Finds and returns the index of the first mismatch between two {@code char}7670* arrays, otherwise return -1 if no mismatch is found. The index will be7671* in the range of 0 (inclusive) up to the length (inclusive) of the smaller7672* array.7673*7674* <p>If the two arrays share a common prefix then the returned index is the7675* length of the common prefix and it follows that there is a mismatch7676* between the two elements at that index within the respective arrays.7677* If one array is a proper prefix of the other then the returned index is7678* the length of the smaller array and it follows that the index is only7679* valid for the larger array.7680* Otherwise, there is no mismatch.7681*7682* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common7683* prefix of length {@code pl} if the following expression is true:7684* <pre>{@code7685* pl >= 0 &&7686* pl < Math.min(a.length, b.length) &&7687* Arrays.equals(a, 0, pl, b, 0, pl) &&7688* a[pl] != b[pl]7689* }</pre>7690* Note that a common prefix length of {@code 0} indicates that the first7691* elements from each array mismatch.7692*7693* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper7694* prefix if the following expression is true:7695* <pre>{@code7696* a.length != b.length &&7697* Arrays.equals(a, 0, Math.min(a.length, b.length),7698* b, 0, Math.min(a.length, b.length))7699* }</pre>7700*7701* @param a the first array to be tested for a mismatch7702* @param b the second array to be tested for a mismatch7703* @return the index of the first mismatch between the two arrays,7704* otherwise {@code -1}.7705* @throws NullPointerException7706* if either array is {@code null}7707* @since 97708*/7709public static int mismatch(char[] a, char[] b) {7710int length = Math.min(a.length, b.length); // Check null array refs7711if (a == b)7712return -1;77137714int i = ArraysSupport.mismatch(a, b, length);7715return (i < 0 && a.length != b.length) ? length : i;7716}77177718/**7719* Finds and returns the relative index of the first mismatch between two7720* {@code char} arrays over the specified ranges, otherwise return -1 if no7721* mismatch is found. The index will be in the range of 0 (inclusive) up to7722* the length (inclusive) of the smaller range.7723*7724* <p>If the two arrays, over the specified ranges, share a common prefix7725* then the returned relative index is the length of the common prefix and7726* it follows that there is a mismatch between the two elements at that7727* relative index within the respective arrays.7728* If one array is a proper prefix of the other, over the specified ranges,7729* then the returned relative index is the length of the smaller range and7730* it follows that the relative index is only valid for the array with the7731* larger range.7732* Otherwise, there is no mismatch.7733*7734* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7735* ranges [{@code aFromIndex}, {@code atoIndex}) and7736* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common7737* prefix of length {@code pl} if the following expression is true:7738* <pre>{@code7739* pl >= 0 &&7740* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&7741* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&7742* a[aFromIndex + pl] != b[bFromIndex + pl]7743* }</pre>7744* Note that a common prefix length of {@code 0} indicates that the first7745* elements from each array mismatch.7746*7747* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7748* ranges [{@code aFromIndex}, {@code atoIndex}) and7749* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper7750* prefix if the following expression is true:7751* <pre>{@code7752* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&7753* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),7754* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7755* }</pre>7756*7757* @param a the first array to be tested for a mismatch7758* @param aFromIndex the index (inclusive) of the first element in the7759* first array to be tested7760* @param aToIndex the index (exclusive) of the last element in the7761* first array to be tested7762* @param b the second array to be tested for a mismatch7763* @param bFromIndex the index (inclusive) of the first element in the7764* second array to be tested7765* @param bToIndex the index (exclusive) of the last element in the7766* second array to be tested7767* @return the relative index of the first mismatch between the two arrays7768* over the specified ranges, otherwise {@code -1}.7769* @throws IllegalArgumentException7770* if {@code aFromIndex > aToIndex} or7771* if {@code bFromIndex > bToIndex}7772* @throws ArrayIndexOutOfBoundsException7773* if {@code aFromIndex < 0 or aToIndex > a.length} or7774* if {@code bFromIndex < 0 or bToIndex > b.length}7775* @throws NullPointerException7776* if either array is {@code null}7777* @since 97778*/7779public static int mismatch(char[] a, int aFromIndex, int aToIndex,7780char[] b, int bFromIndex, int bToIndex) {7781rangeCheck(a.length, aFromIndex, aToIndex);7782rangeCheck(b.length, bFromIndex, bToIndex);77837784int aLength = aToIndex - aFromIndex;7785int bLength = bToIndex - bFromIndex;7786int length = Math.min(aLength, bLength);7787int i = ArraysSupport.mismatch(a, aFromIndex,7788b, bFromIndex,7789length);7790return (i < 0 && aLength != bLength) ? length : i;7791}77927793// Mismatch short77947795/**7796* Finds and returns the index of the first mismatch between two {@code short}7797* arrays, otherwise return -1 if no mismatch is found. The index will be7798* in the range of 0 (inclusive) up to the length (inclusive) of the smaller7799* array.7800*7801* <p>If the two arrays share a common prefix then the returned index is the7802* length of the common prefix and it follows that there is a mismatch7803* between the two elements at that index within the respective arrays.7804* If one array is a proper prefix of the other then the returned index is7805* the length of the smaller array and it follows that the index is only7806* valid for the larger array.7807* Otherwise, there is no mismatch.7808*7809* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common7810* prefix of length {@code pl} if the following expression is true:7811* <pre>{@code7812* pl >= 0 &&7813* pl < Math.min(a.length, b.length) &&7814* Arrays.equals(a, 0, pl, b, 0, pl) &&7815* a[pl] != b[pl]7816* }</pre>7817* Note that a common prefix length of {@code 0} indicates that the first7818* elements from each array mismatch.7819*7820* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper7821* prefix if the following expression is true:7822* <pre>{@code7823* a.length != b.length &&7824* Arrays.equals(a, 0, Math.min(a.length, b.length),7825* b, 0, Math.min(a.length, b.length))7826* }</pre>7827*7828* @param a the first array to be tested for a mismatch7829* @param b the second array to be tested for a mismatch7830* @return the index of the first mismatch between the two arrays,7831* otherwise {@code -1}.7832* @throws NullPointerException7833* if either array is {@code null}7834* @since 97835*/7836public static int mismatch(short[] a, short[] b) {7837int length = Math.min(a.length, b.length); // Check null array refs7838if (a == b)7839return -1;78407841int i = ArraysSupport.mismatch(a, b, length);7842return (i < 0 && a.length != b.length) ? length : i;7843}78447845/**7846* Finds and returns the relative index of the first mismatch between two7847* {@code short} arrays over the specified ranges, otherwise return -1 if no7848* mismatch is found. The index will be in the range of 0 (inclusive) up to7849* the length (inclusive) of the smaller range.7850*7851* <p>If the two arrays, over the specified ranges, share a common prefix7852* then the returned relative index is the length of the common prefix and7853* it follows that there is a mismatch between the two elements at that7854* relative index within the respective arrays.7855* If one array is a proper prefix of the other, over the specified ranges,7856* then the returned relative index is the length of the smaller range and7857* it follows that the relative index is only valid for the array with the7858* larger range.7859* Otherwise, there is no mismatch.7860*7861* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7862* ranges [{@code aFromIndex}, {@code atoIndex}) and7863* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common7864* prefix of length {@code pl} if the following expression is true:7865* <pre>{@code7866* pl >= 0 &&7867* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&7868* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&7869* a[aFromIndex + pl] != b[bFromIndex + pl]7870* }</pre>7871* Note that a common prefix length of {@code 0} indicates that the first7872* elements from each array mismatch.7873*7874* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7875* ranges [{@code aFromIndex}, {@code atoIndex}) and7876* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper7877* prefix if the following expression is true:7878* <pre>{@code7879* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&7880* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),7881* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))7882* }</pre>7883*7884* @param a the first array to be tested for a mismatch7885* @param aFromIndex the index (inclusive) of the first element in the7886* first array to be tested7887* @param aToIndex the index (exclusive) of the last element in the7888* first array to be tested7889* @param b the second array to be tested for a mismatch7890* @param bFromIndex the index (inclusive) of the first element in the7891* second array to be tested7892* @param bToIndex the index (exclusive) of the last element in the7893* second array to be tested7894* @return the relative index of the first mismatch between the two arrays7895* over the specified ranges, otherwise {@code -1}.7896* @throws IllegalArgumentException7897* if {@code aFromIndex > aToIndex} or7898* if {@code bFromIndex > bToIndex}7899* @throws ArrayIndexOutOfBoundsException7900* if {@code aFromIndex < 0 or aToIndex > a.length} or7901* if {@code bFromIndex < 0 or bToIndex > b.length}7902* @throws NullPointerException7903* if either array is {@code null}7904* @since 97905*/7906public static int mismatch(short[] a, int aFromIndex, int aToIndex,7907short[] b, int bFromIndex, int bToIndex) {7908rangeCheck(a.length, aFromIndex, aToIndex);7909rangeCheck(b.length, bFromIndex, bToIndex);79107911int aLength = aToIndex - aFromIndex;7912int bLength = bToIndex - bFromIndex;7913int length = Math.min(aLength, bLength);7914int i = ArraysSupport.mismatch(a, aFromIndex,7915b, bFromIndex,7916length);7917return (i < 0 && aLength != bLength) ? length : i;7918}79197920// Mismatch int79217922/**7923* Finds and returns the index of the first mismatch between two {@code int}7924* arrays, otherwise return -1 if no mismatch is found. The index will be7925* in the range of 0 (inclusive) up to the length (inclusive) of the smaller7926* array.7927*7928* <p>If the two arrays share a common prefix then the returned index is the7929* length of the common prefix and it follows that there is a mismatch7930* between the two elements at that index within the respective arrays.7931* If one array is a proper prefix of the other then the returned index is7932* the length of the smaller array and it follows that the index is only7933* valid for the larger array.7934* Otherwise, there is no mismatch.7935*7936* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common7937* prefix of length {@code pl} if the following expression is true:7938* <pre>{@code7939* pl >= 0 &&7940* pl < Math.min(a.length, b.length) &&7941* Arrays.equals(a, 0, pl, b, 0, pl) &&7942* a[pl] != b[pl]7943* }</pre>7944* Note that a common prefix length of {@code 0} indicates that the first7945* elements from each array mismatch.7946*7947* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper7948* prefix if the following expression is true:7949* <pre>{@code7950* a.length != b.length &&7951* Arrays.equals(a, 0, Math.min(a.length, b.length),7952* b, 0, Math.min(a.length, b.length))7953* }</pre>7954*7955* @param a the first array to be tested for a mismatch7956* @param b the second array to be tested for a mismatch7957* @return the index of the first mismatch between the two arrays,7958* otherwise {@code -1}.7959* @throws NullPointerException7960* if either array is {@code null}7961* @since 97962*/7963public static int mismatch(int[] a, int[] b) {7964int length = Math.min(a.length, b.length); // Check null array refs7965if (a == b)7966return -1;79677968int i = ArraysSupport.mismatch(a, b, length);7969return (i < 0 && a.length != b.length) ? length : i;7970}79717972/**7973* Finds and returns the relative index of the first mismatch between two7974* {@code int} arrays over the specified ranges, otherwise return -1 if no7975* mismatch is found. The index will be in the range of 0 (inclusive) up to7976* the length (inclusive) of the smaller range.7977*7978* <p>If the two arrays, over the specified ranges, share a common prefix7979* then the returned relative index is the length of the common prefix and7980* it follows that there is a mismatch between the two elements at that7981* relative index within the respective arrays.7982* If one array is a proper prefix of the other, over the specified ranges,7983* then the returned relative index is the length of the smaller range and7984* it follows that the relative index is only valid for the array with the7985* larger range.7986* Otherwise, there is no mismatch.7987*7988* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified7989* ranges [{@code aFromIndex}, {@code atoIndex}) and7990* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common7991* prefix of length {@code pl} if the following expression is true:7992* <pre>{@code7993* pl >= 0 &&7994* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&7995* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&7996* a[aFromIndex + pl] != b[bFromIndex + pl]7997* }</pre>7998* Note that a common prefix length of {@code 0} indicates that the first7999* elements from each array mismatch.8000*8001* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8002* ranges [{@code aFromIndex}, {@code atoIndex}) and8003* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8004* prefix if the following expression is true:8005* <pre>{@code8006* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8007* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8008* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))8009* }</pre>8010*8011* @param a the first array to be tested for a mismatch8012* @param aFromIndex the index (inclusive) of the first element in the8013* first array to be tested8014* @param aToIndex the index (exclusive) of the last element in the8015* first array to be tested8016* @param b the second array to be tested for a mismatch8017* @param bFromIndex the index (inclusive) of the first element in the8018* second array to be tested8019* @param bToIndex the index (exclusive) of the last element in the8020* second array to be tested8021* @return the relative index of the first mismatch between the two arrays8022* over the specified ranges, otherwise {@code -1}.8023* @throws IllegalArgumentException8024* if {@code aFromIndex > aToIndex} or8025* if {@code bFromIndex > bToIndex}8026* @throws ArrayIndexOutOfBoundsException8027* if {@code aFromIndex < 0 or aToIndex > a.length} or8028* if {@code bFromIndex < 0 or bToIndex > b.length}8029* @throws NullPointerException8030* if either array is {@code null}8031* @since 98032*/8033public static int mismatch(int[] a, int aFromIndex, int aToIndex,8034int[] b, int bFromIndex, int bToIndex) {8035rangeCheck(a.length, aFromIndex, aToIndex);8036rangeCheck(b.length, bFromIndex, bToIndex);80378038int aLength = aToIndex - aFromIndex;8039int bLength = bToIndex - bFromIndex;8040int length = Math.min(aLength, bLength);8041int i = ArraysSupport.mismatch(a, aFromIndex,8042b, bFromIndex,8043length);8044return (i < 0 && aLength != bLength) ? length : i;8045}80468047// Mismatch long80488049/**8050* Finds and returns the index of the first mismatch between two {@code long}8051* arrays, otherwise return -1 if no mismatch is found. The index will be8052* in the range of 0 (inclusive) up to the length (inclusive) of the smaller8053* array.8054*8055* <p>If the two arrays share a common prefix then the returned index is the8056* length of the common prefix and it follows that there is a mismatch8057* between the two elements at that index within the respective arrays.8058* If one array is a proper prefix of the other then the returned index is8059* the length of the smaller array and it follows that the index is only8060* valid for the larger array.8061* Otherwise, there is no mismatch.8062*8063* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common8064* prefix of length {@code pl} if the following expression is true:8065* <pre>{@code8066* pl >= 0 &&8067* pl < Math.min(a.length, b.length) &&8068* Arrays.equals(a, 0, pl, b, 0, pl) &&8069* a[pl] != b[pl]8070* }</pre>8071* Note that a common prefix length of {@code 0} indicates that the first8072* elements from each array mismatch.8073*8074* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper8075* prefix if the following expression is true:8076* <pre>{@code8077* a.length != b.length &&8078* Arrays.equals(a, 0, Math.min(a.length, b.length),8079* b, 0, Math.min(a.length, b.length))8080* }</pre>8081*8082* @param a the first array to be tested for a mismatch8083* @param b the second array to be tested for a mismatch8084* @return the index of the first mismatch between the two arrays,8085* otherwise {@code -1}.8086* @throws NullPointerException8087* if either array is {@code null}8088* @since 98089*/8090public static int mismatch(long[] a, long[] b) {8091int length = Math.min(a.length, b.length); // Check null array refs8092if (a == b)8093return -1;80948095int i = ArraysSupport.mismatch(a, b, length);8096return (i < 0 && a.length != b.length) ? length : i;8097}80988099/**8100* Finds and returns the relative index of the first mismatch between two8101* {@code long} arrays over the specified ranges, otherwise return -1 if no8102* mismatch is found. The index will be in the range of 0 (inclusive) up to8103* the length (inclusive) of the smaller range.8104*8105* <p>If the two arrays, over the specified ranges, share a common prefix8106* then the returned relative index is the length of the common prefix and8107* it follows that there is a mismatch between the two elements at that8108* relative index within the respective arrays.8109* If one array is a proper prefix of the other, over the specified ranges,8110* then the returned relative index is the length of the smaller range and8111* it follows that the relative index is only valid for the array with the8112* larger range.8113* Otherwise, there is no mismatch.8114*8115* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8116* ranges [{@code aFromIndex}, {@code atoIndex}) and8117* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common8118* prefix of length {@code pl} if the following expression is true:8119* <pre>{@code8120* pl >= 0 &&8121* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&8122* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&8123* a[aFromIndex + pl] != b[bFromIndex + pl]8124* }</pre>8125* Note that a common prefix length of {@code 0} indicates that the first8126* elements from each array mismatch.8127*8128* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8129* ranges [{@code aFromIndex}, {@code atoIndex}) and8130* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8131* prefix if the following expression is true:8132* <pre>{@code8133* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8134* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8135* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))8136* }</pre>8137*8138* @param a the first array to be tested for a mismatch8139* @param aFromIndex the index (inclusive) of the first element in the8140* first array to be tested8141* @param aToIndex the index (exclusive) of the last element in the8142* first array to be tested8143* @param b the second array to be tested for a mismatch8144* @param bFromIndex the index (inclusive) of the first element in the8145* second array to be tested8146* @param bToIndex the index (exclusive) of the last element in the8147* second array to be tested8148* @return the relative index of the first mismatch between the two arrays8149* over the specified ranges, otherwise {@code -1}.8150* @throws IllegalArgumentException8151* if {@code aFromIndex > aToIndex} or8152* if {@code bFromIndex > bToIndex}8153* @throws ArrayIndexOutOfBoundsException8154* if {@code aFromIndex < 0 or aToIndex > a.length} or8155* if {@code bFromIndex < 0 or bToIndex > b.length}8156* @throws NullPointerException8157* if either array is {@code null}8158* @since 98159*/8160public static int mismatch(long[] a, int aFromIndex, int aToIndex,8161long[] b, int bFromIndex, int bToIndex) {8162rangeCheck(a.length, aFromIndex, aToIndex);8163rangeCheck(b.length, bFromIndex, bToIndex);81648165int aLength = aToIndex - aFromIndex;8166int bLength = bToIndex - bFromIndex;8167int length = Math.min(aLength, bLength);8168int i = ArraysSupport.mismatch(a, aFromIndex,8169b, bFromIndex,8170length);8171return (i < 0 && aLength != bLength) ? length : i;8172}81738174// Mismatch float81758176/**8177* Finds and returns the index of the first mismatch between two {@code float}8178* arrays, otherwise return -1 if no mismatch is found. The index will be8179* in the range of 0 (inclusive) up to the length (inclusive) of the smaller8180* array.8181*8182* <p>If the two arrays share a common prefix then the returned index is the8183* length of the common prefix and it follows that there is a mismatch8184* between the two elements at that index within the respective arrays.8185* If one array is a proper prefix of the other then the returned index is8186* the length of the smaller array and it follows that the index is only8187* valid for the larger array.8188* Otherwise, there is no mismatch.8189*8190* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common8191* prefix of length {@code pl} if the following expression is true:8192* <pre>{@code8193* pl >= 0 &&8194* pl < Math.min(a.length, b.length) &&8195* Arrays.equals(a, 0, pl, b, 0, pl) &&8196* Float.compare(a[pl], b[pl]) != 08197* }</pre>8198* Note that a common prefix length of {@code 0} indicates that the first8199* elements from each array mismatch.8200*8201* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper8202* prefix if the following expression is true:8203* <pre>{@code8204* a.length != b.length &&8205* Arrays.equals(a, 0, Math.min(a.length, b.length),8206* b, 0, Math.min(a.length, b.length))8207* }</pre>8208*8209* @param a the first array to be tested for a mismatch8210* @param b the second array to be tested for a mismatch8211* @return the index of the first mismatch between the two arrays,8212* otherwise {@code -1}.8213* @throws NullPointerException8214* if either array is {@code null}8215* @since 98216*/8217public static int mismatch(float[] a, float[] b) {8218int length = Math.min(a.length, b.length); // Check null array refs8219if (a == b)8220return -1;82218222int i = ArraysSupport.mismatch(a, b, length);8223return (i < 0 && a.length != b.length) ? length : i;8224}82258226/**8227* Finds and returns the relative index of the first mismatch between two8228* {@code float} arrays over the specified ranges, otherwise return -1 if no8229* mismatch is found. The index will be in the range of 0 (inclusive) up to8230* the length (inclusive) of the smaller range.8231*8232* <p>If the two arrays, over the specified ranges, share a common prefix8233* then the returned relative index is the length of the common prefix and8234* it follows that there is a mismatch between the two elements at that8235* relative index within the respective arrays.8236* If one array is a proper prefix of the other, over the specified ranges,8237* then the returned relative index is the length of the smaller range and8238* it follows that the relative index is only valid for the array with the8239* larger range.8240* Otherwise, there is no mismatch.8241*8242* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8243* ranges [{@code aFromIndex}, {@code atoIndex}) and8244* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common8245* prefix of length {@code pl} if the following expression is true:8246* <pre>{@code8247* pl >= 0 &&8248* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&8249* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&8250* Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 08251* }</pre>8252* Note that a common prefix length of {@code 0} indicates that the first8253* elements from each array mismatch.8254*8255* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8256* ranges [{@code aFromIndex}, {@code atoIndex}) and8257* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8258* prefix if the following expression is true:8259* <pre>{@code8260* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8261* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8262* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))8263* }</pre>8264*8265* @param a the first array to be tested for a mismatch8266* @param aFromIndex the index (inclusive) of the first element in the8267* first array to be tested8268* @param aToIndex the index (exclusive) of the last element in the8269* first array to be tested8270* @param b the second array to be tested for a mismatch8271* @param bFromIndex the index (inclusive) of the first element in the8272* second array to be tested8273* @param bToIndex the index (exclusive) of the last element in the8274* second array to be tested8275* @return the relative index of the first mismatch between the two arrays8276* over the specified ranges, otherwise {@code -1}.8277* @throws IllegalArgumentException8278* if {@code aFromIndex > aToIndex} or8279* if {@code bFromIndex > bToIndex}8280* @throws ArrayIndexOutOfBoundsException8281* if {@code aFromIndex < 0 or aToIndex > a.length} or8282* if {@code bFromIndex < 0 or bToIndex > b.length}8283* @throws NullPointerException8284* if either array is {@code null}8285* @since 98286*/8287public static int mismatch(float[] a, int aFromIndex, int aToIndex,8288float[] b, int bFromIndex, int bToIndex) {8289rangeCheck(a.length, aFromIndex, aToIndex);8290rangeCheck(b.length, bFromIndex, bToIndex);82918292int aLength = aToIndex - aFromIndex;8293int bLength = bToIndex - bFromIndex;8294int length = Math.min(aLength, bLength);8295int i = ArraysSupport.mismatch(a, aFromIndex,8296b, bFromIndex,8297length);8298return (i < 0 && aLength != bLength) ? length : i;8299}83008301// Mismatch double83028303/**8304* Finds and returns the index of the first mismatch between two8305* {@code double} arrays, otherwise return -1 if no mismatch is found. The8306* index will be in the range of 0 (inclusive) up to the length (inclusive)8307* of the smaller array.8308*8309* <p>If the two arrays share a common prefix then the returned index is the8310* length of the common prefix and it follows that there is a mismatch8311* between the two elements at that index within the respective arrays.8312* If one array is a proper prefix of the other then the returned index is8313* the length of the smaller array and it follows that the index is only8314* valid for the larger array.8315* Otherwise, there is no mismatch.8316*8317* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common8318* prefix of length {@code pl} if the following expression is true:8319* <pre>{@code8320* pl >= 0 &&8321* pl < Math.min(a.length, b.length) &&8322* Arrays.equals(a, 0, pl, b, 0, pl) &&8323* Double.compare(a[pl], b[pl]) != 08324* }</pre>8325* Note that a common prefix length of {@code 0} indicates that the first8326* elements from each array mismatch.8327*8328* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper8329* prefix if the following expression is true:8330* <pre>{@code8331* a.length != b.length &&8332* Arrays.equals(a, 0, Math.min(a.length, b.length),8333* b, 0, Math.min(a.length, b.length))8334* }</pre>8335*8336* @param a the first array to be tested for a mismatch8337* @param b the second array to be tested for a mismatch8338* @return the index of the first mismatch between the two arrays,8339* otherwise {@code -1}.8340* @throws NullPointerException8341* if either array is {@code null}8342* @since 98343*/8344public static int mismatch(double[] a, double[] b) {8345int length = Math.min(a.length, b.length); // Check null array refs8346if (a == b)8347return -1;83488349int i = ArraysSupport.mismatch(a, b, length);8350return (i < 0 && a.length != b.length) ? length : i;8351}83528353/**8354* Finds and returns the relative index of the first mismatch between two8355* {@code double} arrays over the specified ranges, otherwise return -1 if8356* no mismatch is found. The index will be in the range of 0 (inclusive) up8357* to the length (inclusive) of the smaller range.8358*8359* <p>If the two arrays, over the specified ranges, share a common prefix8360* then the returned relative index is the length of the common prefix and8361* it follows that there is a mismatch between the two elements at that8362* relative index within the respective arrays.8363* If one array is a proper prefix of the other, over the specified ranges,8364* then the returned relative index is the length of the smaller range and8365* it follows that the relative index is only valid for the array with the8366* larger range.8367* Otherwise, there is no mismatch.8368*8369* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8370* ranges [{@code aFromIndex}, {@code atoIndex}) and8371* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common8372* prefix of length {@code pl} if the following expression is true:8373* <pre>{@code8374* pl >= 0 &&8375* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&8376* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&8377* Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 08378* }</pre>8379* Note that a common prefix length of {@code 0} indicates that the first8380* elements from each array mismatch.8381*8382* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8383* ranges [{@code aFromIndex}, {@code atoIndex}) and8384* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8385* prefix if the following expression is true:8386* <pre>{@code8387* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8388* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8389* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))8390* }</pre>8391*8392* @param a the first array to be tested for a mismatch8393* @param aFromIndex the index (inclusive) of the first element in the8394* first array to be tested8395* @param aToIndex the index (exclusive) of the last element in the8396* first array to be tested8397* @param b the second array to be tested for a mismatch8398* @param bFromIndex the index (inclusive) of the first element in the8399* second array to be tested8400* @param bToIndex the index (exclusive) of the last element in the8401* second array to be tested8402* @return the relative index of the first mismatch between the two arrays8403* over the specified ranges, otherwise {@code -1}.8404* @throws IllegalArgumentException8405* if {@code aFromIndex > aToIndex} or8406* if {@code bFromIndex > bToIndex}8407* @throws ArrayIndexOutOfBoundsException8408* if {@code aFromIndex < 0 or aToIndex > a.length} or8409* if {@code bFromIndex < 0 or bToIndex > b.length}8410* @throws NullPointerException8411* if either array is {@code null}8412* @since 98413*/8414public static int mismatch(double[] a, int aFromIndex, int aToIndex,8415double[] b, int bFromIndex, int bToIndex) {8416rangeCheck(a.length, aFromIndex, aToIndex);8417rangeCheck(b.length, bFromIndex, bToIndex);84188419int aLength = aToIndex - aFromIndex;8420int bLength = bToIndex - bFromIndex;8421int length = Math.min(aLength, bLength);8422int i = ArraysSupport.mismatch(a, aFromIndex,8423b, bFromIndex,8424length);8425return (i < 0 && aLength != bLength) ? length : i;8426}84278428// Mismatch objects84298430/**8431* Finds and returns the index of the first mismatch between two8432* {@code Object} arrays, otherwise return -1 if no mismatch is found. The8433* index will be in the range of 0 (inclusive) up to the length (inclusive)8434* of the smaller array.8435*8436* <p>If the two arrays share a common prefix then the returned index is the8437* length of the common prefix and it follows that there is a mismatch8438* between the two elements at that index within the respective arrays.8439* If one array is a proper prefix of the other then the returned index is8440* the length of the smaller array and it follows that the index is only8441* valid for the larger array.8442* Otherwise, there is no mismatch.8443*8444* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common8445* prefix of length {@code pl} if the following expression is true:8446* <pre>{@code8447* pl >= 0 &&8448* pl < Math.min(a.length, b.length) &&8449* Arrays.equals(a, 0, pl, b, 0, pl) &&8450* !Objects.equals(a[pl], b[pl])8451* }</pre>8452* Note that a common prefix length of {@code 0} indicates that the first8453* elements from each array mismatch.8454*8455* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper8456* prefix if the following expression is true:8457* <pre>{@code8458* a.length != b.length &&8459* Arrays.equals(a, 0, Math.min(a.length, b.length),8460* b, 0, Math.min(a.length, b.length))8461* }</pre>8462*8463* @param a the first array to be tested for a mismatch8464* @param b the second array to be tested for a mismatch8465* @return the index of the first mismatch between the two arrays,8466* otherwise {@code -1}.8467* @throws NullPointerException8468* if either array is {@code null}8469* @since 98470*/8471public static int mismatch(Object[] a, Object[] b) {8472int length = Math.min(a.length, b.length); // Check null array refs8473if (a == b)8474return -1;84758476for (int i = 0; i < length; i++) {8477if (!Objects.equals(a[i], b[i]))8478return i;8479}84808481return a.length != b.length ? length : -1;8482}84838484/**8485* Finds and returns the relative index of the first mismatch between two8486* {@code Object} arrays over the specified ranges, otherwise return -1 if8487* no mismatch is found. The index will be in the range of 0 (inclusive) up8488* to the length (inclusive) of the smaller range.8489*8490* <p>If the two arrays, over the specified ranges, share a common prefix8491* then the returned relative index is the length of the common prefix and8492* it follows that there is a mismatch between the two elements at that8493* relative index within the respective arrays.8494* If one array is a proper prefix of the other, over the specified ranges,8495* then the returned relative index is the length of the smaller range and8496* it follows that the relative index is only valid for the array with the8497* larger range.8498* Otherwise, there is no mismatch.8499*8500* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8501* ranges [{@code aFromIndex}, {@code atoIndex}) and8502* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common8503* prefix of length {@code pl} if the following expression is true:8504* <pre>{@code8505* pl >= 0 &&8506* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&8507* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&8508* !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])8509* }</pre>8510* Note that a common prefix length of {@code 0} indicates that the first8511* elements from each array mismatch.8512*8513* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8514* ranges [{@code aFromIndex}, {@code atoIndex}) and8515* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8516* prefix if the following expression is true:8517* <pre>{@code8518* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8519* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8520* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))8521* }</pre>8522*8523* @param a the first array to be tested for a mismatch8524* @param aFromIndex the index (inclusive) of the first element in the8525* first array to be tested8526* @param aToIndex the index (exclusive) of the last element in the8527* first array to be tested8528* @param b the second array to be tested for a mismatch8529* @param bFromIndex the index (inclusive) of the first element in the8530* second array to be tested8531* @param bToIndex the index (exclusive) of the last element in the8532* second array to be tested8533* @return the relative index of the first mismatch between the two arrays8534* over the specified ranges, otherwise {@code -1}.8535* @throws IllegalArgumentException8536* if {@code aFromIndex > aToIndex} or8537* if {@code bFromIndex > bToIndex}8538* @throws ArrayIndexOutOfBoundsException8539* if {@code aFromIndex < 0 or aToIndex > a.length} or8540* if {@code bFromIndex < 0 or bToIndex > b.length}8541* @throws NullPointerException8542* if either array is {@code null}8543* @since 98544*/8545public static int mismatch(8546Object[] a, int aFromIndex, int aToIndex,8547Object[] b, int bFromIndex, int bToIndex) {8548rangeCheck(a.length, aFromIndex, aToIndex);8549rangeCheck(b.length, bFromIndex, bToIndex);85508551int aLength = aToIndex - aFromIndex;8552int bLength = bToIndex - bFromIndex;8553int length = Math.min(aLength, bLength);8554for (int i = 0; i < length; i++) {8555if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))8556return i;8557}85588559return aLength != bLength ? length : -1;8560}85618562/**8563* Finds and returns the index of the first mismatch between two8564* {@code Object} arrays, otherwise return -1 if no mismatch is found.8565* The index will be in the range of 0 (inclusive) up to the length8566* (inclusive) of the smaller array.8567*8568* <p>The specified comparator is used to determine if two array elements8569* from the each array are not equal.8570*8571* <p>If the two arrays share a common prefix then the returned index is the8572* length of the common prefix and it follows that there is a mismatch8573* between the two elements at that index within the respective arrays.8574* If one array is a proper prefix of the other then the returned index is8575* the length of the smaller array and it follows that the index is only8576* valid for the larger array.8577* Otherwise, there is no mismatch.8578*8579* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common8580* prefix of length {@code pl} if the following expression is true:8581* <pre>{@code8582* pl >= 0 &&8583* pl < Math.min(a.length, b.length) &&8584* Arrays.equals(a, 0, pl, b, 0, pl, cmp)8585* cmp.compare(a[pl], b[pl]) != 08586* }</pre>8587* Note that a common prefix length of {@code 0} indicates that the first8588* elements from each array mismatch.8589*8590* <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper8591* prefix if the following expression is true:8592* <pre>{@code8593* a.length != b.length &&8594* Arrays.equals(a, 0, Math.min(a.length, b.length),8595* b, 0, Math.min(a.length, b.length),8596* cmp)8597* }</pre>8598*8599* @param a the first array to be tested for a mismatch8600* @param b the second array to be tested for a mismatch8601* @param cmp the comparator to compare array elements8602* @param <T> the type of array elements8603* @return the index of the first mismatch between the two arrays,8604* otherwise {@code -1}.8605* @throws NullPointerException8606* if either array or the comparator is {@code null}8607* @since 98608*/8609public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {8610Objects.requireNonNull(cmp);8611int length = Math.min(a.length, b.length); // Check null array refs8612if (a == b)8613return -1;86148615for (int i = 0; i < length; i++) {8616T oa = a[i];8617T ob = b[i];8618if (oa != ob) {8619// Null-value comparison is deferred to the comparator8620int v = cmp.compare(oa, ob);8621if (v != 0) {8622return i;8623}8624}8625}86268627return a.length != b.length ? length : -1;8628}86298630/**8631* Finds and returns the relative index of the first mismatch between two8632* {@code Object} arrays over the specified ranges, otherwise return -1 if8633* no mismatch is found. The index will be in the range of 0 (inclusive) up8634* to the length (inclusive) of the smaller range.8635*8636* <p>If the two arrays, over the specified ranges, share a common prefix8637* then the returned relative index is the length of the common prefix and8638* it follows that there is a mismatch between the two elements at that8639* relative index within the respective arrays.8640* If one array is a proper prefix of the other, over the specified ranges,8641* then the returned relative index is the length of the smaller range and8642* it follows that the relative index is only valid for the array with the8643* larger range.8644* Otherwise, there is no mismatch.8645*8646* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8647* ranges [{@code aFromIndex}, {@code atoIndex}) and8648* [{@code bFromIndex}, {@code btoIndex}) respectively, share a common8649* prefix of length {@code pl} if the following expression is true:8650* <pre>{@code8651* pl >= 0 &&8652* pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&8653* Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&8654* cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 08655* }</pre>8656* Note that a common prefix length of {@code 0} indicates that the first8657* elements from each array mismatch.8658*8659* <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified8660* ranges [{@code aFromIndex}, {@code atoIndex}) and8661* [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper8662* prefix if the following expression is true:8663* <pre>{@code8664* (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&8665* Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8666* b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),8667* cmp)8668* }</pre>8669*8670* @param a the first array to be tested for a mismatch8671* @param aFromIndex the index (inclusive) of the first element in the8672* first array to be tested8673* @param aToIndex the index (exclusive) of the last element in the8674* first array to be tested8675* @param b the second array to be tested for a mismatch8676* @param bFromIndex the index (inclusive) of the first element in the8677* second array to be tested8678* @param bToIndex the index (exclusive) of the last element in the8679* second array to be tested8680* @param cmp the comparator to compare array elements8681* @param <T> the type of array elements8682* @return the relative index of the first mismatch between the two arrays8683* over the specified ranges, otherwise {@code -1}.8684* @throws IllegalArgumentException8685* if {@code aFromIndex > aToIndex} or8686* if {@code bFromIndex > bToIndex}8687* @throws ArrayIndexOutOfBoundsException8688* if {@code aFromIndex < 0 or aToIndex > a.length} or8689* if {@code bFromIndex < 0 or bToIndex > b.length}8690* @throws NullPointerException8691* if either array or the comparator is {@code null}8692* @since 98693*/8694public static <T> int mismatch(8695T[] a, int aFromIndex, int aToIndex,8696T[] b, int bFromIndex, int bToIndex,8697Comparator<? super T> cmp) {8698Objects.requireNonNull(cmp);8699rangeCheck(a.length, aFromIndex, aToIndex);8700rangeCheck(b.length, bFromIndex, bToIndex);87018702int aLength = aToIndex - aFromIndex;8703int bLength = bToIndex - bFromIndex;8704int length = Math.min(aLength, bLength);8705for (int i = 0; i < length; i++) {8706T oa = a[aFromIndex++];8707T ob = b[bFromIndex++];8708if (oa != ob) {8709// Null-value comparison is deferred to the comparator8710int v = cmp.compare(oa, ob);8711if (v != 0) {8712return i;8713}8714}8715}87168717return aLength != bLength ? length : -1;8718}8719}872087218722