jaroslav@557: /* jaroslav@557: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.util; jaroslav@557: jaroslav@557: import java.lang.reflect.*; jaroslav@557: jaroslav@557: /** jaroslav@557: * This class contains various methods for manipulating arrays (such as jaroslav@557: * sorting and searching). This class also contains a static factory jaroslav@557: * that allows arrays to be viewed as lists. jaroslav@557: * jaroslav@557: *

The methods in this class all throw a {@code NullPointerException}, jaroslav@557: * if the specified array reference is null, except where noted. jaroslav@557: * jaroslav@557: *

The documentation for the methods contained in this class includes jaroslav@557: * briefs description of the implementations. Such descriptions should jaroslav@557: * be regarded as implementation notes, rather than parts of the jaroslav@557: * specification. Implementors should feel free to substitute other jaroslav@557: * algorithms, so long as the specification itself is adhered to. (For jaroslav@557: * example, the algorithm used by {@code sort(Object[])} does not have to be jaroslav@557: * a MergeSort, but it does have to be stable.) jaroslav@557: * jaroslav@557: *

This class is a member of the jaroslav@557: * jaroslav@557: * Java Collections Framework. jaroslav@557: * jaroslav@557: * @author Josh Bloch jaroslav@557: * @author Neal Gafter jaroslav@557: * @author John Rose jaroslav@557: * @since 1.2 jaroslav@557: */ jaroslav@557: public class Arrays { jaroslav@557: jaroslav@557: // Suppresses default constructor, ensuring non-instantiability. jaroslav@557: private Arrays() {} jaroslav@557: jaroslav@557: /* jaroslav@557: * Sorting of primitive type arrays. jaroslav@557: */ jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(int[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(int[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(long[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(long[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(short[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(short[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(char[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(char[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(byte[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(byte[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

The {@code <} relation does not provide a total order on all float jaroslav@557: * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} jaroslav@557: * value compares neither less than, greater than, nor equal to any value, jaroslav@557: * even itself. This method uses the total order imposed by the method jaroslav@557: * {@link Float#compareTo}: {@code -0.0f} is treated as less than value jaroslav@557: * {@code 0.0f} and {@code Float.NaN} is considered greater than any jaroslav@557: * other value and all {@code Float.NaN} values are considered equal. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(float[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

The {@code <} relation does not provide a total order on all float jaroslav@557: * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} jaroslav@557: * value compares neither less than, greater than, nor equal to any value, jaroslav@557: * even itself. This method uses the total order imposed by the method jaroslav@557: * {@link Float#compareTo}: {@code -0.0f} is treated as less than value jaroslav@557: * {@code 0.0f} and {@code Float.NaN} is considered greater than any jaroslav@557: * other value and all {@code Float.NaN} values are considered equal. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(float[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array into ascending numerical order. jaroslav@557: * jaroslav@557: *

The {@code <} relation does not provide a total order on all double jaroslav@557: * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} jaroslav@557: * value compares neither less than, greater than, nor equal to any value, jaroslav@557: * even itself. This method uses the total order imposed by the method jaroslav@557: * {@link Double#compareTo}: {@code -0.0d} is treated as less than value jaroslav@557: * {@code 0.0d} and {@code Double.NaN} is considered greater than any jaroslav@557: * other value and all {@code Double.NaN} values are considered equal. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: */ jaroslav@557: public static void sort(double[] a) { jaroslav@557: DualPivotQuicksort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the array into ascending order. The range jaroslav@557: * to be sorted extends from the index {@code fromIndex}, inclusive, to jaroslav@557: * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, jaroslav@557: * the range to be sorted is empty. jaroslav@557: * jaroslav@557: *

The {@code <} relation does not provide a total order on all double jaroslav@557: * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} jaroslav@557: * value compares neither less than, greater than, nor equal to any value, jaroslav@557: * even itself. This method uses the total order imposed by the method jaroslav@557: * {@link Double#compareTo}: {@code -0.0d} is treated as less than value jaroslav@557: * {@code 0.0d} and {@code Double.NaN} is considered greater than any jaroslav@557: * other value and all {@code Double.NaN} values are considered equal. jaroslav@557: * jaroslav@557: *

Implementation note: The sorting algorithm is a Dual-Pivot Quicksort jaroslav@557: * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm jaroslav@557: * offers O(n log(n)) performance on many data sets that cause other jaroslav@557: * quicksorts to degrade to quadratic performance, and is typically jaroslav@557: * faster than traditional (one-pivot) Quicksort implementations. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element, inclusive, to be sorted jaroslav@557: * @param toIndex the index of the last element, exclusive, to be sorted jaroslav@557: * jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0} or {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(double[] a, int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); jaroslav@557: } jaroslav@557: jaroslav@557: /* jaroslav@557: * Sorting of complex type arrays. jaroslav@557: */ jaroslav@557: jaroslav@557: /** jaroslav@557: * Old merge sort implementation can be selected (for jaroslav@557: * compatibility with broken comparators) using a system property. jaroslav@557: * Cannot be a static boolean in the enclosing class due to jaroslav@557: * circular dependencies. To be removed in a future release. jaroslav@557: */ jaroslav@557: static final class LegacyMergeSort { jaroslav@568: private static final boolean userRequested = false; jaroslav@557: } jaroslav@557: jaroslav@557: /* jaroslav@557: * If this platform has an optimizing VM, check whether ComparableTimSort jaroslav@557: * offers any performance benefit over TimSort in conjunction with a jaroslav@557: * comparator that returns: jaroslav@557: * {@code ((Comparable)first).compareTo(Second)}. jaroslav@557: * If not, you are better off deleting ComparableTimSort to jaroslav@557: * eliminate the code duplication. In other words, the commented jaroslav@557: * out code below is the preferable implementation for sorting jaroslav@557: * arrays of Comparables if it offers sufficient performance. jaroslav@557: */ jaroslav@557: jaroslav@557: // /** jaroslav@557: // * A comparator that implements the natural ordering of a group of jaroslav@557: // * mutually comparable elements. Using this comparator saves us jaroslav@557: // * from duplicating most of the code in this file (one version for jaroslav@557: // * Comparables, one for explicit Comparators). jaroslav@557: // */ jaroslav@557: // private static final Comparator NATURAL_ORDER = jaroslav@557: // new Comparator() { jaroslav@557: // @SuppressWarnings("unchecked") jaroslav@557: // public int compare(Object first, Object second) { jaroslav@557: // return ((Comparable)first).compareTo(second); jaroslav@557: // } jaroslav@557: // }; jaroslav@557: // jaroslav@557: // public static void sort(Object[] a) { jaroslav@557: // sort(a, 0, a.length, NATURAL_ORDER); jaroslav@557: // } jaroslav@557: // jaroslav@557: // public static void sort(Object[] a, int fromIndex, int toIndex) { jaroslav@557: // sort(a, fromIndex, toIndex, NATURAL_ORDER); jaroslav@557: // } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array of objects into ascending order, according jaroslav@557: * to the {@linkplain Comparable natural ordering} of its elements. jaroslav@557: * All elements in the array must implement the {@link Comparable} jaroslav@557: * interface. Furthermore, all elements in the array must be jaroslav@557: * mutually comparable (that is, {@code e1.compareTo(e2)} must jaroslav@557: * not throw a {@code ClassCastException} for any elements {@code e1} jaroslav@557: * and {@code e2} in the array). jaroslav@557: * jaroslav@557: *

This sort is guaranteed to be stable: equal elements will jaroslav@557: * not be reordered as a result of the sort. jaroslav@557: * jaroslav@557: *

Implementation note: This implementation is a stable, adaptive, jaroslav@557: * iterative mergesort that requires far fewer than n lg(n) comparisons jaroslav@557: * when the input array is partially sorted, while offering the jaroslav@557: * performance of a traditional mergesort when the input array is jaroslav@557: * randomly ordered. If the input array is nearly sorted, the jaroslav@557: * implementation requires approximately n comparisons. Temporary jaroslav@557: * storage requirements vary from a small constant for nearly sorted jaroslav@557: * input arrays to n/2 object references for randomly ordered input jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

The implementation takes equal advantage of ascending and jaroslav@557: * descending order in its input array, and can take advantage of jaroslav@557: * ascending and descending order in different parts of the the same jaroslav@557: * input array. It is well-suited to merging two or more sorted arrays: jaroslav@557: * simply concatenate the arrays and sort the resulting array. jaroslav@557: * jaroslav@557: *

