2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.lang.reflect.*;
29 import org.apidesign.bck2brwsr.emul.lang.System;
32 * This class contains various methods for manipulating arrays (such as
33 * sorting and searching). This class also contains a static factory
34 * that allows arrays to be viewed as lists.
36 * <p>The methods in this class all throw a {@code NullPointerException},
37 * if the specified array reference is null, except where noted.
39 * <p>The documentation for the methods contained in this class includes
40 * briefs description of the <i>implementations</i>. Such descriptions should
41 * be regarded as <i>implementation notes</i>, rather than parts of the
42 * <i>specification</i>. Implementors should feel free to substitute other
43 * algorithms, so long as the specification itself is adhered to. (For
44 * example, the algorithm used by {@code sort(Object[])} does not have to be
45 * a MergeSort, but it does have to be <i>stable</i>.)
47 * <p>This class is a member of the
48 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
49 * Java Collections Framework</a>.
58 // Suppresses default constructor, ensuring non-instantiability.
62 * Sorting of primitive type arrays.
66 * Sorts the specified array into ascending numerical order.
68 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
69 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
70 * offers O(n log(n)) performance on many data sets that cause other
71 * quicksorts to degrade to quadratic performance, and is typically
72 * faster than traditional (one-pivot) Quicksort implementations.
74 * @param a the array to be sorted
76 public static void sort(int[] a) {
77 DualPivotQuicksort.sort(a);
81 * Sorts the specified range of the array into ascending order. The range
82 * to be sorted extends from the index {@code fromIndex}, inclusive, to
83 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
84 * the range to be sorted is empty.
86 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
87 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
88 * offers O(n log(n)) performance on many data sets that cause other
89 * quicksorts to degrade to quadratic performance, and is typically
90 * faster than traditional (one-pivot) Quicksort implementations.
92 * @param a the array to be sorted
93 * @param fromIndex the index of the first element, inclusive, to be sorted
94 * @param toIndex the index of the last element, exclusive, to be sorted
96 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
97 * @throws ArrayIndexOutOfBoundsException
98 * if {@code fromIndex < 0} or {@code toIndex > a.length}
100 public static void sort(int[] a, int fromIndex, int toIndex) {
101 rangeCheck(a.length, fromIndex, toIndex);
102 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
106 * Sorts the specified array into ascending numerical order.
108 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
109 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
110 * offers O(n log(n)) performance on many data sets that cause other
111 * quicksorts to degrade to quadratic performance, and is typically
112 * faster than traditional (one-pivot) Quicksort implementations.
114 * @param a the array to be sorted
116 public static void sort(long[] a) {
117 DualPivotQuicksort.sort(a);
121 * Sorts the specified range of the array into ascending order. The range
122 * to be sorted extends from the index {@code fromIndex}, inclusive, to
123 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
124 * the range to be sorted is empty.
126 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
127 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
128 * offers O(n log(n)) performance on many data sets that cause other
129 * quicksorts to degrade to quadratic performance, and is typically
130 * faster than traditional (one-pivot) Quicksort implementations.
132 * @param a the array to be sorted
133 * @param fromIndex the index of the first element, inclusive, to be sorted
134 * @param toIndex the index of the last element, exclusive, to be sorted
136 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
137 * @throws ArrayIndexOutOfBoundsException
138 * if {@code fromIndex < 0} or {@code toIndex > a.length}
140 public static void sort(long[] a, int fromIndex, int toIndex) {
141 rangeCheck(a.length, fromIndex, toIndex);
142 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
146 * Sorts the specified array into ascending numerical order.
148 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
149 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
150 * offers O(n log(n)) performance on many data sets that cause other
151 * quicksorts to degrade to quadratic performance, and is typically
152 * faster than traditional (one-pivot) Quicksort implementations.
154 * @param a the array to be sorted
156 public static void sort(short[] a) {
157 DualPivotQuicksort.sort(a);
161 * Sorts the specified range of the array into ascending order. The range
162 * to be sorted extends from the index {@code fromIndex}, inclusive, to
163 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
164 * the range to be sorted is empty.
166 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
167 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
168 * offers O(n log(n)) performance on many data sets that cause other
169 * quicksorts to degrade to quadratic performance, and is typically
170 * faster than traditional (one-pivot) Quicksort implementations.
172 * @param a the array to be sorted
173 * @param fromIndex the index of the first element, inclusive, to be sorted
174 * @param toIndex the index of the last element, exclusive, to be sorted
176 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
177 * @throws ArrayIndexOutOfBoundsException
178 * if {@code fromIndex < 0} or {@code toIndex > a.length}
180 public static void sort(short[] a, int fromIndex, int toIndex) {
181 rangeCheck(a.length, fromIndex, toIndex);
182 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
186 * Sorts the specified array into ascending numerical order.
188 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
189 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
190 * offers O(n log(n)) performance on many data sets that cause other
191 * quicksorts to degrade to quadratic performance, and is typically
192 * faster than traditional (one-pivot) Quicksort implementations.
194 * @param a the array to be sorted
196 public static void sort(char[] a) {
197 DualPivotQuicksort.sort(a);
201 * Sorts the specified range of the array into ascending order. The range
202 * to be sorted extends from the index {@code fromIndex}, inclusive, to
203 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
204 * the range to be sorted is empty.
206 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
207 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
208 * offers O(n log(n)) performance on many data sets that cause other
209 * quicksorts to degrade to quadratic performance, and is typically
210 * faster than traditional (one-pivot) Quicksort implementations.
212 * @param a the array to be sorted
213 * @param fromIndex the index of the first element, inclusive, to be sorted
214 * @param toIndex the index of the last element, exclusive, to be sorted
216 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
217 * @throws ArrayIndexOutOfBoundsException
218 * if {@code fromIndex < 0} or {@code toIndex > a.length}
220 public static void sort(char[] a, int fromIndex, int toIndex) {
221 rangeCheck(a.length, fromIndex, toIndex);
222 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
226 * Sorts the specified array into ascending numerical order.
228 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
229 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
230 * offers O(n log(n)) performance on many data sets that cause other
231 * quicksorts to degrade to quadratic performance, and is typically
232 * faster than traditional (one-pivot) Quicksort implementations.
234 * @param a the array to be sorted
236 public static void sort(byte[] a) {
237 DualPivotQuicksort.sort(a);
241 * Sorts the specified range of the array into ascending order. The range
242 * to be sorted extends from the index {@code fromIndex}, inclusive, to
243 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
244 * the range to be sorted is empty.
246 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
247 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
248 * offers O(n log(n)) performance on many data sets that cause other
249 * quicksorts to degrade to quadratic performance, and is typically
250 * faster than traditional (one-pivot) Quicksort implementations.
252 * @param a the array to be sorted
253 * @param fromIndex the index of the first element, inclusive, to be sorted
254 * @param toIndex the index of the last element, exclusive, to be sorted
256 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
257 * @throws ArrayIndexOutOfBoundsException
258 * if {@code fromIndex < 0} or {@code toIndex > a.length}
260 public static void sort(byte[] a, int fromIndex, int toIndex) {
261 rangeCheck(a.length, fromIndex, toIndex);
262 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
266 * Sorts the specified array into ascending numerical order.
268 * <p>The {@code <} relation does not provide a total order on all float
269 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
270 * value compares neither less than, greater than, nor equal to any value,
271 * even itself. This method uses the total order imposed by the method
272 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
273 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
274 * other value and all {@code Float.NaN} values are considered equal.
276 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
277 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
278 * offers O(n log(n)) performance on many data sets that cause other
279 * quicksorts to degrade to quadratic performance, and is typically
280 * faster than traditional (one-pivot) Quicksort implementations.
282 * @param a the array to be sorted
284 public static void sort(float[] a) {
285 DualPivotQuicksort.sort(a);
289 * Sorts the specified range of the array into ascending order. The range
290 * to be sorted extends from the index {@code fromIndex}, inclusive, to
291 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
292 * the range to be sorted is empty.
294 * <p>The {@code <} relation does not provide a total order on all float
295 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
296 * value compares neither less than, greater than, nor equal to any value,
297 * even itself. This method uses the total order imposed by the method
298 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
299 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
300 * other value and all {@code Float.NaN} values are considered equal.
302 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
303 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
304 * offers O(n log(n)) performance on many data sets that cause other
305 * quicksorts to degrade to quadratic performance, and is typically
306 * faster than traditional (one-pivot) Quicksort implementations.
308 * @param a the array to be sorted
309 * @param fromIndex the index of the first element, inclusive, to be sorted
310 * @param toIndex the index of the last element, exclusive, to be sorted
312 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
313 * @throws ArrayIndexOutOfBoundsException
314 * if {@code fromIndex < 0} or {@code toIndex > a.length}
316 public static void sort(float[] a, int fromIndex, int toIndex) {
317 rangeCheck(a.length, fromIndex, toIndex);
318 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
322 * Sorts the specified array into ascending numerical order.
324 * <p>The {@code <} relation does not provide a total order on all double
325 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
326 * value compares neither less than, greater than, nor equal to any value,
327 * even itself. This method uses the total order imposed by the method
328 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
329 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
330 * other value and all {@code Double.NaN} values are considered equal.
332 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
333 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
334 * offers O(n log(n)) performance on many data sets that cause other
335 * quicksorts to degrade to quadratic performance, and is typically
336 * faster than traditional (one-pivot) Quicksort implementations.
338 * @param a the array to be sorted
340 public static void sort(double[] a) {
341 DualPivotQuicksort.sort(a);
345 * Sorts the specified range of the array into ascending order. The range
346 * to be sorted extends from the index {@code fromIndex}, inclusive, to
347 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
348 * the range to be sorted is empty.
350 * <p>The {@code <} relation does not provide a total order on all double
351 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
352 * value compares neither less than, greater than, nor equal to any value,
353 * even itself. This method uses the total order imposed by the method
354 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
355 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
356 * other value and all {@code Double.NaN} values are considered equal.
358 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
359 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
360 * offers O(n log(n)) performance on many data sets that cause other
361 * quicksorts to degrade to quadratic performance, and is typically
362 * faster than traditional (one-pivot) Quicksort implementations.
364 * @param a the array to be sorted
365 * @param fromIndex the index of the first element, inclusive, to be sorted
366 * @param toIndex the index of the last element, exclusive, to be sorted
368 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
369 * @throws ArrayIndexOutOfBoundsException
370 * if {@code fromIndex < 0} or {@code toIndex > a.length}
372 public static void sort(double[] a, int fromIndex, int toIndex) {
373 rangeCheck(a.length, fromIndex, toIndex);
374 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
378 * Sorting of complex type arrays.
382 * Old merge sort implementation can be selected (for
383 * compatibility with broken comparators) using a system property.
384 * Cannot be a static boolean in the enclosing class due to
385 * circular dependencies. To be removed in a future release.
387 static final class LegacyMergeSort {
388 private static final boolean userRequested =
389 java.security.AccessController.doPrivileged(
390 new sun.security.action.GetBooleanAction(
391 "java.util.Arrays.useLegacyMergeSort")).booleanValue();
395 * If this platform has an optimizing VM, check whether ComparableTimSort
396 * offers any performance benefit over TimSort in conjunction with a
397 * comparator that returns:
398 * {@code ((Comparable)first).compareTo(Second)}.
399 * If not, you are better off deleting ComparableTimSort to
400 * eliminate the code duplication. In other words, the commented
401 * out code below is the preferable implementation for sorting
402 * arrays of Comparables if it offers sufficient performance.
406 // * A comparator that implements the natural ordering of a group of
407 // * mutually comparable elements. Using this comparator saves us
408 // * from duplicating most of the code in this file (one version for
409 // * Comparables, one for explicit Comparators).
411 // private static final Comparator<Object> NATURAL_ORDER =
412 // new Comparator<Object>() {
413 // @SuppressWarnings("unchecked")
414 // public int compare(Object first, Object second) {
415 // return ((Comparable<Object>)first).compareTo(second);
419 // public static void sort(Object[] a) {
420 // sort(a, 0, a.length, NATURAL_ORDER);
423 // public static void sort(Object[] a, int fromIndex, int toIndex) {
424 // sort(a, fromIndex, toIndex, NATURAL_ORDER);
428 * Sorts the specified array of objects into ascending order, according
429 * to the {@linkplain Comparable natural ordering} of its elements.
430 * All elements in the array must implement the {@link Comparable}
431 * interface. Furthermore, all elements in the array must be
432 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
433 * not throw a {@code ClassCastException} for any elements {@code e1}
434 * and {@code e2} in the array).
436 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
437 * not be reordered as a result of the sort.
439 * <p>Implementation note: This implementation is a stable, adaptive,
440 * iterative mergesort that requires far fewer than n lg(n) comparisons
441 * when the input array is partially sorted, while offering the
442 * performance of a traditional mergesort when the input array is
443 * randomly ordered. If the input array is nearly sorted, the
444 * implementation requires approximately n comparisons. Temporary
445 * storage requirements vary from a small constant for nearly sorted
446 * input arrays to n/2 object references for randomly ordered input
449 * <p>The implementation takes equal advantage of ascending and
450 * descending order in its input array, and can take advantage of
451 * ascending and descending order in different parts of the the same
452 * input array. It is well-suited to merging two or more sorted arrays:
453 * simply concatenate the arrays and sort the resulting array.
455 * <p>The implementation was adapted from Tim Peters's list sort for Python
456 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
457 * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
458 * Sorting and Information Theoretic Complexity", in Proceedings of the
459 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
462 * @param a the array to be sorted
463 * @throws ClassCastException if the array contains elements that are not
464 * <i>mutually comparable</i> (for example, strings and integers)
465 * @throws IllegalArgumentException (optional) if the natural
466 * ordering of the array elements is found to violate the
467 * {@link Comparable} contract
469 public static void sort(Object[] a) {
470 if (LegacyMergeSort.userRequested)
473 ComparableTimSort.sort(a);
476 /** To be removed in a future release. */
477 private static void legacyMergeSort(Object[] a) {
478 Object[] aux = a.clone();
479 mergeSort(aux, a, 0, a.length, 0);
483 * Sorts the specified range of the specified array of objects into
484 * ascending order, according to the
485 * {@linkplain Comparable natural ordering} of its
486 * elements. The range to be sorted extends from index
487 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
488 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All
489 * elements in this range must implement the {@link Comparable}
490 * interface. Furthermore, all elements in this range must be <i>mutually
491 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
492 * {@code ClassCastException} for any elements {@code e1} and
493 * {@code e2} in the array).
495 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
496 * not be reordered as a result of the sort.
498 * <p>Implementation note: This implementation is a stable, adaptive,
499 * iterative mergesort that requires far fewer than n lg(n) comparisons
500 * when the input array is partially sorted, while offering the
501 * performance of a traditional mergesort when the input array is
502 * randomly ordered. If the input array is nearly sorted, the
503 * implementation requires approximately n comparisons. Temporary
504 * storage requirements vary from a small constant for nearly sorted
505 * input arrays to n/2 object references for randomly ordered input
508 * <p>The implementation takes equal advantage of ascending and
509 * descending order in its input array, and can take advantage of
510 * ascending and descending order in different parts of the the same
511 * input array. It is well-suited to merging two or more sorted arrays:
512 * simply concatenate the arrays and sort the resulting array.
514 * <p>The implementation was adapted from Tim Peters's list sort for Python
515 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
516 * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
517 * Sorting and Information Theoretic Complexity", in Proceedings of the
518 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
521 * @param a the array to be sorted
522 * @param fromIndex the index of the first element (inclusive) to be
524 * @param toIndex the index of the last element (exclusive) to be sorted
525 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
526 * (optional) if the natural ordering of the array elements is
527 * found to violate the {@link Comparable} contract
528 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
529 * {@code toIndex > a.length}
530 * @throws ClassCastException if the array contains elements that are
531 * not <i>mutually comparable</i> (for example, strings and
534 public static void sort(Object[] a, int fromIndex, int toIndex) {
535 if (LegacyMergeSort.userRequested)
536 legacyMergeSort(a, fromIndex, toIndex);
538 ComparableTimSort.sort(a, fromIndex, toIndex);
541 /** To be removed in a future release. */
542 private static void legacyMergeSort(Object[] a,
543 int fromIndex, int toIndex) {
544 rangeCheck(a.length, fromIndex, toIndex);
545 Object[] aux = copyOfRange(a, fromIndex, toIndex);
546 mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
550 * Tuning parameter: list size at or below which insertion sort will be
551 * used in preference to mergesort.
552 * To be removed in a future release.
554 private static final int INSERTIONSORT_THRESHOLD = 7;
557 * Src is the source array that starts at index 0
558 * Dest is the (possibly larger) array destination with a possible offset
559 * low is the index in dest to start sorting
560 * high is the end index in dest to end sorting
561 * off is the offset to generate corresponding low, high in src
562 * To be removed in a future release.
564 private static void mergeSort(Object[] src,
569 int length = high - low;
571 // Insertion sort on smallest arrays
572 if (length < INSERTIONSORT_THRESHOLD) {
573 for (int i=low; i<high; i++)
574 for (int j=i; j>low &&
575 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
580 // Recursively sort halves of dest into src
585 int mid = (low + high) >>> 1;
586 mergeSort(dest, src, low, mid, -off);
587 mergeSort(dest, src, mid, high, -off);
589 // If list is already sorted, just copy from src to dest. This is an
590 // optimization that results in faster sorts for nearly ordered lists.
591 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
592 System.arraycopy(src, low, dest, destLow, length);
596 // Merge sorted halves (now in src) into dest
597 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
598 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
606 * Swaps x[a] with x[b].
608 private static void swap(Object[] x, int a, int b) {
615 * Sorts the specified array of objects according to the order induced by
616 * the specified comparator. All elements in the array must be
617 * <i>mutually comparable</i> by the specified comparator (that is,
618 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
619 * for any elements {@code e1} and {@code e2} in the array).
621 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
622 * not be reordered as a result of the sort.
624 * <p>Implementation note: This implementation is a stable, adaptive,
625 * iterative mergesort that requires far fewer than n lg(n) comparisons
626 * when the input array is partially sorted, while offering the
627 * performance of a traditional mergesort when the input array is
628 * randomly ordered. If the input array is nearly sorted, the
629 * implementation requires approximately n comparisons. Temporary
630 * storage requirements vary from a small constant for nearly sorted
631 * input arrays to n/2 object references for randomly ordered input
634 * <p>The implementation takes equal advantage of ascending and
635 * descending order in its input array, and can take advantage of
636 * ascending and descending order in different parts of the the same
637 * input array. It is well-suited to merging two or more sorted arrays:
638 * simply concatenate the arrays and sort the resulting array.
640 * <p>The implementation was adapted from Tim Peters's list sort for Python
641 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
642 * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
643 * Sorting and Information Theoretic Complexity", in Proceedings of the
644 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
647 * @param a the array to be sorted
648 * @param c the comparator to determine the order of the array. A
649 * {@code null} value indicates that the elements'
650 * {@linkplain Comparable natural ordering} should be used.
651 * @throws ClassCastException if the array contains elements that are
652 * not <i>mutually comparable</i> using the specified comparator
653 * @throws IllegalArgumentException (optional) if the comparator is
654 * found to violate the {@link Comparator} contract
656 public static <T> void sort(T[] a, Comparator<? super T> c) {
657 if (LegacyMergeSort.userRequested)
658 legacyMergeSort(a, c);
663 /** To be removed in a future release. */
664 private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
667 mergeSort(aux, a, 0, a.length, 0);
669 mergeSort(aux, a, 0, a.length, 0, c);
673 * Sorts the specified range of the specified array of objects according
674 * to the order induced by the specified comparator. The range to be
675 * sorted extends from index {@code fromIndex}, inclusive, to index
676 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
677 * range to be sorted is empty.) All elements in the range must be
678 * <i>mutually comparable</i> by the specified comparator (that is,
679 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
680 * for any elements {@code e1} and {@code e2} in the range).
682 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
683 * not be reordered as a result of the sort.
685 * <p>Implementation note: This implementation is a stable, adaptive,
686 * iterative mergesort that requires far fewer than n lg(n) comparisons
687 * when the input array is partially sorted, while offering the
688 * performance of a traditional mergesort when the input array is
689 * randomly ordered. If the input array is nearly sorted, the
690 * implementation requires approximately n comparisons. Temporary
691 * storage requirements vary from a small constant for nearly sorted
692 * input arrays to n/2 object references for randomly ordered input
695 * <p>The implementation takes equal advantage of ascending and
696 * descending order in its input array, and can take advantage of
697 * ascending and descending order in different parts of the the same
698 * input array. It is well-suited to merging two or more sorted arrays:
699 * simply concatenate the arrays and sort the resulting array.
701 * <p>The implementation was adapted from Tim Peters's list sort for Python
702 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
703 * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
704 * Sorting and Information Theoretic Complexity", in Proceedings of the
705 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
708 * @param a the array to be sorted
709 * @param fromIndex the index of the first element (inclusive) to be
711 * @param toIndex the index of the last element (exclusive) to be sorted
712 * @param c the comparator to determine the order of the array. A
713 * {@code null} value indicates that the elements'
714 * {@linkplain Comparable natural ordering} should be used.
715 * @throws ClassCastException if the array contains elements that are not
716 * <i>mutually comparable</i> using the specified comparator.
717 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
718 * (optional) if the comparator is found to violate the
719 * {@link Comparator} contract
720 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
721 * {@code toIndex > a.length}
723 public static <T> void sort(T[] a, int fromIndex, int toIndex,
724 Comparator<? super T> c) {
725 if (LegacyMergeSort.userRequested)
726 legacyMergeSort(a, fromIndex, toIndex, c);
728 TimSort.sort(a, fromIndex, toIndex, c);
731 /** To be removed in a future release. */
732 private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
733 Comparator<? super T> c) {
734 rangeCheck(a.length, fromIndex, toIndex);
735 T[] aux = copyOfRange(a, fromIndex, toIndex);
737 mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
739 mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
743 * Src is the source array that starts at index 0
744 * Dest is the (possibly larger) array destination with a possible offset
745 * low is the index in dest to start sorting
746 * high is the end index in dest to end sorting
747 * off is the offset into src corresponding to low in dest
748 * To be removed in a future release.
750 private static void mergeSort(Object[] src,
752 int low, int high, int off,
754 int length = high - low;
756 // Insertion sort on smallest arrays
757 if (length < INSERTIONSORT_THRESHOLD) {
758 for (int i=low; i<high; i++)
759 for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
764 // Recursively sort halves of dest into src
769 int mid = (low + high) >>> 1;
770 mergeSort(dest, src, low, mid, -off, c);
771 mergeSort(dest, src, mid, high, -off, c);
773 // If list is already sorted, just copy from src to dest. This is an
774 // optimization that results in faster sorts for nearly ordered lists.
775 if (c.compare(src[mid-1], src[mid]) <= 0) {
776 System.arraycopy(src, low, dest, destLow, length);
780 // Merge sorted halves (now in src) into dest
781 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
782 if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
790 * Checks that {@code fromIndex} and {@code toIndex} are in
791 * the range and throws an appropriate exception, if they aren't.
793 private static void rangeCheck(int length, int fromIndex, int toIndex) {
794 if (fromIndex > toIndex) {
795 throw new IllegalArgumentException(
796 "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
799 throw new ArrayIndexOutOfBoundsException(fromIndex);
801 if (toIndex > length) {
802 throw new ArrayIndexOutOfBoundsException(toIndex);
809 * Searches the specified array of longs for the specified value using the
810 * binary search algorithm. The array must be sorted (as
811 * by the {@link #sort(long[])} method) prior to making this call. If it
812 * is not sorted, the results are undefined. If the array contains
813 * multiple elements with the specified value, there is no guarantee which
816 * @param a the array to be searched
817 * @param key the value to be searched for
818 * @return index of the search key, if it is contained in the array;
819 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
820 * <i>insertion point</i> is defined as the point at which the
821 * key would be inserted into the array: the index of the first
822 * element greater than the key, or <tt>a.length</tt> if all
823 * elements in the array are less than the specified key. Note
824 * that this guarantees that the return value will be >= 0 if
825 * and only if the key is found.
827 public static int binarySearch(long[] a, long key) {
828 return binarySearch0(a, 0, a.length, key);
832 * Searches a range of
833 * the specified array of longs for the specified value using the
834 * binary search algorithm.
835 * The range must be sorted (as
836 * by the {@link #sort(long[], int, int)} method)
837 * prior to making this call. If it
838 * is not sorted, the results are undefined. If the range contains
839 * multiple elements with the specified value, there is no guarantee which
842 * @param a the array to be searched
843 * @param fromIndex the index of the first element (inclusive) to be
845 * @param toIndex the index of the last element (exclusive) to be searched
846 * @param key the value to be searched for
847 * @return index of the search key, if it is contained in the array
848 * within the specified range;
849 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
850 * <i>insertion point</i> is defined as the point at which the
851 * key would be inserted into the array: the index of the first
852 * element in the range greater than the key,
853 * or <tt>toIndex</tt> if all
854 * elements in the range are less than the specified key. Note
855 * that this guarantees that the return value will be >= 0 if
856 * and only if the key is found.
857 * @throws IllegalArgumentException
858 * if {@code fromIndex > toIndex}
859 * @throws ArrayIndexOutOfBoundsException
860 * if {@code fromIndex < 0 or toIndex > a.length}
863 public static int binarySearch(long[] a, int fromIndex, int toIndex,
865 rangeCheck(a.length, fromIndex, toIndex);
866 return binarySearch0(a, fromIndex, toIndex, key);
869 // Like public version, but without range checks.
870 private static int binarySearch0(long[] a, int fromIndex, int toIndex,
873 int high = toIndex - 1;
875 while (low <= high) {
876 int mid = (low + high) >>> 1;
877 long midVal = a[mid];
881 else if (midVal > key)
884 return mid; // key found
886 return -(low + 1); // key not found.
890 * Searches the specified array of ints for the specified value using the
891 * binary search algorithm. The array must be sorted (as
892 * by the {@link #sort(int[])} method) prior to making this call. If it
893 * is not sorted, the results are undefined. If the array contains
894 * multiple elements with the specified value, there is no guarantee which
897 * @param a the array to be searched
898 * @param key the value to be searched for
899 * @return index of the search key, if it is contained in the array;
900 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
901 * <i>insertion point</i> is defined as the point at which the
902 * key would be inserted into the array: the index of the first
903 * element greater than the key, or <tt>a.length</tt> if all
904 * elements in the array are less than the specified key. Note
905 * that this guarantees that the return value will be >= 0 if
906 * and only if the key is found.
908 public static int binarySearch(int[] a, int key) {
909 return binarySearch0(a, 0, a.length, key);
913 * Searches a range of
914 * the specified array of ints for the specified value using the
915 * binary search algorithm.
916 * The range must be sorted (as
917 * by the {@link #sort(int[], int, int)} method)
918 * prior to making this call. If it
919 * is not sorted, the results are undefined. If the range contains
920 * multiple elements with the specified value, there is no guarantee which
923 * @param a the array to be searched
924 * @param fromIndex the index of the first element (inclusive) to be
926 * @param toIndex the index of the last element (exclusive) to be searched
927 * @param key the value to be searched for
928 * @return index of the search key, if it is contained in the array
929 * within the specified range;
930 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
931 * <i>insertion point</i> is defined as the point at which the
932 * key would be inserted into the array: the index of the first
933 * element in the range greater than the key,
934 * or <tt>toIndex</tt> if all
935 * elements in the range are less than the specified key. Note
936 * that this guarantees that the return value will be >= 0 if
937 * and only if the key is found.
938 * @throws IllegalArgumentException
939 * if {@code fromIndex > toIndex}
940 * @throws ArrayIndexOutOfBoundsException
941 * if {@code fromIndex < 0 or toIndex > a.length}
944 public static int binarySearch(int[] a, int fromIndex, int toIndex,
946 rangeCheck(a.length, fromIndex, toIndex);
947 return binarySearch0(a, fromIndex, toIndex, key);
950 // Like public version, but without range checks.
951 private static int binarySearch0(int[] a, int fromIndex, int toIndex,
954 int high = toIndex - 1;
956 while (low <= high) {
957 int mid = (low + high) >>> 1;
962 else if (midVal > key)
965 return mid; // key found
967 return -(low + 1); // key not found.
971 * Searches the specified array of shorts for the specified value using
972 * the binary search algorithm. The array must be sorted
973 * (as by the {@link #sort(short[])} method) prior to making this call. If
974 * it is not sorted, the results are undefined. If the array contains
975 * multiple elements with the specified value, there is no guarantee which
978 * @param a the array to be searched
979 * @param key the value to be searched for
980 * @return index of the search key, if it is contained in the array;
981 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
982 * <i>insertion point</i> is defined as the point at which the
983 * key would be inserted into the array: the index of the first
984 * element greater than the key, or <tt>a.length</tt> if all
985 * elements in the array are less than the specified key. Note
986 * that this guarantees that the return value will be >= 0 if
987 * and only if the key is found.
989 public static int binarySearch(short[] a, short key) {
990 return binarySearch0(a, 0, a.length, key);
994 * Searches a range of
995 * the specified array of shorts for the specified value using
996 * the binary search algorithm.
997 * The range must be sorted
998 * (as by the {@link #sort(short[], int, int)} method)
999 * prior to making this call. If
1000 * it is not sorted, the results are undefined. If the range contains
1001 * multiple elements with the specified value, there is no guarantee which
1002 * one will be found.
1004 * @param a the array to be searched
1005 * @param fromIndex the index of the first element (inclusive) to be
1007 * @param toIndex the index of the last element (exclusive) to be searched
1008 * @param key the value to be searched for
1009 * @return index of the search key, if it is contained in the array
1010 * within the specified range;
1011 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1012 * <i>insertion point</i> is defined as the point at which the
1013 * key would be inserted into the array: the index of the first
1014 * element in the range greater than the key,
1015 * or <tt>toIndex</tt> if all
1016 * elements in the range are less than the specified key. Note
1017 * that this guarantees that the return value will be >= 0 if
1018 * and only if the key is found.
1019 * @throws IllegalArgumentException
1020 * if {@code fromIndex > toIndex}
1021 * @throws ArrayIndexOutOfBoundsException
1022 * if {@code fromIndex < 0 or toIndex > a.length}
1025 public static int binarySearch(short[] a, int fromIndex, int toIndex,
1027 rangeCheck(a.length, fromIndex, toIndex);
1028 return binarySearch0(a, fromIndex, toIndex, key);
1031 // Like public version, but without range checks.
1032 private static int binarySearch0(short[] a, int fromIndex, int toIndex,
1034 int low = fromIndex;
1035 int high = toIndex - 1;
1037 while (low <= high) {
1038 int mid = (low + high) >>> 1;
1039 short midVal = a[mid];
1043 else if (midVal > key)
1046 return mid; // key found
1048 return -(low + 1); // key not found.
1052 * Searches the specified array of chars for the specified value using the
1053 * binary search algorithm. The array must be sorted (as
1054 * by the {@link #sort(char[])} method) prior to making this call. If it
1055 * is not sorted, the results are undefined. If the array contains
1056 * multiple elements with the specified value, there is no guarantee which
1057 * one will be found.
1059 * @param a the array to be searched
1060 * @param key the value to be searched for
1061 * @return index of the search key, if it is contained in the array;
1062 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1063 * <i>insertion point</i> is defined as the point at which the
1064 * key would be inserted into the array: the index of the first
1065 * element greater than the key, or <tt>a.length</tt> if all
1066 * elements in the array are less than the specified key. Note
1067 * that this guarantees that the return value will be >= 0 if
1068 * and only if the key is found.
1070 public static int binarySearch(char[] a, char key) {
1071 return binarySearch0(a, 0, a.length, key);
1075 * Searches a range of
1076 * the specified array of chars for the specified value using the
1077 * binary search algorithm.
1078 * The range must be sorted (as
1079 * by the {@link #sort(char[], int, int)} method)
1080 * prior to making this call. If it
1081 * is not sorted, the results are undefined. If the range contains
1082 * multiple elements with the specified value, there is no guarantee which
1083 * one will be found.
1085 * @param a the array to be searched
1086 * @param fromIndex the index of the first element (inclusive) to be
1088 * @param toIndex the index of the last element (exclusive) to be searched
1089 * @param key the value to be searched for
1090 * @return index of the search key, if it is contained in the array
1091 * within the specified range;
1092 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1093 * <i>insertion point</i> is defined as the point at which the
1094 * key would be inserted into the array: the index of the first
1095 * element in the range greater than the key,
1096 * or <tt>toIndex</tt> if all
1097 * elements in the range are less than the specified key. Note
1098 * that this guarantees that the return value will be >= 0 if
1099 * and only if the key is found.
1100 * @throws IllegalArgumentException
1101 * if {@code fromIndex > toIndex}
1102 * @throws ArrayIndexOutOfBoundsException
1103 * if {@code fromIndex < 0 or toIndex > a.length}
1106 public static int binarySearch(char[] a, int fromIndex, int toIndex,
1108 rangeCheck(a.length, fromIndex, toIndex);
1109 return binarySearch0(a, fromIndex, toIndex, key);
1112 // Like public version, but without range checks.
1113 private static int binarySearch0(char[] a, int fromIndex, int toIndex,
1115 int low = fromIndex;
1116 int high = toIndex - 1;
1118 while (low <= high) {
1119 int mid = (low + high) >>> 1;
1120 char midVal = a[mid];
1124 else if (midVal > key)
1127 return mid; // key found
1129 return -(low + 1); // key not found.
1133 * Searches the specified array of bytes for the specified value using the
1134 * binary search algorithm. The array must be sorted (as
1135 * by the {@link #sort(byte[])} method) prior to making this call. If it
1136 * is not sorted, the results are undefined. If the array contains
1137 * multiple elements with the specified value, there is no guarantee which
1138 * one will be found.
1140 * @param a the array to be searched
1141 * @param key the value to be searched for
1142 * @return index of the search key, if it is contained in the array;
1143 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1144 * <i>insertion point</i> is defined as the point at which the
1145 * key would be inserted into the array: the index of the first
1146 * element greater than the key, or <tt>a.length</tt> if all
1147 * elements in the array are less than the specified key. Note
1148 * that this guarantees that the return value will be >= 0 if
1149 * and only if the key is found.
1151 public static int binarySearch(byte[] a, byte key) {
1152 return binarySearch0(a, 0, a.length, key);
1156 * Searches a range of
1157 * the specified array of bytes for the specified value using the
1158 * binary search algorithm.
1159 * The range must be sorted (as
1160 * by the {@link #sort(byte[], int, int)} method)
1161 * prior to making this call. If it
1162 * is not sorted, the results are undefined. If the range contains
1163 * multiple elements with the specified value, there is no guarantee which
1164 * one will be found.
1166 * @param a the array to be searched
1167 * @param fromIndex the index of the first element (inclusive) to be
1169 * @param toIndex the index of the last element (exclusive) to be searched
1170 * @param key the value to be searched for
1171 * @return index of the search key, if it is contained in the array
1172 * within the specified range;
1173 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1174 * <i>insertion point</i> is defined as the point at which the
1175 * key would be inserted into the array: the index of the first
1176 * element in the range greater than the key,
1177 * or <tt>toIndex</tt> if all
1178 * elements in the range are less than the specified key. Note
1179 * that this guarantees that the return value will be >= 0 if
1180 * and only if the key is found.
1181 * @throws IllegalArgumentException
1182 * if {@code fromIndex > toIndex}
1183 * @throws ArrayIndexOutOfBoundsException
1184 * if {@code fromIndex < 0 or toIndex > a.length}
1187 public static int binarySearch(byte[] a, int fromIndex, int toIndex,
1189 rangeCheck(a.length, fromIndex, toIndex);
1190 return binarySearch0(a, fromIndex, toIndex, key);
1193 // Like public version, but without range checks.
1194 private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
1196 int low = fromIndex;
1197 int high = toIndex - 1;
1199 while (low <= high) {
1200 int mid = (low + high) >>> 1;
1201 byte midVal = a[mid];
1205 else if (midVal > key)
1208 return mid; // key found
1210 return -(low + 1); // key not found.
1214 * Searches the specified array of doubles for the specified value using
1215 * the binary search algorithm. The array must be sorted
1216 * (as by the {@link #sort(double[])} method) prior to making this call.
1217 * If it is not sorted, the results are undefined. If the array contains
1218 * multiple elements with the specified value, there is no guarantee which
1219 * one will be found. This method considers all NaN values to be
1220 * equivalent and equal.
1222 * @param a the array to be searched
1223 * @param key the value to be searched for
1224 * @return index of the search key, if it is contained in the array;
1225 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1226 * <i>insertion point</i> is defined as the point at which the
1227 * key would be inserted into the array: the index of the first
1228 * element greater than the key, or <tt>a.length</tt> if all
1229 * elements in the array are less than the specified key. Note
1230 * that this guarantees that the return value will be >= 0 if
1231 * and only if the key is found.
1233 public static int binarySearch(double[] a, double key) {
1234 return binarySearch0(a, 0, a.length, key);
1238 * Searches a range of
1239 * the specified array of doubles for the specified value using
1240 * the binary search algorithm.
1241 * The range must be sorted
1242 * (as by the {@link #sort(double[], int, int)} method)
1243 * prior to making this call.
1244 * If it is not sorted, the results are undefined. If the range contains
1245 * multiple elements with the specified value, there is no guarantee which
1246 * one will be found. This method considers all NaN values to be
1247 * equivalent and equal.
1249 * @param a the array to be searched
1250 * @param fromIndex the index of the first element (inclusive) to be
1252 * @param toIndex the index of the last element (exclusive) to be searched
1253 * @param key the value to be searched for
1254 * @return index of the search key, if it is contained in the array
1255 * within the specified range;
1256 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1257 * <i>insertion point</i> is defined as the point at which the
1258 * key would be inserted into the array: the index of the first
1259 * element in the range greater than the key,
1260 * or <tt>toIndex</tt> if all
1261 * elements in the range are less than the specified key. Note
1262 * that this guarantees that the return value will be >= 0 if
1263 * and only if the key is found.
1264 * @throws IllegalArgumentException
1265 * if {@code fromIndex > toIndex}
1266 * @throws ArrayIndexOutOfBoundsException
1267 * if {@code fromIndex < 0 or toIndex > a.length}
1270 public static int binarySearch(double[] a, int fromIndex, int toIndex,
1272 rangeCheck(a.length, fromIndex, toIndex);
1273 return binarySearch0(a, fromIndex, toIndex, key);
1276 // Like public version, but without range checks.
1277 private static int binarySearch0(double[] a, int fromIndex, int toIndex,
1279 int low = fromIndex;
1280 int high = toIndex - 1;
1282 while (low <= high) {
1283 int mid = (low + high) >>> 1;
1284 double midVal = a[mid];
1287 low = mid + 1; // Neither val is NaN, thisVal is smaller
1288 else if (midVal > key)
1289 high = mid - 1; // Neither val is NaN, thisVal is larger
1291 long midBits = Double.doubleToLongBits(midVal);
1292 long keyBits = Double.doubleToLongBits(key);
1293 if (midBits == keyBits) // Values are equal
1294 return mid; // Key found
1295 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
1297 else // (0.0, -0.0) or (NaN, !NaN)
1301 return -(low + 1); // key not found.
1305 * Searches the specified array of floats for the specified value using
1306 * the binary search algorithm. The array must be sorted
1307 * (as by the {@link #sort(float[])} method) prior to making this call. If
1308 * it is not sorted, the results are undefined. If the array contains
1309 * multiple elements with the specified value, there is no guarantee which
1310 * one will be found. This method considers all NaN values to be
1311 * equivalent and equal.
1313 * @param a the array to be searched
1314 * @param key the value to be searched for
1315 * @return index of the search key, if it is contained in the array;
1316 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1317 * <i>insertion point</i> is defined as the point at which the
1318 * key would be inserted into the array: the index of the first
1319 * element greater than the key, or <tt>a.length</tt> if all
1320 * elements in the array are less than the specified key. Note
1321 * that this guarantees that the return value will be >= 0 if
1322 * and only if the key is found.
1324 public static int binarySearch(float[] a, float key) {
1325 return binarySearch0(a, 0, a.length, key);
1329 * Searches a range of
1330 * the specified array of floats for the specified value using
1331 * the binary search algorithm.
1332 * The range must be sorted
1333 * (as by the {@link #sort(float[], int, int)} method)
1334 * prior to making this call. If
1335 * it is not sorted, the results are undefined. If the range contains
1336 * multiple elements with the specified value, there is no guarantee which
1337 * one will be found. This method considers all NaN values to be
1338 * equivalent and equal.
1340 * @param a the array to be searched
1341 * @param fromIndex the index of the first element (inclusive) to be
1343 * @param toIndex the index of the last element (exclusive) to be searched
1344 * @param key the value to be searched for
1345 * @return index of the search key, if it is contained in the array
1346 * within the specified range;
1347 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1348 * <i>insertion point</i> is defined as the point at which the
1349 * key would be inserted into the array: the index of the first
1350 * element in the range greater than the key,
1351 * or <tt>toIndex</tt> if all
1352 * elements in the range are less than the specified key. Note
1353 * that this guarantees that the return value will be >= 0 if
1354 * and only if the key is found.
1355 * @throws IllegalArgumentException
1356 * if {@code fromIndex > toIndex}
1357 * @throws ArrayIndexOutOfBoundsException
1358 * if {@code fromIndex < 0 or toIndex > a.length}
1361 public static int binarySearch(float[] a, int fromIndex, int toIndex,
1363 rangeCheck(a.length, fromIndex, toIndex);
1364 return binarySearch0(a, fromIndex, toIndex, key);
1367 // Like public version, but without range checks.
1368 private static int binarySearch0(float[] a, int fromIndex, int toIndex,
1370 int low = fromIndex;
1371 int high = toIndex - 1;
1373 while (low <= high) {
1374 int mid = (low + high) >>> 1;
1375 float midVal = a[mid];
1378 low = mid + 1; // Neither val is NaN, thisVal is smaller
1379 else if (midVal > key)
1380 high = mid - 1; // Neither val is NaN, thisVal is larger
1382 int midBits = Float.floatToIntBits(midVal);
1383 int keyBits = Float.floatToIntBits(key);
1384 if (midBits == keyBits) // Values are equal
1385 return mid; // Key found
1386 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
1388 else // (0.0, -0.0) or (NaN, !NaN)
1392 return -(low + 1); // key not found.
1396 * Searches the specified array for the specified object using the binary
1397 * search algorithm. The array must be sorted into ascending order
1399 * {@linkplain Comparable natural ordering}
1400 * of its elements (as by the
1401 * {@link #sort(Object[])} method) prior to making this call.
1402 * If it is not sorted, the results are undefined.
1403 * (If the array contains elements that are not mutually comparable (for
1404 * example, strings and integers), it <i>cannot</i> be sorted according
1405 * to the natural ordering of its elements, hence results are undefined.)
1406 * If the array contains multiple
1407 * elements equal to the specified object, there is no guarantee which
1408 * one will be found.
1410 * @param a the array to be searched
1411 * @param key the value to be searched for
1412 * @return index of the search key, if it is contained in the array;
1413 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1414 * <i>insertion point</i> is defined as the point at which the
1415 * key would be inserted into the array: the index of the first
1416 * element greater than the key, or <tt>a.length</tt> if all
1417 * elements in the array are less than the specified key. Note
1418 * that this guarantees that the return value will be >= 0 if
1419 * and only if the key is found.
1420 * @throws ClassCastException if the search key is not comparable to the
1421 * elements of the array.
1423 public static int binarySearch(Object[] a, Object key) {
1424 return binarySearch0(a, 0, a.length, key);
1428 * Searches a range of
1429 * the specified array for the specified object using the binary
1431 * The range must be sorted into ascending order
1433 * {@linkplain Comparable natural ordering}
1434 * of its elements (as by the
1435 * {@link #sort(Object[], int, int)} method) prior to making this
1436 * call. If it is not sorted, the results are undefined.
1437 * (If the range contains elements that are not mutually comparable (for
1438 * example, strings and integers), it <i>cannot</i> be sorted according
1439 * to the natural ordering of its elements, hence results are undefined.)
1440 * If the range contains multiple
1441 * elements equal to the specified object, there is no guarantee which
1442 * one will be found.
1444 * @param a the array to be searched
1445 * @param fromIndex the index of the first element (inclusive) to be
1447 * @param toIndex the index of the last element (exclusive) to be searched
1448 * @param key the value to be searched for
1449 * @return index of the search key, if it is contained in the array
1450 * within the specified range;
1451 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1452 * <i>insertion point</i> is defined as the point at which the
1453 * key would be inserted into the array: the index of the first
1454 * element in the range greater than the key,
1455 * or <tt>toIndex</tt> if all
1456 * elements in the range are less than the specified key. Note
1457 * that this guarantees that the return value will be >= 0 if
1458 * and only if the key is found.
1459 * @throws ClassCastException if the search key is not comparable to the
1460 * elements of the array within the specified range.
1461 * @throws IllegalArgumentException
1462 * if {@code fromIndex > toIndex}
1463 * @throws ArrayIndexOutOfBoundsException
1464 * if {@code fromIndex < 0 or toIndex > a.length}
1467 public static int binarySearch(Object[] a, int fromIndex, int toIndex,
1469 rangeCheck(a.length, fromIndex, toIndex);
1470 return binarySearch0(a, fromIndex, toIndex, key);
1473 // Like public version, but without range checks.
1474 private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
1476 int low = fromIndex;
1477 int high = toIndex - 1;
1479 while (low <= high) {
1480 int mid = (low + high) >>> 1;
1481 Comparable midVal = (Comparable)a[mid];
1482 int cmp = midVal.compareTo(key);
1489 return mid; // key found
1491 return -(low + 1); // key not found.
1495 * Searches the specified array for the specified object using the binary
1496 * search algorithm. The array must be sorted into ascending order
1497 * according to the specified comparator (as by the
1498 * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
1499 * method) prior to making this call. If it is
1500 * not sorted, the results are undefined.
1501 * If the array contains multiple
1502 * elements equal to the specified object, there is no guarantee which one
1505 * @param a the array to be searched
1506 * @param key the value to be searched for
1507 * @param c the comparator by which the array is ordered. A
1508 * <tt>null</tt> value indicates that the elements'
1509 * {@linkplain Comparable natural ordering} should be used.
1510 * @return index of the search key, if it is contained in the array;
1511 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1512 * <i>insertion point</i> is defined as the point at which the
1513 * key would be inserted into the array: the index of the first
1514 * element greater than the key, or <tt>a.length</tt> if all
1515 * elements in the array are less than the specified key. Note
1516 * that this guarantees that the return value will be >= 0 if
1517 * and only if the key is found.
1518 * @throws ClassCastException if the array contains elements that are not
1519 * <i>mutually comparable</i> using the specified comparator,
1520 * or the search key is not comparable to the
1521 * elements of the array using this comparator.
1523 public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
1524 return binarySearch0(a, 0, a.length, key, c);
1528 * Searches a range of
1529 * the specified array for the specified object using the binary
1531 * The range must be sorted into ascending order
1532 * according to the specified comparator (as by the
1533 * {@link #sort(Object[], int, int, Comparator)
1534 * sort(T[], int, int, Comparator)}
1535 * method) prior to making this call.
1536 * If it is not sorted, the results are undefined.
1537 * If the range contains multiple elements equal to the specified object,
1538 * there is no guarantee which one will be found.
1540 * @param a the array to be searched
1541 * @param fromIndex the index of the first element (inclusive) to be
1543 * @param toIndex the index of the last element (exclusive) to be searched
1544 * @param key the value to be searched for
1545 * @param c the comparator by which the array is ordered. A
1546 * <tt>null</tt> value indicates that the elements'
1547 * {@linkplain Comparable natural ordering} should be used.
1548 * @return index of the search key, if it is contained in the array
1549 * within the specified range;
1550 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
1551 * <i>insertion point</i> is defined as the point at which the
1552 * key would be inserted into the array: the index of the first
1553 * element in the range greater than the key,
1554 * or <tt>toIndex</tt> if all
1555 * elements in the range are less than the specified key. Note
1556 * that this guarantees that the return value will be >= 0 if
1557 * and only if the key is found.
1558 * @throws ClassCastException if the range contains elements that are not
1559 * <i>mutually comparable</i> using the specified comparator,
1560 * or the search key is not comparable to the
1561 * elements in the range using this comparator.
1562 * @throws IllegalArgumentException
1563 * if {@code fromIndex > toIndex}
1564 * @throws ArrayIndexOutOfBoundsException
1565 * if {@code fromIndex < 0 or toIndex > a.length}
1568 public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
1569 T key, Comparator<? super T> c) {
1570 rangeCheck(a.length, fromIndex, toIndex);
1571 return binarySearch0(a, fromIndex, toIndex, key, c);
1574 // Like public version, but without range checks.
1575 private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
1576 T key, Comparator<? super T> c) {
1578 return binarySearch0(a, fromIndex, toIndex, key);
1580 int low = fromIndex;
1581 int high = toIndex - 1;
1583 while (low <= high) {
1584 int mid = (low + high) >>> 1;
1586 int cmp = c.compare(midVal, key);
1592 return mid; // key found
1594 return -(low + 1); // key not found.
1600 * Returns <tt>true</tt> if the two specified arrays of longs are
1601 * <i>equal</i> to one another. Two arrays are considered equal if both
1602 * arrays contain the same number of elements, and all corresponding pairs
1603 * of elements in the two arrays are equal. In other words, two arrays
1604 * are equal if they contain the same elements in the same order. Also,
1605 * two array references are considered equal if both are <tt>null</tt>.<p>
1607 * @param a one array to be tested for equality
1608 * @param a2 the other array to be tested for equality
1609 * @return <tt>true</tt> if the two arrays are equal
1611 public static boolean equals(long[] a, long[] a2) {
1614 if (a==null || a2==null)
1617 int length = a.length;
1618 if (a2.length != length)
1621 for (int i=0; i<length; i++)
1629 * Returns <tt>true</tt> if the two specified arrays of ints are
1630 * <i>equal</i> to one another. Two arrays are considered equal if both
1631 * arrays contain the same number of elements, and all corresponding pairs
1632 * of elements in the two arrays are equal. In other words, two arrays
1633 * are equal if they contain the same elements in the same order. Also,
1634 * two array references are considered equal if both are <tt>null</tt>.<p>
1636 * @param a one array to be tested for equality
1637 * @param a2 the other array to be tested for equality
1638 * @return <tt>true</tt> if the two arrays are equal
1640 public static boolean equals(int[] a, int[] a2) {
1643 if (a==null || a2==null)
1646 int length = a.length;
1647 if (a2.length != length)
1650 for (int i=0; i<length; i++)
1658 * Returns <tt>true</tt> if the two specified arrays of shorts are
1659 * <i>equal</i> to one another. Two arrays are considered equal if both
1660 * arrays contain the same number of elements, and all corresponding pairs
1661 * of elements in the two arrays are equal. In other words, two arrays
1662 * are equal if they contain the same elements in the same order. Also,
1663 * two array references are considered equal if both are <tt>null</tt>.<p>
1665 * @param a one array to be tested for equality
1666 * @param a2 the other array to be tested for equality
1667 * @return <tt>true</tt> if the two arrays are equal
1669 public static boolean equals(short[] a, short a2[]) {
1672 if (a==null || a2==null)
1675 int length = a.length;
1676 if (a2.length != length)
1679 for (int i=0; i<length; i++)
1687 * Returns <tt>true</tt> if the two specified arrays of chars are
1688 * <i>equal</i> to one another. Two arrays are considered equal if both
1689 * arrays contain the same number of elements, and all corresponding pairs
1690 * of elements in the two arrays are equal. In other words, two arrays
1691 * are equal if they contain the same elements in the same order. Also,
1692 * two array references are considered equal if both are <tt>null</tt>.<p>
1694 * @param a one array to be tested for equality
1695 * @param a2 the other array to be tested for equality
1696 * @return <tt>true</tt> if the two arrays are equal
1698 public static boolean equals(char[] a, char[] a2) {
1701 if (a==null || a2==null)
1704 int length = a.length;
1705 if (a2.length != length)
1708 for (int i=0; i<length; i++)
1716 * Returns <tt>true</tt> if the two specified arrays of bytes are
1717 * <i>equal</i> to one another. Two arrays are considered equal if both
1718 * arrays contain the same number of elements, and all corresponding pairs
1719 * of elements in the two arrays are equal. In other words, two arrays
1720 * are equal if they contain the same elements in the same order. Also,
1721 * two array references are considered equal if both are <tt>null</tt>.<p>
1723 * @param a one array to be tested for equality
1724 * @param a2 the other array to be tested for equality
1725 * @return <tt>true</tt> if the two arrays are equal
1727 public static boolean equals(byte[] a, byte[] a2) {
1730 if (a==null || a2==null)
1733 int length = a.length;
1734 if (a2.length != length)
1737 for (int i=0; i<length; i++)
1745 * Returns <tt>true</tt> if the two specified arrays of booleans are
1746 * <i>equal</i> to one another. Two arrays are considered equal if both
1747 * arrays contain the same number of elements, and all corresponding pairs
1748 * of elements in the two arrays are equal. In other words, two arrays
1749 * are equal if they contain the same elements in the same order. Also,
1750 * two array references are considered equal if both are <tt>null</tt>.<p>
1752 * @param a one array to be tested for equality
1753 * @param a2 the other array to be tested for equality
1754 * @return <tt>true</tt> if the two arrays are equal
1756 public static boolean equals(boolean[] a, boolean[] a2) {
1759 if (a==null || a2==null)
1762 int length = a.length;
1763 if (a2.length != length)
1766 for (int i=0; i<length; i++)
1774 * Returns <tt>true</tt> if the two specified arrays of doubles are
1775 * <i>equal</i> to one another. Two arrays are considered equal if both
1776 * arrays contain the same number of elements, and all corresponding pairs
1777 * of elements in the two arrays are equal. In other words, two arrays
1778 * are equal if they contain the same elements in the same order. Also,
1779 * two array references are considered equal if both are <tt>null</tt>.<p>
1781 * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if:
1782 * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre>
1783 * (Unlike the <tt>==</tt> operator, this method considers
1784 * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.)
1786 * @param a one array to be tested for equality
1787 * @param a2 the other array to be tested for equality
1788 * @return <tt>true</tt> if the two arrays are equal
1789 * @see Double#equals(Object)
1791 public static boolean equals(double[] a, double[] a2) {
1794 if (a==null || a2==null)
1797 int length = a.length;
1798 if (a2.length != length)
1801 for (int i=0; i<length; i++)
1802 if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i]))
1809 * Returns <tt>true</tt> if the two specified arrays of floats are
1810 * <i>equal</i> to one another. Two arrays are considered equal if both
1811 * arrays contain the same number of elements, and all corresponding pairs
1812 * of elements in the two arrays are equal. In other words, two arrays
1813 * are equal if they contain the same elements in the same order. Also,
1814 * two array references are considered equal if both are <tt>null</tt>.<p>
1816 * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if:
1817 * <pre> <tt>new Float(f1).equals(new Float(f2))</tt></pre>
1818 * (Unlike the <tt>==</tt> operator, this method considers
1819 * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.)
1821 * @param a one array to be tested for equality
1822 * @param a2 the other array to be tested for equality
1823 * @return <tt>true</tt> if the two arrays are equal
1824 * @see Float#equals(Object)
1826 public static boolean equals(float[] a, float[] a2) {
1829 if (a==null || a2==null)
1832 int length = a.length;
1833 if (a2.length != length)
1836 for (int i=0; i<length; i++)
1837 if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i]))
1844 * Returns <tt>true</tt> if the two specified arrays of Objects are
1845 * <i>equal</i> to one another. The two arrays are considered equal if
1846 * both arrays contain the same number of elements, and all corresponding
1847 * pairs of elements in the two arrays are equal. Two objects <tt>e1</tt>
1848 * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null
1849 * : e1.equals(e2))</tt>. In other words, the two arrays are equal if
1850 * they contain the same elements in the same order. Also, two array
1851 * references are considered equal if both are <tt>null</tt>.<p>
1853 * @param a one array to be tested for equality
1854 * @param a2 the other array to be tested for equality
1855 * @return <tt>true</tt> if the two arrays are equal
1857 public static boolean equals(Object[] a, Object[] a2) {
1860 if (a==null || a2==null)
1863 int length = a.length;
1864 if (a2.length != length)
1867 for (int i=0; i<length; i++) {
1870 if (!(o1==null ? o2==null : o1.equals(o2)))
1880 * Assigns the specified long value to each element of the specified array
1883 * @param a the array to be filled
1884 * @param val the value to be stored in all elements of the array
1886 public static void fill(long[] a, long val) {
1887 for (int i = 0, len = a.length; i < len; i++)
1892 * Assigns the specified long value to each element of the specified
1893 * range of the specified array of longs. The range to be filled
1894 * extends from index <tt>fromIndex</tt>, inclusive, to index
1895 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
1896 * range to be filled is empty.)
1898 * @param a the array to be filled
1899 * @param fromIndex the index of the first element (inclusive) to be
1900 * filled with the specified value
1901 * @param toIndex the index of the last element (exclusive) to be
1902 * filled with the specified value
1903 * @param val the value to be stored in all elements of the array
1904 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
1905 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
1906 * <tt>toIndex > a.length</tt>
1908 public static void fill(long[] a, int fromIndex, int toIndex, long val) {
1909 rangeCheck(a.length, fromIndex, toIndex);
1910 for (int i = fromIndex; i < toIndex; i++)
1915 * Assigns the specified int value to each element of the specified array
1918 * @param a the array to be filled
1919 * @param val the value to be stored in all elements of the array
1921 public static void fill(int[] a, int val) {
1922 for (int i = 0, len = a.length; i < len; i++)
1927 * Assigns the specified int value to each element of the specified
1928 * range of the specified array of ints. The range to be filled
1929 * extends from index <tt>fromIndex</tt>, inclusive, to index
1930 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
1931 * range to be filled is empty.)
1933 * @param a the array to be filled
1934 * @param fromIndex the index of the first element (inclusive) to be
1935 * filled with the specified value
1936 * @param toIndex the index of the last element (exclusive) to be
1937 * filled with the specified value
1938 * @param val the value to be stored in all elements of the array
1939 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
1940 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
1941 * <tt>toIndex > a.length</tt>
1943 public static void fill(int[] a, int fromIndex, int toIndex, int val) {
1944 rangeCheck(a.length, fromIndex, toIndex);
1945 for (int i = fromIndex; i < toIndex; i++)
1950 * Assigns the specified short value to each element of the specified array
1953 * @param a the array to be filled
1954 * @param val the value to be stored in all elements of the array
1956 public static void fill(short[] a, short val) {
1957 for (int i = 0, len = a.length; i < len; i++)
1962 * Assigns the specified short value to each element of the specified
1963 * range of the specified array of shorts. The range to be filled
1964 * extends from index <tt>fromIndex</tt>, inclusive, to index
1965 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
1966 * range to be filled is empty.)
1968 * @param a the array to be filled
1969 * @param fromIndex the index of the first element (inclusive) to be
1970 * filled with the specified value
1971 * @param toIndex the index of the last element (exclusive) to be
1972 * filled with the specified value
1973 * @param val the value to be stored in all elements of the array
1974 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
1975 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
1976 * <tt>toIndex > a.length</tt>
1978 public static void fill(short[] a, int fromIndex, int toIndex, short val) {
1979 rangeCheck(a.length, fromIndex, toIndex);
1980 for (int i = fromIndex; i < toIndex; i++)
1985 * Assigns the specified char value to each element of the specified array
1988 * @param a the array to be filled
1989 * @param val the value to be stored in all elements of the array
1991 public static void fill(char[] a, char val) {
1992 for (int i = 0, len = a.length; i < len; i++)
1997 * Assigns the specified char value to each element of the specified
1998 * range of the specified array of chars. The range to be filled
1999 * extends from index <tt>fromIndex</tt>, inclusive, to index
2000 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2001 * range to be filled is empty.)
2003 * @param a the array to be filled
2004 * @param fromIndex the index of the first element (inclusive) to be
2005 * filled with the specified value
2006 * @param toIndex the index of the last element (exclusive) to be
2007 * filled with the specified value
2008 * @param val the value to be stored in all elements of the array
2009 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2010 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2011 * <tt>toIndex > a.length</tt>
2013 public static void fill(char[] a, int fromIndex, int toIndex, char val) {
2014 rangeCheck(a.length, fromIndex, toIndex);
2015 for (int i = fromIndex; i < toIndex; i++)
2020 * Assigns the specified byte value to each element of the specified array
2023 * @param a the array to be filled
2024 * @param val the value to be stored in all elements of the array
2026 public static void fill(byte[] a, byte val) {
2027 for (int i = 0, len = a.length; i < len; i++)
2032 * Assigns the specified byte value to each element of the specified
2033 * range of the specified array of bytes. The range to be filled
2034 * extends from index <tt>fromIndex</tt>, inclusive, to index
2035 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2036 * range to be filled is empty.)
2038 * @param a the array to be filled
2039 * @param fromIndex the index of the first element (inclusive) to be
2040 * filled with the specified value
2041 * @param toIndex the index of the last element (exclusive) to be
2042 * filled with the specified value
2043 * @param val the value to be stored in all elements of the array
2044 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2045 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2046 * <tt>toIndex > a.length</tt>
2048 public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
2049 rangeCheck(a.length, fromIndex, toIndex);
2050 for (int i = fromIndex; i < toIndex; i++)
2055 * Assigns the specified boolean value to each element of the specified
2056 * array of booleans.
2058 * @param a the array to be filled
2059 * @param val the value to be stored in all elements of the array
2061 public static void fill(boolean[] a, boolean val) {
2062 for (int i = 0, len = a.length; i < len; i++)
2067 * Assigns the specified boolean value to each element of the specified
2068 * range of the specified array of booleans. The range to be filled
2069 * extends from index <tt>fromIndex</tt>, inclusive, to index
2070 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2071 * range to be filled is empty.)
2073 * @param a the array to be filled
2074 * @param fromIndex the index of the first element (inclusive) to be
2075 * filled with the specified value
2076 * @param toIndex the index of the last element (exclusive) to be
2077 * filled with the specified value
2078 * @param val the value to be stored in all elements of the array
2079 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2080 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2081 * <tt>toIndex > a.length</tt>
2083 public static void fill(boolean[] a, int fromIndex, int toIndex,
2085 rangeCheck(a.length, fromIndex, toIndex);
2086 for (int i = fromIndex; i < toIndex; i++)
2091 * Assigns the specified double value to each element of the specified
2094 * @param a the array to be filled
2095 * @param val the value to be stored in all elements of the array
2097 public static void fill(double[] a, double val) {
2098 for (int i = 0, len = a.length; i < len; i++)
2103 * Assigns the specified double value to each element of the specified
2104 * range of the specified array of doubles. The range to be filled
2105 * extends from index <tt>fromIndex</tt>, inclusive, to index
2106 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2107 * range to be filled is empty.)
2109 * @param a the array to be filled
2110 * @param fromIndex the index of the first element (inclusive) to be
2111 * filled with the specified value
2112 * @param toIndex the index of the last element (exclusive) to be
2113 * filled with the specified value
2114 * @param val the value to be stored in all elements of the array
2115 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2116 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2117 * <tt>toIndex > a.length</tt>
2119 public static void fill(double[] a, int fromIndex, int toIndex,double val){
2120 rangeCheck(a.length, fromIndex, toIndex);
2121 for (int i = fromIndex; i < toIndex; i++)
2126 * Assigns the specified float value to each element of the specified array
2129 * @param a the array to be filled
2130 * @param val the value to be stored in all elements of the array
2132 public static void fill(float[] a, float val) {
2133 for (int i = 0, len = a.length; i < len; i++)
2138 * Assigns the specified float value to each element of the specified
2139 * range of the specified array of floats. The range to be filled
2140 * extends from index <tt>fromIndex</tt>, inclusive, to index
2141 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2142 * range to be filled is empty.)
2144 * @param a the array to be filled
2145 * @param fromIndex the index of the first element (inclusive) to be
2146 * filled with the specified value
2147 * @param toIndex the index of the last element (exclusive) to be
2148 * filled with the specified value
2149 * @param val the value to be stored in all elements of the array
2150 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2151 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2152 * <tt>toIndex > a.length</tt>
2154 public static void fill(float[] a, int fromIndex, int toIndex, float val) {
2155 rangeCheck(a.length, fromIndex, toIndex);
2156 for (int i = fromIndex; i < toIndex; i++)
2161 * Assigns the specified Object reference to each element of the specified
2164 * @param a the array to be filled
2165 * @param val the value to be stored in all elements of the array
2166 * @throws ArrayStoreException if the specified value is not of a
2167 * runtime type that can be stored in the specified array
2169 public static void fill(Object[] a, Object val) {
2170 for (int i = 0, len = a.length; i < len; i++)
2175 * Assigns the specified Object reference to each element of the specified
2176 * range of the specified array of Objects. The range to be filled
2177 * extends from index <tt>fromIndex</tt>, inclusive, to index
2178 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
2179 * range to be filled is empty.)
2181 * @param a the array to be filled
2182 * @param fromIndex the index of the first element (inclusive) to be
2183 * filled with the specified value
2184 * @param toIndex the index of the last element (exclusive) to be
2185 * filled with the specified value
2186 * @param val the value to be stored in all elements of the array
2187 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
2188 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
2189 * <tt>toIndex > a.length</tt>
2190 * @throws ArrayStoreException if the specified value is not of a
2191 * runtime type that can be stored in the specified array
2193 public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
2194 rangeCheck(a.length, fromIndex, toIndex);
2195 for (int i = fromIndex; i < toIndex; i++)
2202 * Copies the specified array, truncating or padding with nulls (if necessary)
2203 * so the copy has the specified length. For all indices that are
2204 * valid in both the original array and the copy, the two arrays will
2205 * contain identical values. For any indices that are valid in the
2206 * copy but not the original, the copy will contain <tt>null</tt>.
2207 * Such indices will exist if and only if the specified length
2208 * is greater than that of the original array.
2209 * The resulting array is of exactly the same class as the original array.
2211 * @param original the array to be copied
2212 * @param newLength the length of the copy to be returned
2213 * @return a copy of the original array, truncated or padded with nulls
2214 * to obtain the specified length
2215 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2216 * @throws NullPointerException if <tt>original</tt> is null
2219 public static <T> T[] copyOf(T[] original, int newLength) {
2220 return (T[]) copyOf(original, newLength, original.getClass());
2224 * Copies the specified array, truncating or padding with nulls (if necessary)
2225 * so the copy has the specified length. For all indices that are
2226 * valid in both the original array and the copy, the two arrays will
2227 * contain identical values. For any indices that are valid in the
2228 * copy but not the original, the copy will contain <tt>null</tt>.
2229 * Such indices will exist if and only if the specified length
2230 * is greater than that of the original array.
2231 * The resulting array is of the class <tt>newType</tt>.
2233 * @param original the array to be copied
2234 * @param newLength the length of the copy to be returned
2235 * @param newType the class of the copy to be returned
2236 * @return a copy of the original array, truncated or padded with nulls
2237 * to obtain the specified length
2238 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2239 * @throws NullPointerException if <tt>original</tt> is null
2240 * @throws ArrayStoreException if an element copied from
2241 * <tt>original</tt> is not of a runtime type that can be stored in
2242 * an array of class <tt>newType</tt>
2245 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
2246 T[] copy = ((Object)newType == (Object)Object[].class)
2247 ? (T[]) new Object[newLength]
2248 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
2249 System.arraycopy(original, 0, copy, 0,
2250 Math.min(original.length, newLength));
2255 * Copies the specified array, truncating or padding with zeros (if necessary)
2256 * so the copy has the specified length. For all indices that are
2257 * valid in both the original array and the copy, the two arrays will
2258 * contain identical values. For any indices that are valid in the
2259 * copy but not the original, the copy will contain <tt>(byte)0</tt>.
2260 * Such indices will exist if and only if the specified length
2261 * is greater than that of the original array.
2263 * @param original the array to be copied
2264 * @param newLength the length of the copy to be returned
2265 * @return a copy of the original array, truncated or padded with zeros
2266 * to obtain the specified length
2267 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2268 * @throws NullPointerException if <tt>original</tt> is null
2271 public static byte[] copyOf(byte[] original, int newLength) {
2272 byte[] copy = new byte[newLength];
2273 System.arraycopy(original, 0, copy, 0,
2274 Math.min(original.length, newLength));
2279 * Copies the specified array, truncating or padding with zeros (if necessary)
2280 * so the copy has the specified length. For all indices that are
2281 * valid in both the original array and the copy, the two arrays will
2282 * contain identical values. For any indices that are valid in the
2283 * copy but not the original, the copy will contain <tt>(short)0</tt>.
2284 * Such indices will exist if and only if the specified length
2285 * is greater than that of the original array.
2287 * @param original the array to be copied
2288 * @param newLength the length of the copy to be returned
2289 * @return a copy of the original array, truncated or padded with zeros
2290 * to obtain the specified length
2291 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2292 * @throws NullPointerException if <tt>original</tt> is null
2295 public static short[] copyOf(short[] original, int newLength) {
2296 short[] copy = new short[newLength];
2297 System.arraycopy(original, 0, copy, 0,
2298 Math.min(original.length, newLength));
2303 * Copies the specified array, truncating or padding with zeros (if necessary)
2304 * so the copy has the specified length. For all indices that are
2305 * valid in both the original array and the copy, the two arrays will
2306 * contain identical values. For any indices that are valid in the
2307 * copy but not the original, the copy will contain <tt>0</tt>.
2308 * Such indices will exist if and only if the specified length
2309 * is greater than that of the original array.
2311 * @param original the array to be copied
2312 * @param newLength the length of the copy to be returned
2313 * @return a copy of the original array, truncated or padded with zeros
2314 * to obtain the specified length
2315 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2316 * @throws NullPointerException if <tt>original</tt> is null
2319 public static int[] copyOf(int[] original, int newLength) {
2320 int[] copy = new int[newLength];
2321 System.arraycopy(original, 0, copy, 0,
2322 Math.min(original.length, newLength));
2327 * Copies the specified array, truncating or padding with zeros (if necessary)
2328 * so the copy has the specified length. For all indices that are
2329 * valid in both the original array and the copy, the two arrays will
2330 * contain identical values. For any indices that are valid in the
2331 * copy but not the original, the copy will contain <tt>0L</tt>.
2332 * Such indices will exist if and only if the specified length
2333 * is greater than that of the original array.
2335 * @param original the array to be copied
2336 * @param newLength the length of the copy to be returned
2337 * @return a copy of the original array, truncated or padded with zeros
2338 * to obtain the specified length
2339 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2340 * @throws NullPointerException if <tt>original</tt> is null
2343 public static long[] copyOf(long[] original, int newLength) {
2344 long[] copy = new long[newLength];
2345 System.arraycopy(original, 0, copy, 0,
2346 Math.min(original.length, newLength));
2351 * Copies the specified array, truncating or padding with null characters (if necessary)
2352 * so the copy has the specified length. For all indices that are valid
2353 * in both the original array and the copy, the two arrays will contain
2354 * identical values. For any indices that are valid in the copy but not
2355 * the original, the copy will contain <tt>'\\u000'</tt>. Such indices
2356 * will exist if and only if the specified length is greater than that of
2357 * the original array.
2359 * @param original the array to be copied
2360 * @param newLength the length of the copy to be returned
2361 * @return a copy of the original array, truncated or padded with null characters
2362 * to obtain the specified length
2363 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2364 * @throws NullPointerException if <tt>original</tt> is null
2367 public static char[] copyOf(char[] original, int newLength) {
2368 char[] copy = new char[newLength];
2369 System.arraycopy(original, 0, copy, 0,
2370 Math.min(original.length, newLength));
2375 * Copies the specified array, truncating or padding with zeros (if necessary)
2376 * so the copy has the specified length. For all indices that are
2377 * valid in both the original array and the copy, the two arrays will
2378 * contain identical values. For any indices that are valid in the
2379 * copy but not the original, the copy will contain <tt>0f</tt>.
2380 * Such indices will exist if and only if the specified length
2381 * is greater than that of the original array.
2383 * @param original the array to be copied
2384 * @param newLength the length of the copy to be returned
2385 * @return a copy of the original array, truncated or padded with zeros
2386 * to obtain the specified length
2387 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2388 * @throws NullPointerException if <tt>original</tt> is null
2391 public static float[] copyOf(float[] original, int newLength) {
2392 float[] copy = new float[newLength];
2393 System.arraycopy(original, 0, copy, 0,
2394 Math.min(original.length, newLength));
2399 * Copies the specified array, truncating or padding with zeros (if necessary)
2400 * so the copy has the specified length. For all indices that are
2401 * valid in both the original array and the copy, the two arrays will
2402 * contain identical values. For any indices that are valid in the
2403 * copy but not the original, the copy will contain <tt>0d</tt>.
2404 * Such indices will exist if and only if the specified length
2405 * is greater than that of the original array.
2407 * @param original the array to be copied
2408 * @param newLength the length of the copy to be returned
2409 * @return a copy of the original array, truncated or padded with zeros
2410 * to obtain the specified length
2411 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2412 * @throws NullPointerException if <tt>original</tt> is null
2415 public static double[] copyOf(double[] original, int newLength) {
2416 double[] copy = new double[newLength];
2417 System.arraycopy(original, 0, copy, 0,
2418 Math.min(original.length, newLength));
2423 * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary)
2424 * so the copy has the specified length. For all indices that are
2425 * valid in both the original array and the copy, the two arrays will
2426 * contain identical values. For any indices that are valid in the
2427 * copy but not the original, the copy will contain <tt>false</tt>.
2428 * Such indices will exist if and only if the specified length
2429 * is greater than that of the original array.
2431 * @param original the array to be copied
2432 * @param newLength the length of the copy to be returned
2433 * @return a copy of the original array, truncated or padded with false elements
2434 * to obtain the specified length
2435 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
2436 * @throws NullPointerException if <tt>original</tt> is null
2439 public static boolean[] copyOf(boolean[] original, int newLength) {
2440 boolean[] copy = new boolean[newLength];
2441 System.arraycopy(original, 0, copy, 0,
2442 Math.min(original.length, newLength));
2447 * Copies the specified range of the specified array into a new array.
2448 * The initial index of the range (<tt>from</tt>) must lie between zero
2449 * and <tt>original.length</tt>, inclusive. The value at
2450 * <tt>original[from]</tt> is placed into the initial element of the copy
2451 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2452 * Values from subsequent elements in the original array are placed into
2453 * subsequent elements in the copy. The final index of the range
2454 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2455 * may be greater than <tt>original.length</tt>, in which case
2456 * <tt>null</tt> is placed in all elements of the copy whose index is
2457 * greater than or equal to <tt>original.length - from</tt>. The length
2458 * of the returned array will be <tt>to - from</tt>.
2460 * The resulting array is of exactly the same class as the original array.
2462 * @param original the array from which a range is to be copied
2463 * @param from the initial index of the range to be copied, inclusive
2464 * @param to the final index of the range to be copied, exclusive.
2465 * (This index may lie outside the array.)
2466 * @return a new array containing the specified range from the original array,
2467 * truncated or padded with nulls to obtain the required length
2468 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2469 * or {@code from > original.length}
2470 * @throws IllegalArgumentException if <tt>from > to</tt>
2471 * @throws NullPointerException if <tt>original</tt> is null
2474 public static <T> T[] copyOfRange(T[] original, int from, int to) {
2475 return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
2479 * Copies the specified range of the specified array into a new array.
2480 * The initial index of the range (<tt>from</tt>) must lie between zero
2481 * and <tt>original.length</tt>, inclusive. The value at
2482 * <tt>original[from]</tt> is placed into the initial element of the copy
2483 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2484 * Values from subsequent elements in the original array are placed into
2485 * subsequent elements in the copy. The final index of the range
2486 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2487 * may be greater than <tt>original.length</tt>, in which case
2488 * <tt>null</tt> is placed in all elements of the copy whose index is
2489 * greater than or equal to <tt>original.length - from</tt>. The length
2490 * of the returned array will be <tt>to - from</tt>.
2491 * The resulting array is of the class <tt>newType</tt>.
2493 * @param original the array from which a range is to be copied
2494 * @param from the initial index of the range to be copied, inclusive
2495 * @param to the final index of the range to be copied, exclusive.
2496 * (This index may lie outside the array.)
2497 * @param newType the class of the copy to be returned
2498 * @return a new array containing the specified range from the original array,
2499 * truncated or padded with nulls to obtain the required length
2500 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2501 * or {@code from > original.length}
2502 * @throws IllegalArgumentException if <tt>from > to</tt>
2503 * @throws NullPointerException if <tt>original</tt> is null
2504 * @throws ArrayStoreException if an element copied from
2505 * <tt>original</tt> is not of a runtime type that can be stored in
2506 * an array of class <tt>newType</tt>.
2509 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
2510 int newLength = to - from;
2512 throw new IllegalArgumentException(from + " > " + to);
2513 T[] copy = ((Object)newType == (Object)Object[].class)
2514 ? (T[]) new Object[newLength]
2515 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
2516 System.arraycopy(original, from, copy, 0,
2517 Math.min(original.length - from, newLength));
2522 * Copies the specified range of the specified array into a new array.
2523 * The initial index of the range (<tt>from</tt>) must lie between zero
2524 * and <tt>original.length</tt>, inclusive. The value at
2525 * <tt>original[from]</tt> is placed into the initial element of the copy
2526 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2527 * Values from subsequent elements in the original array are placed into
2528 * subsequent elements in the copy. The final index of the range
2529 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2530 * may be greater than <tt>original.length</tt>, in which case
2531 * <tt>(byte)0</tt> is placed in all elements of the copy whose index is
2532 * greater than or equal to <tt>original.length - from</tt>. The length
2533 * of the returned array will be <tt>to - from</tt>.
2535 * @param original the array from which a range is to be copied
2536 * @param from the initial index of the range to be copied, inclusive
2537 * @param to the final index of the range to be copied, exclusive.
2538 * (This index may lie outside the array.)
2539 * @return a new array containing the specified range from the original array,
2540 * truncated or padded with zeros to obtain the required length
2541 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2542 * or {@code from > original.length}
2543 * @throws IllegalArgumentException if <tt>from > to</tt>
2544 * @throws NullPointerException if <tt>original</tt> is null
2547 public static byte[] copyOfRange(byte[] original, int from, int to) {
2548 int newLength = to - from;
2550 throw new IllegalArgumentException(from + " > " + to);
2551 byte[] copy = new byte[newLength];
2552 System.arraycopy(original, from, copy, 0,
2553 Math.min(original.length - from, newLength));
2558 * Copies the specified range of the specified array into a new array.
2559 * The initial index of the range (<tt>from</tt>) must lie between zero
2560 * and <tt>original.length</tt>, inclusive. The value at
2561 * <tt>original[from]</tt> is placed into the initial element of the copy
2562 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2563 * Values from subsequent elements in the original array are placed into
2564 * subsequent elements in the copy. The final index of the range
2565 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2566 * may be greater than <tt>original.length</tt>, in which case
2567 * <tt>(short)0</tt> is placed in all elements of the copy whose index is
2568 * greater than or equal to <tt>original.length - from</tt>. The length
2569 * of the returned array will be <tt>to - from</tt>.
2571 * @param original the array from which a range is to be copied
2572 * @param from the initial index of the range to be copied, inclusive
2573 * @param to the final index of the range to be copied, exclusive.
2574 * (This index may lie outside the array.)
2575 * @return a new array containing the specified range from the original array,
2576 * truncated or padded with zeros to obtain the required length
2577 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2578 * or {@code from > original.length}
2579 * @throws IllegalArgumentException if <tt>from > to</tt>
2580 * @throws NullPointerException if <tt>original</tt> is null
2583 public static short[] copyOfRange(short[] original, int from, int to) {
2584 int newLength = to - from;
2586 throw new IllegalArgumentException(from + " > " + to);
2587 short[] copy = new short[newLength];
2588 System.arraycopy(original, from, copy, 0,
2589 Math.min(original.length - from, newLength));
2594 * Copies the specified range of the specified array into a new array.
2595 * The initial index of the range (<tt>from</tt>) must lie between zero
2596 * and <tt>original.length</tt>, inclusive. The value at
2597 * <tt>original[from]</tt> is placed into the initial element of the copy
2598 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2599 * Values from subsequent elements in the original array are placed into
2600 * subsequent elements in the copy. The final index of the range
2601 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2602 * may be greater than <tt>original.length</tt>, in which case
2603 * <tt>0</tt> is placed in all elements of the copy whose index is
2604 * greater than or equal to <tt>original.length - from</tt>. The length
2605 * of the returned array will be <tt>to - from</tt>.
2607 * @param original the array from which a range is to be copied
2608 * @param from the initial index of the range to be copied, inclusive
2609 * @param to the final index of the range to be copied, exclusive.
2610 * (This index may lie outside the array.)
2611 * @return a new array containing the specified range from the original array,
2612 * truncated or padded with zeros to obtain the required length
2613 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2614 * or {@code from > original.length}
2615 * @throws IllegalArgumentException if <tt>from > to</tt>
2616 * @throws NullPointerException if <tt>original</tt> is null
2619 public static int[] copyOfRange(int[] original, int from, int to) {
2620 int newLength = to - from;
2622 throw new IllegalArgumentException(from + " > " + to);
2623 int[] copy = new int[newLength];
2624 System.arraycopy(original, from, copy, 0,
2625 Math.min(original.length - from, newLength));
2630 * Copies the specified range of the specified array into a new array.
2631 * The initial index of the range (<tt>from</tt>) must lie between zero
2632 * and <tt>original.length</tt>, inclusive. The value at
2633 * <tt>original[from]</tt> is placed into the initial element of the copy
2634 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2635 * Values from subsequent elements in the original array are placed into
2636 * subsequent elements in the copy. The final index of the range
2637 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2638 * may be greater than <tt>original.length</tt>, in which case
2639 * <tt>0L</tt> is placed in all elements of the copy whose index is
2640 * greater than or equal to <tt>original.length - from</tt>. The length
2641 * of the returned array will be <tt>to - from</tt>.
2643 * @param original the array from which a range is to be copied
2644 * @param from the initial index of the range to be copied, inclusive
2645 * @param to the final index of the range to be copied, exclusive.
2646 * (This index may lie outside the array.)
2647 * @return a new array containing the specified range from the original array,
2648 * truncated or padded with zeros to obtain the required length
2649 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2650 * or {@code from > original.length}
2651 * @throws IllegalArgumentException if <tt>from > to</tt>
2652 * @throws NullPointerException if <tt>original</tt> is null
2655 public static long[] copyOfRange(long[] original, int from, int to) {
2656 int newLength = to - from;
2658 throw new IllegalArgumentException(from + " > " + to);
2659 long[] copy = new long[newLength];
2660 System.arraycopy(original, from, copy, 0,
2661 Math.min(original.length - from, newLength));
2666 * Copies the specified range of the specified array into a new array.
2667 * The initial index of the range (<tt>from</tt>) must lie between zero
2668 * and <tt>original.length</tt>, inclusive. The value at
2669 * <tt>original[from]</tt> is placed into the initial element of the copy
2670 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2671 * Values from subsequent elements in the original array are placed into
2672 * subsequent elements in the copy. The final index of the range
2673 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2674 * may be greater than <tt>original.length</tt>, in which case
2675 * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is
2676 * greater than or equal to <tt>original.length - from</tt>. The length
2677 * of the returned array will be <tt>to - from</tt>.
2679 * @param original the array from which a range is to be copied
2680 * @param from the initial index of the range to be copied, inclusive
2681 * @param to the final index of the range to be copied, exclusive.
2682 * (This index may lie outside the array.)
2683 * @return a new array containing the specified range from the original array,
2684 * truncated or padded with null characters to obtain the required length
2685 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2686 * or {@code from > original.length}
2687 * @throws IllegalArgumentException if <tt>from > to</tt>
2688 * @throws NullPointerException if <tt>original</tt> is null
2691 public static char[] copyOfRange(char[] original, int from, int to) {
2692 int newLength = to - from;
2694 throw new IllegalArgumentException(from + " > " + to);
2695 char[] copy = new char[newLength];
2696 System.arraycopy(original, from, copy, 0,
2697 Math.min(original.length - from, newLength));
2702 * Copies the specified range of the specified array into a new array.
2703 * The initial index of the range (<tt>from</tt>) must lie between zero
2704 * and <tt>original.length</tt>, inclusive. The value at
2705 * <tt>original[from]</tt> is placed into the initial element of the copy
2706 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2707 * Values from subsequent elements in the original array are placed into
2708 * subsequent elements in the copy. The final index of the range
2709 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2710 * may be greater than <tt>original.length</tt>, in which case
2711 * <tt>0f</tt> is placed in all elements of the copy whose index is
2712 * greater than or equal to <tt>original.length - from</tt>. The length
2713 * of the returned array will be <tt>to - from</tt>.
2715 * @param original the array from which a range is to be copied
2716 * @param from the initial index of the range to be copied, inclusive
2717 * @param to the final index of the range to be copied, exclusive.
2718 * (This index may lie outside the array.)
2719 * @return a new array containing the specified range from the original array,
2720 * truncated or padded with zeros to obtain the required length
2721 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2722 * or {@code from > original.length}
2723 * @throws IllegalArgumentException if <tt>from > to</tt>
2724 * @throws NullPointerException if <tt>original</tt> is null
2727 public static float[] copyOfRange(float[] original, int from, int to) {
2728 int newLength = to - from;
2730 throw new IllegalArgumentException(from + " > " + to);
2731 float[] copy = new float[newLength];
2732 System.arraycopy(original, from, copy, 0,
2733 Math.min(original.length - from, newLength));
2738 * Copies the specified range of the specified array into a new array.
2739 * The initial index of the range (<tt>from</tt>) must lie between zero
2740 * and <tt>original.length</tt>, inclusive. The value at
2741 * <tt>original[from]</tt> is placed into the initial element of the copy
2742 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2743 * Values from subsequent elements in the original array are placed into
2744 * subsequent elements in the copy. The final index of the range
2745 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2746 * may be greater than <tt>original.length</tt>, in which case
2747 * <tt>0d</tt> is placed in all elements of the copy whose index is
2748 * greater than or equal to <tt>original.length - from</tt>. The length
2749 * of the returned array will be <tt>to - from</tt>.
2751 * @param original the array from which a range is to be copied
2752 * @param from the initial index of the range to be copied, inclusive
2753 * @param to the final index of the range to be copied, exclusive.
2754 * (This index may lie outside the array.)
2755 * @return a new array containing the specified range from the original array,
2756 * truncated or padded with zeros to obtain the required length
2757 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2758 * or {@code from > original.length}
2759 * @throws IllegalArgumentException if <tt>from > to</tt>
2760 * @throws NullPointerException if <tt>original</tt> is null
2763 public static double[] copyOfRange(double[] original, int from, int to) {
2764 int newLength = to - from;
2766 throw new IllegalArgumentException(from + " > " + to);
2767 double[] copy = new double[newLength];
2768 System.arraycopy(original, from, copy, 0,
2769 Math.min(original.length - from, newLength));
2774 * Copies the specified range of the specified array into a new array.
2775 * The initial index of the range (<tt>from</tt>) must lie between zero
2776 * and <tt>original.length</tt>, inclusive. The value at
2777 * <tt>original[from]</tt> is placed into the initial element of the copy
2778 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
2779 * Values from subsequent elements in the original array are placed into
2780 * subsequent elements in the copy. The final index of the range
2781 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
2782 * may be greater than <tt>original.length</tt>, in which case
2783 * <tt>false</tt> is placed in all elements of the copy whose index is
2784 * greater than or equal to <tt>original.length - from</tt>. The length
2785 * of the returned array will be <tt>to - from</tt>.
2787 * @param original the array from which a range is to be copied
2788 * @param from the initial index of the range to be copied, inclusive
2789 * @param to the final index of the range to be copied, exclusive.
2790 * (This index may lie outside the array.)
2791 * @return a new array containing the specified range from the original array,
2792 * truncated or padded with false elements to obtain the required length
2793 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
2794 * or {@code from > original.length}
2795 * @throws IllegalArgumentException if <tt>from > to</tt>
2796 * @throws NullPointerException if <tt>original</tt> is null
2799 public static boolean[] copyOfRange(boolean[] original, int from, int to) {
2800 int newLength = to - from;
2802 throw new IllegalArgumentException(from + " > " + to);
2803 boolean[] copy = new boolean[newLength];
2804 System.arraycopy(original, from, copy, 0,
2805 Math.min(original.length - from, newLength));
2812 * Returns a fixed-size list backed by the specified array. (Changes to
2813 * the returned list "write through" to the array.) This method acts
2814 * as bridge between array-based and collection-based APIs, in
2815 * combination with {@link Collection#toArray}. The returned list is
2816 * serializable and implements {@link RandomAccess}.
2818 * <p>This method also provides a convenient way to create a fixed-size
2819 * list initialized to contain several elements:
2821 * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
2824 * @param a the array by which the list will be backed
2825 * @return a list view of the specified array
2828 public static <T> List<T> asList(T... a) {
2829 return new ArrayList<>(a);
2835 private static class ArrayList<E> extends AbstractList<E>
2836 implements RandomAccess, java.io.Serializable
2838 private static final long serialVersionUID = -2764017481108945198L;
2839 private final E[] a;
2841 ArrayList(E[] array) {
2843 throw new NullPointerException();
2851 public Object[] toArray() {
2855 public <T> T[] toArray(T[] a) {
2857 if (a.length < size)
2858 return Arrays.copyOf(this.a, size,
2859 (Class<? extends T[]>) a.getClass());
2860 System.arraycopy(this.a, 0, a, 0, size);
2861 if (a.length > size)
2866 public E get(int index) {
2870 public E set(int index, E element) {
2871 E oldValue = a[index];
2876 public int indexOf(Object o) {
2878 for (int i=0; i<a.length; i++)
2882 for (int i=0; i<a.length; i++)
2889 public boolean contains(Object o) {
2890 return indexOf(o) != -1;
2895 * Returns a hash code based on the contents of the specified array.
2896 * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt>
2897 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
2898 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
2900 * <p>The value returned by this method is the same value that would be
2901 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
2902 * method on a {@link List} containing a sequence of {@link Long}
2903 * instances representing the elements of <tt>a</tt> in the same order.
2904 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
2906 * @param a the array whose hash value to compute
2907 * @return a content-based hash code for <tt>a</tt>
2910 public static int hashCode(long a[]) {
2915 for (long element : a) {
2916 int elementHash = (int)(element ^ (element >>> 32));
2917 result = 31 * result + elementHash;
2924 * Returns a hash code based on the contents of the specified array.
2925 * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt>
2926 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
2927 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
2929 * <p>The value returned by this method is the same value that would be
2930 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
2931 * method on a {@link List} containing a sequence of {@link Integer}
2932 * instances representing the elements of <tt>a</tt> in the same order.
2933 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
2935 * @param a the array whose hash value to compute
2936 * @return a content-based hash code for <tt>a</tt>
2939 public static int hashCode(int a[]) {
2944 for (int element : a)
2945 result = 31 * result + element;
2951 * Returns a hash code based on the contents of the specified array.
2952 * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt>
2953 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
2954 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
2956 * <p>The value returned by this method is the same value that would be
2957 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
2958 * method on a {@link List} containing a sequence of {@link Short}
2959 * instances representing the elements of <tt>a</tt> in the same order.
2960 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
2962 * @param a the array whose hash value to compute
2963 * @return a content-based hash code for <tt>a</tt>
2966 public static int hashCode(short a[]) {
2971 for (short element : a)
2972 result = 31 * result + element;
2978 * Returns a hash code based on the contents of the specified array.
2979 * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt>
2980 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
2981 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
2983 * <p>The value returned by this method is the same value that would be
2984 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
2985 * method on a {@link List} containing a sequence of {@link Character}
2986 * instances representing the elements of <tt>a</tt> in the same order.
2987 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
2989 * @param a the array whose hash value to compute
2990 * @return a content-based hash code for <tt>a</tt>
2993 public static int hashCode(char a[]) {
2998 for (char element : a)
2999 result = 31 * result + element;
3005 * Returns a hash code based on the contents of the specified array.
3006 * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt>
3007 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
3008 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
3010 * <p>The value returned by this method is the same value that would be
3011 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
3012 * method on a {@link List} containing a sequence of {@link Byte}
3013 * instances representing the elements of <tt>a</tt> in the same order.
3014 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
3016 * @param a the array whose hash value to compute
3017 * @return a content-based hash code for <tt>a</tt>
3020 public static int hashCode(byte a[]) {
3025 for (byte element : a)
3026 result = 31 * result + element;
3032 * Returns a hash code based on the contents of the specified array.
3033 * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt>
3034 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
3035 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
3037 * <p>The value returned by this method is the same value that would be
3038 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
3039 * method on a {@link List} containing a sequence of {@link Boolean}
3040 * instances representing the elements of <tt>a</tt> in the same order.
3041 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
3043 * @param a the array whose hash value to compute
3044 * @return a content-based hash code for <tt>a</tt>
3047 public static int hashCode(boolean a[]) {
3052 for (boolean element : a)
3053 result = 31 * result + (element ? 1231 : 1237);
3059 * Returns a hash code based on the contents of the specified array.
3060 * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt>
3061 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
3062 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
3064 * <p>The value returned by this method is the same value that would be
3065 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
3066 * method on a {@link List} containing a sequence of {@link Float}
3067 * instances representing the elements of <tt>a</tt> in the same order.
3068 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
3070 * @param a the array whose hash value to compute
3071 * @return a content-based hash code for <tt>a</tt>
3074 public static int hashCode(float a[]) {
3079 for (float element : a)
3080 result = 31 * result + Float.floatToIntBits(element);
3086 * Returns a hash code based on the contents of the specified array.
3087 * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt>
3088 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that
3089 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
3091 * <p>The value returned by this method is the same value that would be
3092 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>}
3093 * method on a {@link List} containing a sequence of {@link Double}
3094 * instances representing the elements of <tt>a</tt> in the same order.
3095 * If <tt>a</tt> is <tt>null</tt>, this method returns 0.
3097 * @param a the array whose hash value to compute
3098 * @return a content-based hash code for <tt>a</tt>
3101 public static int hashCode(double a[]) {
3106 for (double element : a) {
3107 long bits = Double.doubleToLongBits(element);
3108 result = 31 * result + (int)(bits ^ (bits >>> 32));
3114 * Returns a hash code based on the contents of the specified array. If
3115 * the array contains other arrays as elements, the hash code is based on
3116 * their identities rather than their contents. It is therefore
3117 * acceptable to invoke this method on an array that contains itself as an
3118 * element, either directly or indirectly through one or more levels of
3121 * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
3122 * <tt>Arrays.equals(a, b)</tt>, it is also the case that
3123 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>.
3125 * <p>The value returned by this method is equal to the value that would
3126 * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt>
3127 * is <tt>null</tt>, in which case <tt>0</tt> is returned.
3129 * @param a the array whose content-based hash code to compute
3130 * @return a content-based hash code for <tt>a</tt>
3131 * @see #deepHashCode(Object[])
3134 public static int hashCode(Object a[]) {
3140 for (Object element : a)
3141 result = 31 * result + (element == null ? 0 : element.hashCode());
3147 * Returns a hash code based on the "deep contents" of the specified
3148 * array. If the array contains other arrays as elements, the
3149 * hash code is based on their contents and so on, ad infinitum.
3150 * It is therefore unacceptable to invoke this method on an array that
3151 * contains itself as an element, either directly or indirectly through
3152 * one or more levels of arrays. The behavior of such an invocation is
3155 * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that
3156 * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that
3157 * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>.
3159 * <p>The computation of the value returned by this method is similar to
3160 * that of the value returned by {@link List#hashCode()} on a list
3161 * containing the same elements as <tt>a</tt> in the same order, with one
3162 * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array,
3163 * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as
3164 * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt>
3165 * if <tt>e</tt> is an array of a primitive type, or as by calling
3166 * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array
3167 * of a reference type. If <tt>a</tt> is <tt>null</tt>, this method
3170 * @param a the array whose deep-content-based hash code to compute
3171 * @return a deep-content-based hash code for <tt>a</tt>
3172 * @see #hashCode(Object[])
3175 public static int deepHashCode(Object a[]) {
3181 for (Object element : a) {
3182 int elementHash = 0;
3183 if (element instanceof Object[])
3184 elementHash = deepHashCode((Object[]) element);
3185 else if (element instanceof byte[])
3186 elementHash = hashCode((byte[]) element);
3187 else if (element instanceof short[])
3188 elementHash = hashCode((short[]) element);
3189 else if (element instanceof int[])
3190 elementHash = hashCode((int[]) element);
3191 else if (element instanceof long[])
3192 elementHash = hashCode((long[]) element);
3193 else if (element instanceof char[])
3194 elementHash = hashCode((char[]) element);
3195 else if (element instanceof float[])
3196 elementHash = hashCode((float[]) element);
3197 else if (element instanceof double[])
3198 elementHash = hashCode((double[]) element);
3199 else if (element instanceof boolean[])
3200 elementHash = hashCode((boolean[]) element);
3201 else if (element != null)
3202 elementHash = element.hashCode();
3204 result = 31 * result + elementHash;
3211 * Returns <tt>true</tt> if the two specified arrays are <i>deeply
3212 * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])}
3213 * method, this method is appropriate for use with nested arrays of
3216 * <p>Two array references are considered deeply equal if both
3217 * are <tt>null</tt>, or if they refer to arrays that contain the same
3218 * number of elements and all corresponding pairs of elements in the two
3219 * arrays are deeply equal.
3221 * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are
3222 * deeply equal if any of the following conditions hold:
3224 * <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference
3225 * types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt>
3226 * <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive
3227 * type, and the appropriate overloading of
3228 * <tt>Arrays.equals(e1, e2)</tt> would return true.
3229 * <li> <tt>e1 == e2</tt>
3230 * <li> <tt>e1.equals(e2)</tt> would return true.
3232 * Note that this definition permits <tt>null</tt> elements at any depth.
3234 * <p>If either of the specified arrays contain themselves as elements
3235 * either directly or indirectly through one or more levels of arrays,
3236 * the behavior of this method is undefined.
3238 * @param a1 one array to be tested for equality
3239 * @param a2 the other array to be tested for equality
3240 * @return <tt>true</tt> if the two arrays are equal
3241 * @see #equals(Object[],Object[])
3242 * @see Objects#deepEquals(Object, Object)
3245 public static boolean deepEquals(Object[] a1, Object[] a2) {
3248 if (a1 == null || a2==null)
3250 int length = a1.length;
3251 if (a2.length != length)
3254 for (int i = 0; i < length; i++) {
3263 // Figure out whether the two elements are equal
3264 boolean eq = deepEquals0(e1, e2);
3272 static boolean deepEquals0(Object e1, Object e2) {
3275 if (e1 instanceof Object[] && e2 instanceof Object[])
3276 eq = deepEquals ((Object[]) e1, (Object[]) e2);
3277 else if (e1 instanceof byte[] && e2 instanceof byte[])
3278 eq = equals((byte[]) e1, (byte[]) e2);
3279 else if (e1 instanceof short[] && e2 instanceof short[])
3280 eq = equals((short[]) e1, (short[]) e2);
3281 else if (e1 instanceof int[] && e2 instanceof int[])
3282 eq = equals((int[]) e1, (int[]) e2);
3283 else if (e1 instanceof long[] && e2 instanceof long[])
3284 eq = equals((long[]) e1, (long[]) e2);
3285 else if (e1 instanceof char[] && e2 instanceof char[])
3286 eq = equals((char[]) e1, (char[]) e2);
3287 else if (e1 instanceof float[] && e2 instanceof float[])
3288 eq = equals((float[]) e1, (float[]) e2);
3289 else if (e1 instanceof double[] && e2 instanceof double[])
3290 eq = equals((double[]) e1, (double[]) e2);
3291 else if (e1 instanceof boolean[] && e2 instanceof boolean[])
3292 eq = equals((boolean[]) e1, (boolean[]) e2);
3299 * Returns a string representation of the contents of the specified array.
3300 * The string representation consists of a list of the array's elements,
3301 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3302 * separated by the characters <tt>", "</tt> (a comma followed by a
3303 * space). Elements are converted to strings as by
3304 * <tt>String.valueOf(long)</tt>. Returns <tt>"null"</tt> if <tt>a</tt>
3307 * @param a the array whose string representation to return
3308 * @return a string representation of <tt>a</tt>
3311 public static String toString(long[] a) {
3314 int iMax = a.length - 1;
3318 StringBuilder b = new StringBuilder();
3320 for (int i = 0; ; i++) {
3323 return b.append(']').toString();
3329 * Returns a string representation of the contents of the specified array.
3330 * The string representation consists of a list of the array's elements,
3331 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3332 * separated by the characters <tt>", "</tt> (a comma followed by a
3333 * space). Elements are converted to strings as by
3334 * <tt>String.valueOf(int)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> is
3337 * @param a the array whose string representation to return
3338 * @return a string representation of <tt>a</tt>
3341 public static String toString(int[] a) {
3344 int iMax = a.length - 1;
3348 StringBuilder b = new StringBuilder();
3350 for (int i = 0; ; i++) {
3353 return b.append(']').toString();
3359 * Returns a string representation of the contents of the specified array.
3360 * The string representation consists of a list of the array's elements,
3361 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3362 * separated by the characters <tt>", "</tt> (a comma followed by a
3363 * space). Elements are converted to strings as by
3364 * <tt>String.valueOf(short)</tt>. Returns <tt>"null"</tt> if <tt>a</tt>
3367 * @param a the array whose string representation to return
3368 * @return a string representation of <tt>a</tt>
3371 public static String toString(short[] a) {
3374 int iMax = a.length - 1;
3378 StringBuilder b = new StringBuilder();
3380 for (int i = 0; ; i++) {
3383 return b.append(']').toString();
3389 * Returns a string representation of the contents of the specified array.
3390 * The string representation consists of a list of the array's elements,
3391 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3392 * separated by the characters <tt>", "</tt> (a comma followed by a
3393 * space). Elements are converted to strings as by
3394 * <tt>String.valueOf(char)</tt>. Returns <tt>"null"</tt> if <tt>a</tt>
3397 * @param a the array whose string representation to return
3398 * @return a string representation of <tt>a</tt>
3401 public static String toString(char[] a) {
3404 int iMax = a.length - 1;
3408 StringBuilder b = new StringBuilder();
3410 for (int i = 0; ; i++) {
3413 return b.append(']').toString();
3419 * Returns a string representation of the contents of the specified array.
3420 * The string representation consists of a list of the array's elements,
3421 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements
3422 * are separated by the characters <tt>", "</tt> (a comma followed
3423 * by a space). Elements are converted to strings as by
3424 * <tt>String.valueOf(byte)</tt>. Returns <tt>"null"</tt> if
3425 * <tt>a</tt> is <tt>null</tt>.
3427 * @param a the array whose string representation to return
3428 * @return a string representation of <tt>a</tt>
3431 public static String toString(byte[] a) {
3434 int iMax = a.length - 1;
3438 StringBuilder b = new StringBuilder();
3440 for (int i = 0; ; i++) {
3443 return b.append(']').toString();
3449 * Returns a string representation of the contents of the specified array.
3450 * The string representation consists of a list of the array's elements,
3451 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3452 * separated by the characters <tt>", "</tt> (a comma followed by a
3453 * space). Elements are converted to strings as by
3454 * <tt>String.valueOf(boolean)</tt>. Returns <tt>"null"</tt> if
3455 * <tt>a</tt> is <tt>null</tt>.
3457 * @param a the array whose string representation to return
3458 * @return a string representation of <tt>a</tt>
3461 public static String toString(boolean[] a) {
3464 int iMax = a.length - 1;
3468 StringBuilder b = new StringBuilder();
3470 for (int i = 0; ; i++) {
3473 return b.append(']').toString();
3479 * Returns a string representation of the contents of the specified array.
3480 * The string representation consists of a list of the array's elements,
3481 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3482 * separated by the characters <tt>", "</tt> (a comma followed by a
3483 * space). Elements are converted to strings as by
3484 * <tt>String.valueOf(float)</tt>. Returns <tt>"null"</tt> if <tt>a</tt>
3487 * @param a the array whose string representation to return
3488 * @return a string representation of <tt>a</tt>
3491 public static String toString(float[] a) {
3495 int iMax = a.length - 1;
3499 StringBuilder b = new StringBuilder();
3501 for (int i = 0; ; i++) {
3504 return b.append(']').toString();
3510 * Returns a string representation of the contents of the specified array.
3511 * The string representation consists of a list of the array's elements,
3512 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are
3513 * separated by the characters <tt>", "</tt> (a comma followed by a
3514 * space). Elements are converted to strings as by
3515 * <tt>String.valueOf(double)</tt>. Returns <tt>"null"</tt> if <tt>a</tt>
3518 * @param a the array whose string representation to return
3519 * @return a string representation of <tt>a</tt>
3522 public static String toString(double[] a) {
3525 int iMax = a.length - 1;
3529 StringBuilder b = new StringBuilder();
3531 for (int i = 0; ; i++) {
3534 return b.append(']').toString();
3540 * Returns a string representation of the contents of the specified array.
3541 * If the array contains other arrays as elements, they are converted to
3542 * strings by the {@link Object#toString} method inherited from
3543 * <tt>Object</tt>, which describes their <i>identities</i> rather than
3546 * <p>The value returned by this method is equal to the value that would
3547 * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt>
3548 * is <tt>null</tt>, in which case <tt>"null"</tt> is returned.
3550 * @param a the array whose string representation to return
3551 * @return a string representation of <tt>a</tt>
3552 * @see #deepToString(Object[])
3555 public static String toString(Object[] a) {
3559 int iMax = a.length - 1;
3563 StringBuilder b = new StringBuilder();
3565 for (int i = 0; ; i++) {
3566 b.append(String.valueOf(a[i]));
3568 return b.append(']').toString();
3574 * Returns a string representation of the "deep contents" of the specified
3575 * array. If the array contains other arrays as elements, the string
3576 * representation contains their contents and so on. This method is
3577 * designed for converting multidimensional arrays to strings.
3579 * <p>The string representation consists of a list of the array's
3580 * elements, enclosed in square brackets (<tt>"[]"</tt>). Adjacent
3581 * elements are separated by the characters <tt>", "</tt> (a comma
3582 * followed by a space). Elements are converted to strings as by
3583 * <tt>String.valueOf(Object)</tt>, unless they are themselves
3586 * <p>If an element <tt>e</tt> is an array of a primitive type, it is
3587 * converted to a string as by invoking the appropriate overloading of
3588 * <tt>Arrays.toString(e)</tt>. If an element <tt>e</tt> is an array of a
3589 * reference type, it is converted to a string as by invoking
3590 * this method recursively.
3592 * <p>To avoid infinite recursion, if the specified array contains itself
3593 * as an element, or contains an indirect reference to itself through one
3594 * or more levels of arrays, the self-reference is converted to the string
3595 * <tt>"[...]"</tt>. For example, an array containing only a reference
3596 * to itself would be rendered as <tt>"[[...]]"</tt>.
3598 * <p>This method returns <tt>"null"</tt> if the specified array
3601 * @param a the array whose string representation to return
3602 * @return a string representation of <tt>a</tt>
3603 * @see #toString(Object[])
3606 public static String deepToString(Object[] a) {
3610 int bufLen = 20 * a.length;
3611 if (a.length != 0 && bufLen <= 0)
3612 bufLen = Integer.MAX_VALUE;
3613 StringBuilder buf = new StringBuilder(bufLen);
3614 deepToString(a, buf, new HashSet<Object[]>());
3615 return buf.toString();
3618 private static void deepToString(Object[] a, StringBuilder buf,
3619 Set<Object[]> dejaVu) {
3624 int iMax = a.length - 1;
3632 for (int i = 0; ; i++) {
3634 Object element = a[i];
3635 if (element == null) {
3638 Class eClass = element.getClass();
3640 if (eClass.isArray()) {
3641 if (eClass == byte[].class)
3642 buf.append(toString((byte[]) element));
3643 else if (eClass == short[].class)
3644 buf.append(toString((short[]) element));
3645 else if (eClass == int[].class)
3646 buf.append(toString((int[]) element));
3647 else if (eClass == long[].class)
3648 buf.append(toString((long[]) element));
3649 else if (eClass == char[].class)
3650 buf.append(toString((char[]) element));
3651 else if (eClass == float[].class)
3652 buf.append(toString((float[]) element));
3653 else if (eClass == double[].class)
3654 buf.append(toString((double[]) element));
3655 else if (eClass == boolean[].class)
3656 buf.append(toString((boolean[]) element));
3657 else { // element is an array of object references
3658 if (dejaVu.contains(element))
3659 buf.append("[...]");
3661 deepToString((Object[])element, buf, dejaVu);
3663 } else { // element is non-null and not an array
3664 buf.append(element.toString());