ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Arrays.java
Revision: 1.2
Committed: Tue Sep 6 00:13:32 2005 UTC (18 years, 8 months ago) by peierls
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
State: FILE REMOVED
Log Message:
Removing Arrays

File Contents

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