The implementation was adapted from Tim Peters's list sort for Python jaroslav@557: * ( jaroslav@557: * TimSort). It uses techiques from Peter McIlroy's "Optimistic jaroslav@557: * Sorting and Information Theoretic Complexity", in Proceedings of the jaroslav@557: * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, jaroslav@557: * January 1993. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @throws ClassCastException if the array contains elements that are not jaroslav@557: * mutually comparable (for example, strings and integers) jaroslav@557: * @throws IllegalArgumentException (optional) if the natural jaroslav@557: * ordering of the array elements is found to violate the jaroslav@557: * {@link Comparable} contract jaroslav@557: */ jaroslav@557: public static void sort(Object[] a) { jaroslav@557: if (LegacyMergeSort.userRequested) jaroslav@557: legacyMergeSort(a); jaroslav@557: else jaroslav@557: ComparableTimSort.sort(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** To be removed in a future release. */ jaroslav@557: private static void legacyMergeSort(Object[] a) { jaroslav@557: Object[] aux = a.clone(); jaroslav@557: mergeSort(aux, a, 0, a.length, 0); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the specified array of objects into jaroslav@557: * ascending order, according to the jaroslav@557: * {@linkplain Comparable natural ordering} of its jaroslav@557: * elements. The range to be sorted extends from index jaroslav@557: * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. jaroslav@557: * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All jaroslav@557: * elements in this range must implement the {@link Comparable} jaroslav@557: * interface. Furthermore, all elements in this range must be mutually jaroslav@557: * comparable (that is, {@code e1.compareTo(e2)} must not throw a jaroslav@557: * {@code ClassCastException} for any elements {@code e1} and jaroslav@557: * {@code e2} in the array). jaroslav@557: * jaroslav@557: *

This sort is guaranteed to be stable: equal elements will jaroslav@557: * not be reordered as a result of the sort. jaroslav@557: * jaroslav@557: *

Implementation note: This implementation is a stable, adaptive, jaroslav@557: * iterative mergesort that requires far fewer than n lg(n) comparisons jaroslav@557: * when the input array is partially sorted, while offering the jaroslav@557: * performance of a traditional mergesort when the input array is jaroslav@557: * randomly ordered. If the input array is nearly sorted, the jaroslav@557: * implementation requires approximately n comparisons. Temporary jaroslav@557: * storage requirements vary from a small constant for nearly sorted jaroslav@557: * input arrays to n/2 object references for randomly ordered input jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

The implementation takes equal advantage of ascending and jaroslav@557: * descending order in its input array, and can take advantage of jaroslav@557: * ascending and descending order in different parts of the the same jaroslav@557: * input array. It is well-suited to merging two or more sorted arrays: jaroslav@557: * simply concatenate the arrays and sort the resulting array. jaroslav@557: * jaroslav@557: *

The implementation was adapted from Tim Peters's list sort for Python jaroslav@557: * ( jaroslav@557: * TimSort). It uses techiques from Peter McIlroy's "Optimistic jaroslav@557: * Sorting and Information Theoretic Complexity", in Proceedings of the jaroslav@557: * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, jaroslav@557: * January 1993. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * sorted jaroslav@557: * @param toIndex the index of the last element (exclusive) to be sorted jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} or jaroslav@557: * (optional) if the natural ordering of the array elements is jaroslav@557: * found to violate the {@link Comparable} contract jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or jaroslav@557: * {@code toIndex > a.length} jaroslav@557: * @throws ClassCastException if the array contains elements that are jaroslav@557: * not mutually comparable (for example, strings and jaroslav@557: * integers). jaroslav@557: */ jaroslav@557: public static void sort(Object[] a, int fromIndex, int toIndex) { jaroslav@557: if (LegacyMergeSort.userRequested) jaroslav@557: legacyMergeSort(a, fromIndex, toIndex); jaroslav@557: else jaroslav@557: ComparableTimSort.sort(a, fromIndex, toIndex); jaroslav@557: } jaroslav@557: jaroslav@557: /** To be removed in a future release. */ jaroslav@557: private static void legacyMergeSort(Object[] a, jaroslav@557: int fromIndex, int toIndex) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: Object[] aux = copyOfRange(a, fromIndex, toIndex); jaroslav@557: mergeSort(aux, a, fromIndex, toIndex, -fromIndex); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Tuning parameter: list size at or below which insertion sort will be jaroslav@557: * used in preference to mergesort. jaroslav@557: * To be removed in a future release. jaroslav@557: */ jaroslav@557: private static final int INSERTIONSORT_THRESHOLD = 7; jaroslav@557: jaroslav@557: /** jaroslav@557: * Src is the source array that starts at index 0 jaroslav@557: * Dest is the (possibly larger) array destination with a possible offset jaroslav@557: * low is the index in dest to start sorting jaroslav@557: * high is the end index in dest to end sorting jaroslav@557: * off is the offset to generate corresponding low, high in src jaroslav@557: * To be removed in a future release. jaroslav@557: */ jaroslav@557: private static void mergeSort(Object[] src, jaroslav@557: Object[] dest, jaroslav@557: int low, jaroslav@557: int high, jaroslav@557: int off) { jaroslav@557: int length = high - low; jaroslav@557: jaroslav@557: // Insertion sort on smallest arrays jaroslav@557: if (length < INSERTIONSORT_THRESHOLD) { jaroslav@557: for (int i=low; ilow && jaroslav@557: ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) jaroslav@557: swap(dest, j, j-1); jaroslav@557: return; jaroslav@557: } jaroslav@557: jaroslav@557: // Recursively sort halves of dest into src jaroslav@557: int destLow = low; jaroslav@557: int destHigh = high; jaroslav@557: low += off; jaroslav@557: high += off; jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: mergeSort(dest, src, low, mid, -off); jaroslav@557: mergeSort(dest, src, mid, high, -off); jaroslav@557: jaroslav@557: // If list is already sorted, just copy from src to dest. This is an jaroslav@557: // optimization that results in faster sorts for nearly ordered lists. jaroslav@557: if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { jaroslav@557: System.arraycopy(src, low, dest, destLow, length); jaroslav@557: return; jaroslav@557: } jaroslav@557: jaroslav@557: // Merge sorted halves (now in src) into dest jaroslav@557: for(int i = destLow, p = low, q = mid; i < destHigh; i++) { jaroslav@557: if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) jaroslav@557: dest[i] = src[p++]; jaroslav@557: else jaroslav@557: dest[i] = src[q++]; jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Swaps x[a] with x[b]. jaroslav@557: */ jaroslav@557: private static void swap(Object[] x, int a, int b) { jaroslav@557: Object t = x[a]; jaroslav@557: x[a] = x[b]; jaroslav@557: x[b] = t; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified array of objects according to the order induced by jaroslav@557: * the specified comparator. All elements in the array must be jaroslav@557: * mutually comparable by the specified comparator (that is, jaroslav@557: * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} jaroslav@557: * for any elements {@code e1} and {@code e2} in the array). jaroslav@557: * jaroslav@557: *

This sort is guaranteed to be stable: equal elements will jaroslav@557: * not be reordered as a result of the sort. jaroslav@557: * jaroslav@557: *

Implementation note: This implementation is a stable, adaptive, jaroslav@557: * iterative mergesort that requires far fewer than n lg(n) comparisons jaroslav@557: * when the input array is partially sorted, while offering the jaroslav@557: * performance of a traditional mergesort when the input array is jaroslav@557: * randomly ordered. If the input array is nearly sorted, the jaroslav@557: * implementation requires approximately n comparisons. Temporary jaroslav@557: * storage requirements vary from a small constant for nearly sorted jaroslav@557: * input arrays to n/2 object references for randomly ordered input jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

The implementation takes equal advantage of ascending and jaroslav@557: * descending order in its input array, and can take advantage of jaroslav@557: * ascending and descending order in different parts of the the same jaroslav@557: * input array. It is well-suited to merging two or more sorted arrays: jaroslav@557: * simply concatenate the arrays and sort the resulting array. jaroslav@557: * jaroslav@557: *

The implementation was adapted from Tim Peters's list sort for Python jaroslav@557: * ( jaroslav@557: * TimSort). It uses techiques from Peter McIlroy's "Optimistic jaroslav@557: * Sorting and Information Theoretic Complexity", in Proceedings of the jaroslav@557: * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, jaroslav@557: * January 1993. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param c the comparator to determine the order of the array. A jaroslav@557: * {@code null} value indicates that the elements' jaroslav@557: * {@linkplain Comparable natural ordering} should be used. jaroslav@557: * @throws ClassCastException if the array contains elements that are jaroslav@557: * not mutually comparable using the specified comparator jaroslav@557: * @throws IllegalArgumentException (optional) if the comparator is jaroslav@557: * found to violate the {@link Comparator} contract jaroslav@557: */ jaroslav@557: public static void sort(T[] a, Comparator c) { jaroslav@557: if (LegacyMergeSort.userRequested) jaroslav@557: legacyMergeSort(a, c); jaroslav@557: else jaroslav@557: TimSort.sort(a, c); jaroslav@557: } jaroslav@557: jaroslav@557: /** To be removed in a future release. */ jaroslav@557: private static void legacyMergeSort(T[] a, Comparator c) { jaroslav@557: T[] aux = a.clone(); jaroslav@557: if (c==null) jaroslav@557: mergeSort(aux, a, 0, a.length, 0); jaroslav@557: else jaroslav@557: mergeSort(aux, a, 0, a.length, 0, c); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Sorts the specified range of the specified array of objects according jaroslav@557: * to the order induced by the specified comparator. The range to be jaroslav@557: * sorted extends from index {@code fromIndex}, inclusive, to index jaroslav@557: * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the jaroslav@557: * range to be sorted is empty.) All elements in the range must be jaroslav@557: * mutually comparable by the specified comparator (that is, jaroslav@557: * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} jaroslav@557: * for any elements {@code e1} and {@code e2} in the range). jaroslav@557: * jaroslav@557: *

This sort is guaranteed to be stable: equal elements will jaroslav@557: * not be reordered as a result of the sort. jaroslav@557: * jaroslav@557: *

Implementation note: This implementation is a stable, adaptive, jaroslav@557: * iterative mergesort that requires far fewer than n lg(n) comparisons jaroslav@557: * when the input array is partially sorted, while offering the jaroslav@557: * performance of a traditional mergesort when the input array is jaroslav@557: * randomly ordered. If the input array is nearly sorted, the jaroslav@557: * implementation requires approximately n comparisons. Temporary jaroslav@557: * storage requirements vary from a small constant for nearly sorted jaroslav@557: * input arrays to n/2 object references for randomly ordered input jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

The implementation takes equal advantage of ascending and jaroslav@557: * descending order in its input array, and can take advantage of jaroslav@557: * ascending and descending order in different parts of the the same jaroslav@557: * input array. It is well-suited to merging two or more sorted arrays: jaroslav@557: * simply concatenate the arrays and sort the resulting array. jaroslav@557: * jaroslav@557: *

The implementation was adapted from Tim Peters's list sort for Python jaroslav@557: * ( jaroslav@557: * TimSort). It uses techiques from Peter McIlroy's "Optimistic jaroslav@557: * Sorting and Information Theoretic Complexity", in Proceedings of the jaroslav@557: * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, jaroslav@557: * January 1993. jaroslav@557: * jaroslav@557: * @param a the array to be sorted jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * sorted jaroslav@557: * @param toIndex the index of the last element (exclusive) to be sorted jaroslav@557: * @param c the comparator to determine the order of the array. A jaroslav@557: * {@code null} value indicates that the elements' jaroslav@557: * {@linkplain Comparable natural ordering} should be used. jaroslav@557: * @throws ClassCastException if the array contains elements that are not jaroslav@557: * mutually comparable using the specified comparator. jaroslav@557: * @throws IllegalArgumentException if {@code fromIndex > toIndex} or jaroslav@557: * (optional) if the comparator is found to violate the jaroslav@557: * {@link Comparator} contract jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or jaroslav@557: * {@code toIndex > a.length} jaroslav@557: */ jaroslav@557: public static void sort(T[] a, int fromIndex, int toIndex, jaroslav@557: Comparator c) { jaroslav@557: if (LegacyMergeSort.userRequested) jaroslav@557: legacyMergeSort(a, fromIndex, toIndex, c); jaroslav@557: else jaroslav@557: TimSort.sort(a, fromIndex, toIndex, c); jaroslav@557: } jaroslav@557: jaroslav@557: /** To be removed in a future release. */ jaroslav@557: private static void legacyMergeSort(T[] a, int fromIndex, int toIndex, jaroslav@557: Comparator c) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: T[] aux = copyOfRange(a, fromIndex, toIndex); jaroslav@557: if (c==null) jaroslav@557: mergeSort(aux, a, fromIndex, toIndex, -fromIndex); jaroslav@557: else jaroslav@557: mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Src is the source array that starts at index 0 jaroslav@557: * Dest is the (possibly larger) array destination with a possible offset jaroslav@557: * low is the index in dest to start sorting jaroslav@557: * high is the end index in dest to end sorting jaroslav@557: * off is the offset into src corresponding to low in dest jaroslav@557: * To be removed in a future release. jaroslav@557: */ jaroslav@557: private static void mergeSort(Object[] src, jaroslav@557: Object[] dest, jaroslav@557: int low, int high, int off, jaroslav@557: Comparator c) { jaroslav@557: int length = high - low; jaroslav@557: jaroslav@557: // Insertion sort on smallest arrays jaroslav@557: if (length < INSERTIONSORT_THRESHOLD) { jaroslav@557: for (int i=low; ilow && c.compare(dest[j-1], dest[j])>0; j--) jaroslav@557: swap(dest, j, j-1); jaroslav@557: return; jaroslav@557: } jaroslav@557: jaroslav@557: // Recursively sort halves of dest into src jaroslav@557: int destLow = low; jaroslav@557: int destHigh = high; jaroslav@557: low += off; jaroslav@557: high += off; jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: mergeSort(dest, src, low, mid, -off, c); jaroslav@557: mergeSort(dest, src, mid, high, -off, c); jaroslav@557: jaroslav@557: // If list is already sorted, just copy from src to dest. This is an jaroslav@557: // optimization that results in faster sorts for nearly ordered lists. jaroslav@557: if (c.compare(src[mid-1], src[mid]) <= 0) { jaroslav@557: System.arraycopy(src, low, dest, destLow, length); jaroslav@557: return; jaroslav@557: } jaroslav@557: jaroslav@557: // Merge sorted halves (now in src) into dest jaroslav@557: for(int i = destLow, p = low, q = mid; i < destHigh; i++) { jaroslav@557: if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) jaroslav@557: dest[i] = src[p++]; jaroslav@557: else jaroslav@557: dest[i] = src[q++]; jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Checks that {@code fromIndex} and {@code toIndex} are in jaroslav@557: * the range and throws an appropriate exception, if they aren't. jaroslav@557: */ jaroslav@557: private static void rangeCheck(int length, int fromIndex, int toIndex) { jaroslav@557: if (fromIndex > toIndex) { jaroslav@557: throw new IllegalArgumentException( jaroslav@557: "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); jaroslav@557: } jaroslav@557: if (fromIndex < 0) { jaroslav@557: throw new ArrayIndexOutOfBoundsException(fromIndex); jaroslav@557: } jaroslav@557: if (toIndex > length) { jaroslav@557: throw new ArrayIndexOutOfBoundsException(toIndex); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: // Searching jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of longs for the specified value using the jaroslav@557: * binary search algorithm. The array must be sorted (as jaroslav@557: * by the {@link #sort(long[])} method) prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(long[] a, long key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of longs for the specified value using the jaroslav@557: * binary search algorithm. jaroslav@557: * The range must be sorted (as jaroslav@557: * by the {@link #sort(long[], int, int)} method) jaroslav@557: * prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(long[] a, int fromIndex, int toIndex, jaroslav@557: long key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(long[] a, int fromIndex, int toIndex, jaroslav@557: long key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: long midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of ints for the specified value using the jaroslav@557: * binary search algorithm. The array must be sorted (as jaroslav@557: * by the {@link #sort(int[])} method) prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(int[] a, int key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of ints for the specified value using the jaroslav@557: * binary search algorithm. jaroslav@557: * The range must be sorted (as jaroslav@557: * by the {@link #sort(int[], int, int)} method) jaroslav@557: * prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(int[] a, int fromIndex, int toIndex, jaroslav@557: int key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(int[] a, int fromIndex, int toIndex, jaroslav@557: int key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: int midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of shorts for the specified value using jaroslav@557: * the binary search algorithm. The array must be sorted jaroslav@557: * (as by the {@link #sort(short[])} method) prior to making this call. If jaroslav@557: * it is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(short[] a, short key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of shorts for the specified value using jaroslav@557: * the binary search algorithm. jaroslav@557: * The range must be sorted jaroslav@557: * (as by the {@link #sort(short[], int, int)} method) jaroslav@557: * prior to making this call. If jaroslav@557: * it is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(short[] a, int fromIndex, int toIndex, jaroslav@557: short key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(short[] a, int fromIndex, int toIndex, jaroslav@557: short key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: short midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of chars for the specified value using the jaroslav@557: * binary search algorithm. The array must be sorted (as jaroslav@557: * by the {@link #sort(char[])} method) prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(char[] a, char key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of chars for the specified value using the jaroslav@557: * binary search algorithm. jaroslav@557: * The range must be sorted (as jaroslav@557: * by the {@link #sort(char[], int, int)} method) jaroslav@557: * prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(char[] a, int fromIndex, int toIndex, jaroslav@557: char key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(char[] a, int fromIndex, int toIndex, jaroslav@557: char key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: char midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of bytes for the specified value using the jaroslav@557: * binary search algorithm. The array must be sorted (as jaroslav@557: * by the {@link #sort(byte[])} method) prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(byte[] a, byte key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of bytes for the specified value using the jaroslav@557: * binary search algorithm. jaroslav@557: * The range must be sorted (as jaroslav@557: * by the {@link #sort(byte[], int, int)} method) jaroslav@557: * prior to making this call. If it jaroslav@557: * is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(byte[] a, int fromIndex, int toIndex, jaroslav@557: byte key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(byte[] a, int fromIndex, int toIndex, jaroslav@557: byte key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: byte midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of doubles for the specified value using jaroslav@557: * the binary search algorithm. The array must be sorted jaroslav@557: * (as by the {@link #sort(double[])} method) prior to making this call. jaroslav@557: * If it is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. This method considers all NaN values to be jaroslav@557: * equivalent and equal. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(double[] a, double key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of doubles for the specified value using jaroslav@557: * the binary search algorithm. jaroslav@557: * The range must be sorted jaroslav@557: * (as by the {@link #sort(double[], int, int)} method) jaroslav@557: * prior to making this call. jaroslav@557: * If it is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. This method considers all NaN values to be jaroslav@557: * equivalent and equal. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(double[] a, int fromIndex, int toIndex, jaroslav@557: double key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(double[] a, int fromIndex, int toIndex, jaroslav@557: double key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: double midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; // Neither val is NaN, thisVal is smaller jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; // Neither val is NaN, thisVal is larger jaroslav@557: else { jaroslav@557: long midBits = Double.doubleToLongBits(midVal); jaroslav@557: long keyBits = Double.doubleToLongBits(key); jaroslav@557: if (midBits == keyBits) // Values are equal jaroslav@557: return mid; // Key found jaroslav@557: else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) jaroslav@557: low = mid + 1; jaroslav@557: else // (0.0, -0.0) or (NaN, !NaN) jaroslav@557: high = mid - 1; jaroslav@557: } jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array of floats for the specified value using jaroslav@557: * the binary search algorithm. The array must be sorted jaroslav@557: * (as by the {@link #sort(float[])} method) prior to making this call. If jaroslav@557: * it is not sorted, the results are undefined. If the array contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. This method considers all NaN values to be jaroslav@557: * equivalent and equal. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: */ jaroslav@557: public static int binarySearch(float[] a, float key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array of floats for the specified value using jaroslav@557: * the binary search algorithm. jaroslav@557: * The range must be sorted jaroslav@557: * (as by the {@link #sort(float[], int, int)} method) jaroslav@557: * prior to making this call. If jaroslav@557: * it is not sorted, the results are undefined. If the range contains jaroslav@557: * multiple elements with the specified value, there is no guarantee which jaroslav@557: * one will be found. This method considers all NaN values to be jaroslav@557: * equivalent and equal. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(float[] a, int fromIndex, int toIndex, jaroslav@557: float key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(float[] a, int fromIndex, int toIndex, jaroslav@557: float key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: float midVal = a[mid]; jaroslav@557: jaroslav@557: if (midVal < key) jaroslav@557: low = mid + 1; // Neither val is NaN, thisVal is smaller jaroslav@557: else if (midVal > key) jaroslav@557: high = mid - 1; // Neither val is NaN, thisVal is larger jaroslav@557: else { jaroslav@557: int midBits = Float.floatToIntBits(midVal); jaroslav@557: int keyBits = Float.floatToIntBits(key); jaroslav@557: if (midBits == keyBits) // Values are equal jaroslav@557: return mid; // Key found jaroslav@557: else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) jaroslav@557: low = mid + 1; jaroslav@557: else // (0.0, -0.0) or (NaN, !NaN) jaroslav@557: high = mid - 1; jaroslav@557: } jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array for the specified object using the binary jaroslav@557: * search algorithm. The array must be sorted into ascending order jaroslav@557: * according to the jaroslav@557: * {@linkplain Comparable natural ordering} jaroslav@557: * of its elements (as by the jaroslav@557: * {@link #sort(Object[])} method) prior to making this call. jaroslav@557: * If it is not sorted, the results are undefined. jaroslav@557: * (If the array contains elements that are not mutually comparable (for jaroslav@557: * example, strings and integers), it cannot be sorted according jaroslav@557: * to the natural ordering of its elements, hence results are undefined.) jaroslav@557: * If the array contains multiple jaroslav@557: * elements equal to the specified object, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws ClassCastException if the search key is not comparable to the jaroslav@557: * elements of the array. jaroslav@557: */ jaroslav@557: public static int binarySearch(Object[] a, Object key) { jaroslav@557: return binarySearch0(a, 0, a.length, key); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array for the specified object using the binary jaroslav@557: * search algorithm. jaroslav@557: * The range must be sorted into ascending order jaroslav@557: * according to the jaroslav@557: * {@linkplain Comparable natural ordering} jaroslav@557: * of its elements (as by the jaroslav@557: * {@link #sort(Object[], int, int)} method) prior to making this jaroslav@557: * call. If it is not sorted, the results are undefined. jaroslav@557: * (If the range contains elements that are not mutually comparable (for jaroslav@557: * example, strings and integers), it cannot be sorted according jaroslav@557: * to the natural ordering of its elements, hence results are undefined.) jaroslav@557: * If the range contains multiple jaroslav@557: * elements equal to the specified object, there is no guarantee which jaroslav@557: * one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws ClassCastException if the search key is not comparable to the jaroslav@557: * elements of the array within the specified range. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(Object[] a, int fromIndex, int toIndex, jaroslav@557: Object key) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(Object[] a, int fromIndex, int toIndex, jaroslav@557: Object key) { jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: Comparable midVal = (Comparable)a[mid]; jaroslav@557: int cmp = midVal.compareTo(key); jaroslav@557: jaroslav@557: if (cmp < 0) jaroslav@557: low = mid + 1; jaroslav@557: else if (cmp > 0) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches the specified array for the specified object using the binary jaroslav@557: * search algorithm. The array must be sorted into ascending order jaroslav@557: * according to the specified comparator (as by the jaroslav@557: * {@link #sort(Object[], Comparator) sort(T[], Comparator)} jaroslav@557: * method) prior to making this call. If it is jaroslav@557: * not sorted, the results are undefined. jaroslav@557: * If the array contains multiple jaroslav@557: * elements equal to the specified object, there is no guarantee which one jaroslav@557: * will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @param c the comparator by which the array is ordered. A jaroslav@557: * null value indicates that the elements' jaroslav@557: * {@linkplain Comparable natural ordering} should be used. jaroslav@557: * @return index of the search key, if it is contained in the array; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element greater than the key, or a.length if all jaroslav@557: * elements in the array are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws ClassCastException if the array contains elements that are not jaroslav@557: * mutually comparable using the specified comparator, jaroslav@557: * or the search key is not comparable to the jaroslav@557: * elements of the array using this comparator. jaroslav@557: */ jaroslav@557: public static int binarySearch(T[] a, T key, Comparator c) { jaroslav@557: return binarySearch0(a, 0, a.length, key, c); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Searches a range of jaroslav@557: * the specified array for the specified object using the binary jaroslav@557: * search algorithm. jaroslav@557: * The range must be sorted into ascending order jaroslav@557: * according to the specified comparator (as by the jaroslav@557: * {@link #sort(Object[], int, int, Comparator) jaroslav@557: * sort(T[], int, int, Comparator)} jaroslav@557: * method) prior to making this call. jaroslav@557: * If it is not sorted, the results are undefined. jaroslav@557: * If the range contains multiple elements equal to the specified object, jaroslav@557: * there is no guarantee which one will be found. jaroslav@557: * jaroslav@557: * @param a the array to be searched jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * searched jaroslav@557: * @param toIndex the index of the last element (exclusive) to be searched jaroslav@557: * @param key the value to be searched for jaroslav@557: * @param c the comparator by which the array is ordered. A jaroslav@557: * null value indicates that the elements' jaroslav@557: * {@linkplain Comparable natural ordering} should be used. jaroslav@557: * @return index of the search key, if it is contained in the array jaroslav@557: * within the specified range; jaroslav@557: * otherwise, (-(insertion point) - 1). The jaroslav@557: * insertion point is defined as the point at which the jaroslav@557: * key would be inserted into the array: the index of the first jaroslav@557: * element in the range greater than the key, jaroslav@557: * or toIndex if all jaroslav@557: * elements in the range are less than the specified key. Note jaroslav@557: * that this guarantees that the return value will be >= 0 if jaroslav@557: * and only if the key is found. jaroslav@557: * @throws ClassCastException if the range contains elements that are not jaroslav@557: * mutually comparable using the specified comparator, jaroslav@557: * or the search key is not comparable to the jaroslav@557: * elements in the range using this comparator. jaroslav@557: * @throws IllegalArgumentException jaroslav@557: * if {@code fromIndex > toIndex} jaroslav@557: * @throws ArrayIndexOutOfBoundsException jaroslav@557: * if {@code fromIndex < 0 or toIndex > a.length} jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int binarySearch(T[] a, int fromIndex, int toIndex, jaroslav@557: T key, Comparator c) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key, c); jaroslav@557: } jaroslav@557: jaroslav@557: // Like public version, but without range checks. jaroslav@557: private static int binarySearch0(T[] a, int fromIndex, int toIndex, jaroslav@557: T key, Comparator c) { jaroslav@557: if (c == null) { jaroslav@557: return binarySearch0(a, fromIndex, toIndex, key); jaroslav@557: } jaroslav@557: int low = fromIndex; jaroslav@557: int high = toIndex - 1; jaroslav@557: jaroslav@557: while (low <= high) { jaroslav@557: int mid = (low + high) >>> 1; jaroslav@557: T midVal = a[mid]; jaroslav@557: int cmp = c.compare(midVal, key); jaroslav@557: if (cmp < 0) jaroslav@557: low = mid + 1; jaroslav@557: else if (cmp > 0) jaroslav@557: high = mid - 1; jaroslav@557: else jaroslav@557: return mid; // key found jaroslav@557: } jaroslav@557: return -(low + 1); // key not found. jaroslav@557: } jaroslav@557: jaroslav@557: // Equality Testing jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns true if the two specified arrays of longs are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(long[] a, long[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of ints are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(int[] a, int[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of shorts are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(short[] a, short a2[]) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of chars are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(char[] a, char[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of bytes are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(byte[] a, byte[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of booleans are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(boolean[] a, boolean[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of doubles are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * Two doubles d1 and d2 are considered equal if: jaroslav@557: *

    new Double(d1).equals(new Double(d2))
jaroslav@557: * (Unlike the == operator, this method considers jaroslav@557: * NaN equals to itself, and 0.0d unequal to -0.0d.) jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: * @see Double#equals(Object) jaroslav@557: */ jaroslav@557: public static boolean equals(double[] a, double[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of floats are jaroslav@557: * equal to one another. Two arrays are considered equal if both jaroslav@557: * arrays contain the same number of elements, and all corresponding pairs jaroslav@557: * of elements in the two arrays are equal. In other words, two arrays jaroslav@557: * are equal if they contain the same elements in the same order. Also, jaroslav@557: * two array references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * Two floats f1 and f2 are considered equal if: jaroslav@557: *

    new Float(f1).equals(new Float(f2))
jaroslav@557: * (Unlike the == operator, this method considers jaroslav@557: * NaN equals to itself, and 0.0f unequal to -0.0f.) jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: * @see Float#equals(Object) jaroslav@557: */ jaroslav@557: public static boolean equals(float[] a, float[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; itrue if the two specified arrays of Objects are jaroslav@557: * equal to one another. The two arrays are considered equal if jaroslav@557: * both arrays contain the same number of elements, and all corresponding jaroslav@557: * pairs of elements in the two arrays are equal. Two objects e1 jaroslav@557: * and e2 are considered equal if (e1==null ? e2==null jaroslav@557: * : e1.equals(e2)). In other words, the two arrays are equal if jaroslav@557: * they contain the same elements in the same order. Also, two array jaroslav@557: * references are considered equal if both are null.

jaroslav@557: * jaroslav@557: * @param a one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: */ jaroslav@557: public static boolean equals(Object[] a, Object[] a2) { jaroslav@557: if (a==a2) jaroslav@557: return true; jaroslav@557: if (a==null || a2==null) jaroslav@557: return false; jaroslav@557: jaroslav@557: int length = a.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i=0; ifromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(long[] a, int fromIndex, int toIndex, long val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified int value to each element of the specified array jaroslav@557: * of ints. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(int[] a, int val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified int value to each element of the specified jaroslav@557: * range of the specified array of ints. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(int[] a, int fromIndex, int toIndex, int val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified short value to each element of the specified array jaroslav@557: * of shorts. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(short[] a, short val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified short value to each element of the specified jaroslav@557: * range of the specified array of shorts. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(short[] a, int fromIndex, int toIndex, short val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified char value to each element of the specified array jaroslav@557: * of chars. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(char[] a, char val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified char value to each element of the specified jaroslav@557: * range of the specified array of chars. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(char[] a, int fromIndex, int toIndex, char val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified byte value to each element of the specified array jaroslav@557: * of bytes. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(byte[] a, byte val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified byte value to each element of the specified jaroslav@557: * range of the specified array of bytes. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified boolean value to each element of the specified jaroslav@557: * array of booleans. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(boolean[] a, boolean val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified boolean value to each element of the specified jaroslav@557: * range of the specified array of booleans. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(boolean[] a, int fromIndex, int toIndex, jaroslav@557: boolean val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified double value to each element of the specified jaroslav@557: * array of doubles. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(double[] a, double val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified double value to each element of the specified jaroslav@557: * range of the specified array of doubles. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(double[] a, int fromIndex, int toIndex,double val){ jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified float value to each element of the specified array jaroslav@557: * of floats. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: */ jaroslav@557: public static void fill(float[] a, float val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified float value to each element of the specified jaroslav@557: * range of the specified array of floats. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: */ jaroslav@557: public static void fill(float[] a, int fromIndex, int toIndex, float val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified Object reference to each element of the specified jaroslav@557: * array of Objects. jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws ArrayStoreException if the specified value is not of a jaroslav@557: * runtime type that can be stored in the specified array jaroslav@557: */ jaroslav@557: public static void fill(Object[] a, Object val) { jaroslav@557: for (int i = 0, len = a.length; i < len; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Assigns the specified Object reference to each element of the specified jaroslav@557: * range of the specified array of Objects. The range to be filled jaroslav@557: * extends from index fromIndex, inclusive, to index jaroslav@557: * toIndex, exclusive. (If fromIndex==toIndex, the jaroslav@557: * range to be filled is empty.) jaroslav@557: * jaroslav@557: * @param a the array to be filled jaroslav@557: * @param fromIndex the index of the first element (inclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param toIndex the index of the last element (exclusive) to be jaroslav@557: * filled with the specified value jaroslav@557: * @param val the value to be stored in all elements of the array jaroslav@557: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@557: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 or jaroslav@557: * toIndex > a.length jaroslav@557: * @throws ArrayStoreException if the specified value is not of a jaroslav@557: * runtime type that can be stored in the specified array jaroslav@557: */ jaroslav@557: public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { jaroslav@557: rangeCheck(a.length, fromIndex, toIndex); jaroslav@557: for (int i = fromIndex; i < toIndex; i++) jaroslav@557: a[i] = val; jaroslav@557: } jaroslav@557: jaroslav@557: // Cloning jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with nulls (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain null. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * The resulting array is of exactly the same class as the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with nulls jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static T[] copyOf(T[] original, int newLength) { jaroslav@557: return (T[]) copyOf(original, newLength, original.getClass()); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with nulls (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain null. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * The resulting array is of the class newType. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @param newType the class of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with nulls jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @throws ArrayStoreException if an element copied from jaroslav@557: * original is not of a runtime type that can be stored in jaroslav@557: * an array of class newType jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static T[] copyOf(U[] original, int newLength, Class newType) { jaroslav@557: T[] copy = ((Object)newType == (Object)Object[].class) jaroslav@557: ? (T[]) new Object[newLength] jaroslav@557: : (T[]) Array.newInstance(newType.getComponentType(), newLength); jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain (byte)0. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static byte[] copyOf(byte[] original, int newLength) { jaroslav@557: byte[] copy = new byte[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain (short)0. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static short[] copyOf(short[] original, int newLength) { jaroslav@557: short[] copy = new short[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain 0. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int[] copyOf(int[] original, int newLength) { jaroslav@557: int[] copy = new int[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain 0L. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static long[] copyOf(long[] original, int newLength) { jaroslav@557: long[] copy = new long[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with null characters (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are valid jaroslav@557: * in both the original array and the copy, the two arrays will contain jaroslav@557: * identical values. For any indices that are valid in the copy but not jaroslav@557: * the original, the copy will contain '\\u000'. Such indices jaroslav@557: * will exist if and only if the specified length is greater than that of jaroslav@557: * the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with null characters jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static char[] copyOf(char[] original, int newLength) { jaroslav@557: char[] copy = new char[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain 0f. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static float[] copyOf(float[] original, int newLength) { jaroslav@557: float[] copy = new float[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with zeros (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain 0d. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with zeros jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static double[] copyOf(double[] original, int newLength) { jaroslav@557: double[] copy = new double[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified array, truncating or padding with false (if necessary) jaroslav@557: * so the copy has the specified length. For all indices that are jaroslav@557: * valid in both the original array and the copy, the two arrays will jaroslav@557: * contain identical values. For any indices that are valid in the jaroslav@557: * copy but not the original, the copy will contain false. jaroslav@557: * Such indices will exist if and only if the specified length jaroslav@557: * is greater than that of the original array. jaroslav@557: * jaroslav@557: * @param original the array to be copied jaroslav@557: * @param newLength the length of the copy to be returned jaroslav@557: * @return a copy of the original array, truncated or padded with false elements jaroslav@557: * to obtain the specified length jaroslav@557: * @throws NegativeArraySizeException if newLength is negative jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static boolean[] copyOf(boolean[] original, int newLength) { jaroslav@557: boolean[] copy = new boolean[newLength]; jaroslav@557: System.arraycopy(original, 0, copy, 0, jaroslav@557: Math.min(original.length, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * null is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: *

jaroslav@557: * The resulting array is of exactly the same class as the original array. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with nulls to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static T[] copyOfRange(T[] original, int from, int to) { jaroslav@557: return copyOfRange(original, from, to, (Class) original.getClass()); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * null is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * The resulting array is of the class newType. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @param newType the class of the copy to be returned jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with nulls to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @throws ArrayStoreException if an element copied from jaroslav@557: * original is not of a runtime type that can be stored in jaroslav@557: * an array of class newType. jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static T[] copyOfRange(U[] original, int from, int to, Class newType) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: T[] copy = ((Object)newType == (Object)Object[].class) jaroslav@557: ? (T[]) new Object[newLength] jaroslav@557: : (T[]) Array.newInstance(newType.getComponentType(), newLength); jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * (byte)0 is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static byte[] copyOfRange(byte[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: byte[] copy = new byte[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * (short)0 is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static short[] copyOfRange(short[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: short[] copy = new short[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * 0 is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static int[] copyOfRange(int[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: int[] copy = new int[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * 0L is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static long[] copyOfRange(long[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: long[] copy = new long[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * '\\u000' is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with null characters to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static char[] copyOfRange(char[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: char[] copy = new char[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * 0f is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static float[] copyOfRange(float[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: float[] copy = new float[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * 0d is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with zeros to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static double[] copyOfRange(double[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: double[] copy = new double[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Copies the specified range of the specified array into a new array. jaroslav@557: * The initial index of the range (from) must lie between zero jaroslav@557: * and original.length, inclusive. The value at jaroslav@557: * original[from] is placed into the initial element of the copy jaroslav@557: * (unless from == original.length or from == to). jaroslav@557: * Values from subsequent elements in the original array are placed into jaroslav@557: * subsequent elements in the copy. The final index of the range jaroslav@557: * (to), which must be greater than or equal to from, jaroslav@557: * may be greater than original.length, in which case jaroslav@557: * false is placed in all elements of the copy whose index is jaroslav@557: * greater than or equal to original.length - from. The length jaroslav@557: * of the returned array will be to - from. jaroslav@557: * jaroslav@557: * @param original the array from which a range is to be copied jaroslav@557: * @param from the initial index of the range to be copied, inclusive jaroslav@557: * @param to the final index of the range to be copied, exclusive. jaroslav@557: * (This index may lie outside the array.) jaroslav@557: * @return a new array containing the specified range from the original array, jaroslav@557: * truncated or padded with false elements to obtain the required length jaroslav@557: * @throws ArrayIndexOutOfBoundsException if {@code from < 0} jaroslav@557: * or {@code from > original.length} jaroslav@557: * @throws IllegalArgumentException if from > to jaroslav@557: * @throws NullPointerException if original is null jaroslav@557: * @since 1.6 jaroslav@557: */ jaroslav@557: public static boolean[] copyOfRange(boolean[] original, int from, int to) { jaroslav@557: int newLength = to - from; jaroslav@557: if (newLength < 0) jaroslav@557: throw new IllegalArgumentException(from + " > " + to); jaroslav@557: boolean[] copy = new boolean[newLength]; jaroslav@557: System.arraycopy(original, from, copy, 0, jaroslav@557: Math.min(original.length - from, newLength)); jaroslav@557: return copy; jaroslav@557: } jaroslav@557: jaroslav@557: // Misc jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a fixed-size list backed by the specified array. (Changes to jaroslav@557: * the returned list "write through" to the array.) This method acts jaroslav@557: * as bridge between array-based and collection-based APIs, in jaroslav@557: * combination with {@link Collection#toArray}. The returned list is jaroslav@557: * serializable and implements {@link RandomAccess}. jaroslav@557: * jaroslav@557: *

This method also provides a convenient way to create a fixed-size jaroslav@557: * list initialized to contain several elements: jaroslav@557: *

jaroslav@557:      *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
jaroslav@557:      * 
jaroslav@557: * jaroslav@557: * @param a the array by which the list will be backed jaroslav@557: * @return a list view of the specified array jaroslav@557: */ jaroslav@557: @SafeVarargs jaroslav@557: public static List asList(T... a) { jaroslav@557: return new ArrayList<>(a); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * @serial include jaroslav@557: */ jaroslav@557: private static class ArrayList extends AbstractList jaroslav@557: implements RandomAccess, java.io.Serializable jaroslav@557: { jaroslav@557: private static final long serialVersionUID = -2764017481108945198L; jaroslav@557: private final E[] a; jaroslav@557: jaroslav@557: ArrayList(E[] array) { jaroslav@557: if (array==null) jaroslav@557: throw new NullPointerException(); jaroslav@557: a = array; jaroslav@557: } jaroslav@557: jaroslav@557: public int size() { jaroslav@557: return a.length; jaroslav@557: } jaroslav@557: jaroslav@557: public Object[] toArray() { jaroslav@557: return a.clone(); jaroslav@557: } jaroslav@557: jaroslav@557: public T[] toArray(T[] a) { jaroslav@557: int size = size(); jaroslav@557: if (a.length < size) jaroslav@557: return Arrays.copyOf(this.a, size, jaroslav@557: (Class) a.getClass()); jaroslav@557: System.arraycopy(this.a, 0, a, 0, size); jaroslav@557: if (a.length > size) jaroslav@557: a[size] = null; jaroslav@557: return a; jaroslav@557: } jaroslav@557: jaroslav@557: public E get(int index) { jaroslav@557: return a[index]; jaroslav@557: } jaroslav@557: jaroslav@557: public E set(int index, E element) { jaroslav@557: E oldValue = a[index]; jaroslav@557: a[index] = element; jaroslav@557: return oldValue; jaroslav@557: } jaroslav@557: jaroslav@557: public int indexOf(Object o) { jaroslav@557: if (o==null) { jaroslav@557: for (int i=0; ilong arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Long} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(long a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (long element : a) { jaroslav@557: int elementHash = (int)(element ^ (element >>> 32)); jaroslav@557: result = 31 * result + elementHash; jaroslav@557: } jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two non-null int arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Integer} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(int a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (int element : a) jaroslav@557: result = 31 * result + element; jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two short arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Short} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(short a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (short element : a) jaroslav@557: result = 31 * result + element; jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two char arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Character} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(char a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (char element : a) jaroslav@557: result = 31 * result + element; jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two byte arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Byte} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(byte a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (byte element : a) jaroslav@557: result = 31 * result + element; jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two boolean arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Boolean} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(boolean a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (boolean element : a) jaroslav@557: result = 31 * result + (element ? 1231 : 1237); jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two float arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Float} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(float a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (float element : a) jaroslav@557: result = 31 * result + Float.floatToIntBits(element); jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. jaroslav@557: * For any two double arrays a and b jaroslav@557: * such that Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is the same value that would be jaroslav@557: * obtained by invoking the {@link List#hashCode() hashCode} jaroslav@557: * method on a {@link List} containing a sequence of {@link Double} jaroslav@557: * instances representing the elements of a in the same order. jaroslav@557: * If a is null, this method returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose hash value to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(double a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: for (double element : a) { jaroslav@557: long bits = Double.doubleToLongBits(element); jaroslav@557: result = 31 * result + (int)(bits ^ (bits >>> 32)); jaroslav@557: } jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the contents of the specified array. If jaroslav@557: * the array contains other arrays as elements, the hash code is based on jaroslav@557: * their identities rather than their contents. It is therefore jaroslav@557: * acceptable to invoke this method on an array that contains itself as an jaroslav@557: * element, either directly or indirectly through one or more levels of jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

For any two arrays a and b such that jaroslav@557: * Arrays.equals(a, b), it is also the case that jaroslav@557: * Arrays.hashCode(a) == Arrays.hashCode(b). jaroslav@557: * jaroslav@557: *

The value returned by this method is equal to the value that would jaroslav@557: * be returned by Arrays.asList(a).hashCode(), unless a jaroslav@557: * is null, in which case 0 is returned. jaroslav@557: * jaroslav@557: * @param a the array whose content-based hash code to compute jaroslav@557: * @return a content-based hash code for a jaroslav@557: * @see #deepHashCode(Object[]) jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int hashCode(Object a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: jaroslav@557: for (Object element : a) jaroslav@557: result = 31 * result + (element == null ? 0 : element.hashCode()); jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a hash code based on the "deep contents" of the specified jaroslav@557: * array. If the array contains other arrays as elements, the jaroslav@557: * hash code is based on their contents and so on, ad infinitum. jaroslav@557: * It is therefore unacceptable to invoke this method on an array that jaroslav@557: * contains itself as an element, either directly or indirectly through jaroslav@557: * one or more levels of arrays. The behavior of such an invocation is jaroslav@557: * undefined. jaroslav@557: * jaroslav@557: *

For any two arrays a and b such that jaroslav@557: * Arrays.deepEquals(a, b), it is also the case that jaroslav@557: * Arrays.deepHashCode(a) == Arrays.deepHashCode(b). jaroslav@557: * jaroslav@557: *

The computation of the value returned by this method is similar to jaroslav@557: * that of the value returned by {@link List#hashCode()} on a list jaroslav@557: * containing the same elements as a in the same order, with one jaroslav@557: * difference: If an element e of a is itself an array, jaroslav@557: * its hash code is computed not by calling e.hashCode(), but as jaroslav@557: * by calling the appropriate overloading of Arrays.hashCode(e) jaroslav@557: * if e is an array of a primitive type, or as by calling jaroslav@557: * Arrays.deepHashCode(e) recursively if e is an array jaroslav@557: * of a reference type. If a is null, this method jaroslav@557: * returns 0. jaroslav@557: * jaroslav@557: * @param a the array whose deep-content-based hash code to compute jaroslav@557: * @return a deep-content-based hash code for a jaroslav@557: * @see #hashCode(Object[]) jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static int deepHashCode(Object a[]) { jaroslav@557: if (a == null) jaroslav@557: return 0; jaroslav@557: jaroslav@557: int result = 1; jaroslav@557: jaroslav@557: for (Object element : a) { jaroslav@557: int elementHash = 0; jaroslav@557: if (element instanceof Object[]) jaroslav@557: elementHash = deepHashCode((Object[]) element); jaroslav@557: else if (element instanceof byte[]) jaroslav@557: elementHash = hashCode((byte[]) element); jaroslav@557: else if (element instanceof short[]) jaroslav@557: elementHash = hashCode((short[]) element); jaroslav@557: else if (element instanceof int[]) jaroslav@557: elementHash = hashCode((int[]) element); jaroslav@557: else if (element instanceof long[]) jaroslav@557: elementHash = hashCode((long[]) element); jaroslav@557: else if (element instanceof char[]) jaroslav@557: elementHash = hashCode((char[]) element); jaroslav@557: else if (element instanceof float[]) jaroslav@557: elementHash = hashCode((float[]) element); jaroslav@557: else if (element instanceof double[]) jaroslav@557: elementHash = hashCode((double[]) element); jaroslav@557: else if (element instanceof boolean[]) jaroslav@557: elementHash = hashCode((boolean[]) element); jaroslav@557: else if (element != null) jaroslav@557: elementHash = element.hashCode(); jaroslav@557: jaroslav@557: result = 31 * result + elementHash; jaroslav@557: } jaroslav@557: jaroslav@557: return result; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns true if the two specified arrays are deeply jaroslav@557: * equal to one another. Unlike the {@link #equals(Object[],Object[])} jaroslav@557: * method, this method is appropriate for use with nested arrays of jaroslav@557: * arbitrary depth. jaroslav@557: * jaroslav@557: *

Two array references are considered deeply equal if both jaroslav@557: * are null, or if they refer to arrays that contain the same jaroslav@557: * number of elements and all corresponding pairs of elements in the two jaroslav@557: * arrays are deeply equal. jaroslav@557: * jaroslav@557: *

Two possibly null elements e1 and e2 are jaroslav@557: * deeply equal if any of the following conditions hold: jaroslav@557: *

    jaroslav@557: *
  • e1 and e2 are both arrays of object reference jaroslav@557: * types, and Arrays.deepEquals(e1, e2) would return true jaroslav@557: *
  • e1 and e2 are arrays of the same primitive jaroslav@557: * type, and the appropriate overloading of jaroslav@557: * Arrays.equals(e1, e2) would return true. jaroslav@557: *
  • e1 == e2 jaroslav@557: *
  • e1.equals(e2) would return true. jaroslav@557: *
jaroslav@557: * Note that this definition permits null elements at any depth. jaroslav@557: * jaroslav@557: *

If either of the specified arrays contain themselves as elements jaroslav@557: * either directly or indirectly through one or more levels of arrays, jaroslav@557: * the behavior of this method is undefined. jaroslav@557: * jaroslav@557: * @param a1 one array to be tested for equality jaroslav@557: * @param a2 the other array to be tested for equality jaroslav@557: * @return true if the two arrays are equal jaroslav@557: * @see #equals(Object[],Object[]) jaroslav@557: * @see Objects#deepEquals(Object, Object) jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static boolean deepEquals(Object[] a1, Object[] a2) { jaroslav@557: if (a1 == a2) jaroslav@557: return true; jaroslav@557: if (a1 == null || a2==null) jaroslav@557: return false; jaroslav@557: int length = a1.length; jaroslav@557: if (a2.length != length) jaroslav@557: return false; jaroslav@557: jaroslav@557: for (int i = 0; i < length; i++) { jaroslav@557: Object e1 = a1[i]; jaroslav@557: Object e2 = a2[i]; jaroslav@557: jaroslav@557: if (e1 == e2) jaroslav@557: continue; jaroslav@557: if (e1 == null) jaroslav@557: return false; jaroslav@557: jaroslav@557: // Figure out whether the two elements are equal jaroslav@557: boolean eq = deepEquals0(e1, e2); jaroslav@557: jaroslav@557: if (!eq) jaroslav@557: return false; jaroslav@557: } jaroslav@557: return true; jaroslav@557: } jaroslav@557: jaroslav@557: static boolean deepEquals0(Object e1, Object e2) { jaroslav@557: assert e1 != null; jaroslav@557: boolean eq; jaroslav@557: if (e1 instanceof Object[] && e2 instanceof Object[]) jaroslav@557: eq = deepEquals ((Object[]) e1, (Object[]) e2); jaroslav@557: else if (e1 instanceof byte[] && e2 instanceof byte[]) jaroslav@557: eq = equals((byte[]) e1, (byte[]) e2); jaroslav@557: else if (e1 instanceof short[] && e2 instanceof short[]) jaroslav@557: eq = equals((short[]) e1, (short[]) e2); jaroslav@557: else if (e1 instanceof int[] && e2 instanceof int[]) jaroslav@557: eq = equals((int[]) e1, (int[]) e2); jaroslav@557: else if (e1 instanceof long[] && e2 instanceof long[]) jaroslav@557: eq = equals((long[]) e1, (long[]) e2); jaroslav@557: else if (e1 instanceof char[] && e2 instanceof char[]) jaroslav@557: eq = equals((char[]) e1, (char[]) e2); jaroslav@557: else if (e1 instanceof float[] && e2 instanceof float[]) jaroslav@557: eq = equals((float[]) e1, (float[]) e2); jaroslav@557: else if (e1 instanceof double[] && e2 instanceof double[]) jaroslav@557: eq = equals((double[]) e1, (double[]) e2); jaroslav@557: else if (e1 instanceof boolean[] && e2 instanceof boolean[]) jaroslav@557: eq = equals((boolean[]) e1, (boolean[]) e2); jaroslav@557: else jaroslav@557: eq = e1.equals(e2); jaroslav@557: return eq; jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(long). Returns "null" if a jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(long[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(int). Returns "null" if a is jaroslav@557: * null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(int[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(short). Returns "null" if a jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(short[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(char). Returns "null" if a jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(char[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements jaroslav@557: * are separated by the characters ", " (a comma followed jaroslav@557: * by a space). Elements are converted to strings as by jaroslav@557: * String.valueOf(byte). Returns "null" if jaroslav@557: * a is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(byte[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(boolean). Returns "null" if jaroslav@557: * a is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(boolean[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(float). Returns "null" if a jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(float[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * The string representation consists of a list of the array's elements, jaroslav@557: * enclosed in square brackets ("[]"). Adjacent elements are jaroslav@557: * separated by the characters ", " (a comma followed by a jaroslav@557: * space). Elements are converted to strings as by jaroslav@557: * String.valueOf(double). Returns "null" if a jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(double[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(a[i]); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the contents of the specified array. jaroslav@557: * If the array contains other arrays as elements, they are converted to jaroslav@557: * strings by the {@link Object#toString} method inherited from jaroslav@557: * Object, which describes their identities rather than jaroslav@557: * their contents. jaroslav@557: * jaroslav@557: *

The value returned by this method is equal to the value that would jaroslav@557: * be returned by Arrays.asList(a).toString(), unless a jaroslav@557: * is null, in which case "null" is returned. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @see #deepToString(Object[]) jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String toString(Object[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) jaroslav@557: return "[]"; jaroslav@557: jaroslav@557: StringBuilder b = new StringBuilder(); jaroslav@557: b.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: b.append(String.valueOf(a[i])); jaroslav@557: if (i == iMax) jaroslav@557: return b.append(']').toString(); jaroslav@557: b.append(", "); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Returns a string representation of the "deep contents" of the specified jaroslav@557: * array. If the array contains other arrays as elements, the string jaroslav@557: * representation contains their contents and so on. This method is jaroslav@557: * designed for converting multidimensional arrays to strings. jaroslav@557: * jaroslav@557: *

The string representation consists of a list of the array's jaroslav@557: * elements, enclosed in square brackets ("[]"). Adjacent jaroslav@557: * elements are separated by the characters ", " (a comma jaroslav@557: * followed by a space). Elements are converted to strings as by jaroslav@557: * String.valueOf(Object), unless they are themselves jaroslav@557: * arrays. jaroslav@557: * jaroslav@557: *

If an element e is an array of a primitive type, it is jaroslav@557: * converted to a string as by invoking the appropriate overloading of jaroslav@557: * Arrays.toString(e). If an element e is an array of a jaroslav@557: * reference type, it is converted to a string as by invoking jaroslav@557: * this method recursively. jaroslav@557: * jaroslav@557: *

To avoid infinite recursion, if the specified array contains itself jaroslav@557: * as an element, or contains an indirect reference to itself through one jaroslav@557: * or more levels of arrays, the self-reference is converted to the string jaroslav@557: * "[...]". For example, an array containing only a reference jaroslav@557: * to itself would be rendered as "[[...]]". jaroslav@557: * jaroslav@557: *

This method returns "null" if the specified array jaroslav@557: * is null. jaroslav@557: * jaroslav@557: * @param a the array whose string representation to return jaroslav@557: * @return a string representation of a jaroslav@557: * @see #toString(Object[]) jaroslav@557: * @since 1.5 jaroslav@557: */ jaroslav@557: public static String deepToString(Object[] a) { jaroslav@557: if (a == null) jaroslav@557: return "null"; jaroslav@557: jaroslav@557: int bufLen = 20 * a.length; jaroslav@557: if (a.length != 0 && bufLen <= 0) jaroslav@557: bufLen = Integer.MAX_VALUE; jaroslav@557: StringBuilder buf = new StringBuilder(bufLen); jaroslav@557: deepToString(a, buf, new HashSet()); jaroslav@557: return buf.toString(); jaroslav@557: } jaroslav@557: jaroslav@557: private static void deepToString(Object[] a, StringBuilder buf, jaroslav@557: Set dejaVu) { jaroslav@557: if (a == null) { jaroslav@557: buf.append("null"); jaroslav@557: return; jaroslav@557: } jaroslav@557: int iMax = a.length - 1; jaroslav@557: if (iMax == -1) { jaroslav@557: buf.append("[]"); jaroslav@557: return; jaroslav@557: } jaroslav@557: jaroslav@557: dejaVu.add(a); jaroslav@557: buf.append('['); jaroslav@557: for (int i = 0; ; i++) { jaroslav@557: jaroslav@557: Object element = a[i]; jaroslav@557: if (element == null) { jaroslav@557: buf.append("null"); jaroslav@557: } else { jaroslav@557: Class eClass = element.getClass(); jaroslav@557: jaroslav@557: if (eClass.isArray()) { jaroslav@557: if (eClass == byte[].class) jaroslav@557: buf.append(toString((byte[]) element)); jaroslav@557: else if (eClass == short[].class) jaroslav@557: buf.append(toString((short[]) element)); jaroslav@557: else if (eClass == int[].class) jaroslav@557: buf.append(toString((int[]) element)); jaroslav@557: else if (eClass == long[].class) jaroslav@557: buf.append(toString((long[]) element)); jaroslav@557: else if (eClass == char[].class) jaroslav@557: buf.append(toString((char[]) element)); jaroslav@557: else if (eClass == float[].class) jaroslav@557: buf.append(toString((float[]) element)); jaroslav@557: else if (eClass == double[].class) jaroslav@557: buf.append(toString((double[]) element)); jaroslav@557: else if (eClass == boolean[].class) jaroslav@557: buf.append(toString((boolean[]) element)); jaroslav@557: else { // element is an array of object references jaroslav@557: if (dejaVu.contains(element)) jaroslav@557: buf.append("[...]"); jaroslav@557: else jaroslav@557: deepToString((Object[])element, buf, dejaVu); jaroslav@557: } jaroslav@557: } else { // element is non-null and not an array jaroslav@557: buf.append(element.toString()); jaroslav@557: } jaroslav@557: } jaroslav@557: if (i == iMax) jaroslav@557: break; jaroslav@557: buf.append(", "); jaroslav@557: } jaroslav@557: buf.append(']'); jaroslav@557: dejaVu.remove(a); jaroslav@557: } jaroslav@557: }