1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 import java.lang.reflect.Array; 29 import java.util.concurrent.ForkJoinPool; 30 import java.util.function.BinaryOperator; 31 import java.util.function.Consumer; 32 import java.util.function.DoubleBinaryOperator; 33 import java.util.function.IntBinaryOperator; 34 import java.util.function.IntFunction; 35 import java.util.function.IntToDoubleFunction; 36 import java.util.function.IntToLongFunction; 37 import java.util.function.IntUnaryOperator; 38 import java.util.function.LongBinaryOperator; 39 import java.util.function.UnaryOperator; 40 import java.util.stream.DoubleStream; 41 import java.util.stream.IntStream; 42 import java.util.stream.LongStream; 43 import java.util.stream.Stream; 44 import java.util.stream.StreamSupport; 45 import jdk.internal.HotSpotIntrinsicCandidate; 46 47 /** 48 * This class contains various methods for manipulating arrays (such as 49 * sorting and searching). This class also contains a static factory 50 * that allows arrays to be viewed as lists. 51 * 52 * <p>The methods in this class all throw a {@code NullPointerException}, 53 * if the specified array reference is null, except where noted. 54 * 55 * <p>The documentation for the methods contained in this class includes 56 * brief descriptions of the <i>implementations</i>. Such descriptions should 57 * be regarded as <i>implementation notes</i>, rather than parts of the 58 * <i>specification</i>. Implementors should feel free to substitute other 59 * algorithms, so long as the specification itself is adhered to. (For 60 * example, the algorithm used by {@code sort(Object[])} does not have to be 61 * a MergeSort, but it does have to be <i>stable</i>.) 62 * 63 * <p>This class is a member of the 64 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 65 * Java Collections Framework</a>. 66 * 67 * @author Josh Bloch 68 * @author Neal Gafter 69 * @author John Rose 70 * @since 1.2 71 */ 72 public class Arrays { 73 74 /** 75 * The minimum array length below which a parallel sorting 76 * algorithm will not further partition the sorting task. Using 77 * smaller sizes typically results in memory contention across 78 * tasks that makes parallel speedups unlikely. 79 */ 80 private static final int MIN_ARRAY_SORT_GRAN = 1 << 13; 81 82 // Suppresses default constructor, ensuring non-instantiability. 83 private Arrays() {} 84 85 /** 86 * A comparator that implements the natural ordering of a group of 87 * mutually comparable elements. May be used when a supplied 88 * comparator is null. To simplify code-sharing within underlying 89 * implementations, the compare method only declares type Object 90 * for its second argument. 91 * 92 * Arrays class implementor's note: It is an empirical matter 93 * whether ComparableTimSort offers any performance benefit over 94 * TimSort used with this comparator. If not, you are better off 95 * deleting or bypassing ComparableTimSort. There is currently no 96 * empirical case for separating them for parallel sorting, so all 97 * public Object parallelSort methods use the same comparator 98 * based implementation. 99 */ 100 static final class NaturalOrder implements Comparator<Object> { 101 @SuppressWarnings("unchecked") 102 public int compare(Object first, Object second) { 103 return ((Comparable<Object>)first).compareTo(second); 104 } 105 static final NaturalOrder INSTANCE = new NaturalOrder(); 106 } 107 108 /** 109 * Checks that {@code fromIndex} and {@code toIndex} are in 110 * the range and throws an exception if they aren't. 111 */ 112 private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { 113 if (fromIndex > toIndex) { 114 throw new IllegalArgumentException( 115 "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); 116 } 117 if (fromIndex < 0) { 118 throw new ArrayIndexOutOfBoundsException(fromIndex); 119 } 120 if (toIndex > arrayLength) { 121 throw new ArrayIndexOutOfBoundsException(toIndex); 122 } 123 } 124 125 /* 126 * Sorting methods. Note that all public "sort" methods take the 127 * same form: Performing argument checks if necessary, and then 128 * expanding arguments into those required for the internal 129 * implementation methods residing in other package-private 130 * classes (except for legacyMergeSort, included in this class). 131 */ 132 133 /** 134 * Sorts the specified array into ascending numerical order. 135 * 136 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 137 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 138 * offers O(n log(n)) performance on many data sets that cause other 139 * quicksorts to degrade to quadratic performance, and is typically 140 * faster than traditional (one-pivot) Quicksort implementations. 141 * 142 * @param a the array to be sorted 143 */ 144 public static void sort(int[] a) { 145 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 146 } 147 148 /** 149 * Sorts the specified range of the array into ascending order. The range 150 * to be sorted extends from the index {@code fromIndex}, inclusive, to 151 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 152 * the range to be sorted is empty. 153 * 154 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 155 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 156 * offers O(n log(n)) performance on many data sets that cause other 157 * quicksorts to degrade to quadratic performance, and is typically 158 * faster than traditional (one-pivot) Quicksort implementations. 159 * 160 * @param a the array to be sorted 161 * @param fromIndex the index of the first element, inclusive, to be sorted 162 * @param toIndex the index of the last element, exclusive, to be sorted 163 * 164 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 165 * @throws ArrayIndexOutOfBoundsException 166 * if {@code fromIndex < 0} or {@code toIndex > a.length} 167 */ 168 public static void sort(int[] a, int fromIndex, int toIndex) { 169 rangeCheck(a.length, fromIndex, toIndex); 170 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 171 } 172 173 /** 174 * Sorts the specified array into ascending numerical order. 175 * 176 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 177 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 178 * offers O(n log(n)) performance on many data sets that cause other 179 * quicksorts to degrade to quadratic performance, and is typically 180 * faster than traditional (one-pivot) Quicksort implementations. 181 * 182 * @param a the array to be sorted 183 */ 184 public static void sort(long[] a) { 185 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 186 } 187 188 /** 189 * Sorts the specified range of the array into ascending order. The range 190 * to be sorted extends from the index {@code fromIndex}, inclusive, to 191 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 192 * the range to be sorted is empty. 193 * 194 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 195 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 196 * offers O(n log(n)) performance on many data sets that cause other 197 * quicksorts to degrade to quadratic performance, and is typically 198 * faster than traditional (one-pivot) Quicksort implementations. 199 * 200 * @param a the array to be sorted 201 * @param fromIndex the index of the first element, inclusive, to be sorted 202 * @param toIndex the index of the last element, exclusive, to be sorted 203 * 204 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 205 * @throws ArrayIndexOutOfBoundsException 206 * if {@code fromIndex < 0} or {@code toIndex > a.length} 207 */ 208 public static void sort(long[] a, int fromIndex, int toIndex) { 209 rangeCheck(a.length, fromIndex, toIndex); 210 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 211 } 212 213 /** 214 * Sorts the specified array into ascending numerical order. 215 * 216 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 217 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 218 * offers O(n log(n)) performance on many data sets that cause other 219 * quicksorts to degrade to quadratic performance, and is typically 220 * faster than traditional (one-pivot) Quicksort implementations. 221 * 222 * @param a the array to be sorted 223 */ 224 public static void sort(short[] a) { 225 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 226 } 227 228 /** 229 * Sorts the specified range of the array into ascending order. The range 230 * to be sorted extends from the index {@code fromIndex}, inclusive, to 231 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 232 * the range to be sorted is empty. 233 * 234 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 235 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 236 * offers O(n log(n)) performance on many data sets that cause other 237 * quicksorts to degrade to quadratic performance, and is typically 238 * faster than traditional (one-pivot) Quicksort implementations. 239 * 240 * @param a the array to be sorted 241 * @param fromIndex the index of the first element, inclusive, to be sorted 242 * @param toIndex the index of the last element, exclusive, to be sorted 243 * 244 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 245 * @throws ArrayIndexOutOfBoundsException 246 * if {@code fromIndex < 0} or {@code toIndex > a.length} 247 */ 248 public static void sort(short[] a, int fromIndex, int toIndex) { 249 rangeCheck(a.length, fromIndex, toIndex); 250 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 251 } 252 253 /** 254 * Sorts the specified array into ascending numerical order. 255 * 256 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 257 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 258 * offers O(n log(n)) performance on many data sets that cause other 259 * quicksorts to degrade to quadratic performance, and is typically 260 * faster than traditional (one-pivot) Quicksort implementations. 261 * 262 * @param a the array to be sorted 263 */ 264 public static void sort(char[] a) { 265 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 266 } 267 268 /** 269 * Sorts the specified range of the array into ascending order. The range 270 * to be sorted extends from the index {@code fromIndex}, inclusive, to 271 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 272 * the range to be sorted is empty. 273 * 274 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 275 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 276 * offers O(n log(n)) performance on many data sets that cause other 277 * quicksorts to degrade to quadratic performance, and is typically 278 * faster than traditional (one-pivot) Quicksort implementations. 279 * 280 * @param a the array to be sorted 281 * @param fromIndex the index of the first element, inclusive, to be sorted 282 * @param toIndex the index of the last element, exclusive, to be sorted 283 * 284 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 285 * @throws ArrayIndexOutOfBoundsException 286 * if {@code fromIndex < 0} or {@code toIndex > a.length} 287 */ 288 public static void sort(char[] a, int fromIndex, int toIndex) { 289 rangeCheck(a.length, fromIndex, toIndex); 290 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 291 } 292 293 /** 294 * Sorts the specified array into ascending numerical order. 295 * 296 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 297 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 298 * offers O(n log(n)) performance on many data sets that cause other 299 * quicksorts to degrade to quadratic performance, and is typically 300 * faster than traditional (one-pivot) Quicksort implementations. 301 * 302 * @param a the array to be sorted 303 */ 304 public static void sort(byte[] a) { 305 DualPivotQuicksort.sort(a, 0, a.length - 1); 306 } 307 308 /** 309 * Sorts the specified range of the array into ascending order. The range 310 * to be sorted extends from the index {@code fromIndex}, inclusive, to 311 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 312 * the range to be sorted is empty. 313 * 314 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 315 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 316 * offers O(n log(n)) performance on many data sets that cause other 317 * quicksorts to degrade to quadratic performance, and is typically 318 * faster than traditional (one-pivot) Quicksort implementations. 319 * 320 * @param a the array to be sorted 321 * @param fromIndex the index of the first element, inclusive, to be sorted 322 * @param toIndex the index of the last element, exclusive, to be sorted 323 * 324 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 325 * @throws ArrayIndexOutOfBoundsException 326 * if {@code fromIndex < 0} or {@code toIndex > a.length} 327 */ 328 public static void sort(byte[] a, int fromIndex, int toIndex) { 329 rangeCheck(a.length, fromIndex, toIndex); 330 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 331 } 332 333 /** 334 * Sorts the specified array into ascending numerical order. 335 * 336 * <p>The {@code <} relation does not provide a total order on all float 337 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 338 * value compares neither less than, greater than, nor equal to any value, 339 * even itself. This method uses the total order imposed by the method 340 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 341 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 342 * other value and all {@code Float.NaN} values are considered equal. 343 * 344 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 345 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 346 * offers O(n log(n)) performance on many data sets that cause other 347 * quicksorts to degrade to quadratic performance, and is typically 348 * faster than traditional (one-pivot) Quicksort implementations. 349 * 350 * @param a the array to be sorted 351 */ 352 public static void sort(float[] a) { 353 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 354 } 355 356 /** 357 * Sorts the specified range of the array into ascending order. The range 358 * to be sorted extends from the index {@code fromIndex}, inclusive, to 359 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 360 * the range to be sorted is empty. 361 * 362 * <p>The {@code <} relation does not provide a total order on all float 363 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 364 * value compares neither less than, greater than, nor equal to any value, 365 * even itself. This method uses the total order imposed by the method 366 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 367 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 368 * other value and all {@code Float.NaN} values are considered equal. 369 * 370 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 371 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 372 * offers O(n log(n)) performance on many data sets that cause other 373 * quicksorts to degrade to quadratic performance, and is typically 374 * faster than traditional (one-pivot) Quicksort implementations. 375 * 376 * @param a the array to be sorted 377 * @param fromIndex the index of the first element, inclusive, to be sorted 378 * @param toIndex the index of the last element, exclusive, to be sorted 379 * 380 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 381 * @throws ArrayIndexOutOfBoundsException 382 * if {@code fromIndex < 0} or {@code toIndex > a.length} 383 */ 384 public static void sort(float[] a, int fromIndex, int toIndex) { 385 rangeCheck(a.length, fromIndex, toIndex); 386 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 387 } 388 389 /** 390 * Sorts the specified array into ascending numerical order. 391 * 392 * <p>The {@code <} relation does not provide a total order on all double 393 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 394 * value compares neither less than, greater than, nor equal to any value, 395 * even itself. This method uses the total order imposed by the method 396 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 397 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 398 * other value and all {@code Double.NaN} values are considered equal. 399 * 400 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 401 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 402 * offers O(n log(n)) performance on many data sets that cause other 403 * quicksorts to degrade to quadratic performance, and is typically 404 * faster than traditional (one-pivot) Quicksort implementations. 405 * 406 * @param a the array to be sorted 407 */ 408 public static void sort(double[] a) { 409 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 410 } 411 412 /** 413 * Sorts the specified range of the array into ascending order. The range 414 * to be sorted extends from the index {@code fromIndex}, inclusive, to 415 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 416 * the range to be sorted is empty. 417 * 418 * <p>The {@code <} relation does not provide a total order on all double 419 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 420 * value compares neither less than, greater than, nor equal to any value, 421 * even itself. This method uses the total order imposed by the method 422 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 423 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 424 * other value and all {@code Double.NaN} values are considered equal. 425 * 426 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 427 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 428 * offers O(n log(n)) performance on many data sets that cause other 429 * quicksorts to degrade to quadratic performance, and is typically 430 * faster than traditional (one-pivot) Quicksort implementations. 431 * 432 * @param a the array to be sorted 433 * @param fromIndex the index of the first element, inclusive, to be sorted 434 * @param toIndex the index of the last element, exclusive, to be sorted 435 * 436 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 437 * @throws ArrayIndexOutOfBoundsException 438 * if {@code fromIndex < 0} or {@code toIndex > a.length} 439 */ 440 public static void sort(double[] a, int fromIndex, int toIndex) { 441 rangeCheck(a.length, fromIndex, toIndex); 442 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 443 } 444 445 /** 446 * Sorts the specified array into ascending numerical order. 447 * 448 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 449 * array into sub-arrays that are themselves sorted and then merged. When 450 * the sub-array length reaches a minimum granularity, the sub-array is 451 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 452 * method. If the length of the specified array is less than the minimum 453 * granularity, then it is sorted using the appropriate {@link 454 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a 455 * working space no greater than the size of the original array. The 456 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 457 * execute any parallel tasks. 458 * 459 * @param a the array to be sorted 460 * 461 * @since 1.8 462 */ 463 public static void parallelSort(byte[] a) { 464 int n = a.length, p, g; 465 if (n <= MIN_ARRAY_SORT_GRAN || 466 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 467 DualPivotQuicksort.sort(a, 0, n - 1); 468 else 469 new ArraysParallelSortHelpers.FJByte.Sorter 470 (null, a, new byte[n], 0, n, 0, 471 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 472 MIN_ARRAY_SORT_GRAN : g).invoke(); 473 } 474 475 /** 476 * Sorts the specified range of the array into ascending numerical order. 477 * The range to be sorted extends from the index {@code fromIndex}, 478 * inclusive, to the index {@code toIndex}, exclusive. If 479 * {@code fromIndex == toIndex}, the range to be sorted is empty. 480 * 481 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 482 * array into sub-arrays that are themselves sorted and then merged. When 483 * the sub-array length reaches a minimum granularity, the sub-array is 484 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 485 * method. If the length of the specified array is less than the minimum 486 * granularity, then it is sorted using the appropriate {@link 487 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working 488 * space no greater than the size of the specified range of the original 489 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 490 * used to execute any parallel tasks. 491 * 492 * @param a the array to be sorted 493 * @param fromIndex the index of the first element, inclusive, to be sorted 494 * @param toIndex the index of the last element, exclusive, to be sorted 495 * 496 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 497 * @throws ArrayIndexOutOfBoundsException 498 * if {@code fromIndex < 0} or {@code toIndex > a.length} 499 * 500 * @since 1.8 501 */ 502 public static void parallelSort(byte[] a, int fromIndex, int toIndex) { 503 rangeCheck(a.length, fromIndex, toIndex); 504 int n = toIndex - fromIndex, p, g; 505 if (n <= MIN_ARRAY_SORT_GRAN || 506 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 507 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 508 else 509 new ArraysParallelSortHelpers.FJByte.Sorter 510 (null, a, new byte[n], fromIndex, n, 0, 511 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 512 MIN_ARRAY_SORT_GRAN : g).invoke(); 513 } 514 515 /** 516 * Sorts the specified array into ascending numerical order. 517 * 518 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 519 * array into sub-arrays that are themselves sorted and then merged. When 520 * the sub-array length reaches a minimum granularity, the sub-array is 521 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 522 * method. If the length of the specified array is less than the minimum 523 * granularity, then it is sorted using the appropriate {@link 524 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a 525 * working space no greater than the size of the original array. The 526 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 527 * execute any parallel tasks. 528 * 529 * @param a the array to be sorted 530 * 531 * @since 1.8 532 */ 533 public static void parallelSort(char[] a) { 534 int n = a.length, p, g; 535 if (n <= MIN_ARRAY_SORT_GRAN || 536 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 537 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 538 else 539 new ArraysParallelSortHelpers.FJChar.Sorter 540 (null, a, new char[n], 0, n, 0, 541 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 542 MIN_ARRAY_SORT_GRAN : g).invoke(); 543 } 544 545 /** 546 * Sorts the specified range of the array into ascending numerical order. 547 * The range to be sorted extends from the index {@code fromIndex}, 548 * inclusive, to the index {@code toIndex}, exclusive. If 549 * {@code fromIndex == toIndex}, the range to be sorted is empty. 550 * 551 @implNote The sorting algorithm is a parallel sort-merge that breaks the 552 * array into sub-arrays that are themselves sorted and then merged. When 553 * the sub-array length reaches a minimum granularity, the sub-array is 554 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 555 * method. If the length of the specified array is less than the minimum 556 * granularity, then it is sorted using the appropriate {@link 557 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working 558 * space no greater than the size of the specified range of the original 559 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 560 * used to execute any parallel tasks. 561 * 562 * @param a the array to be sorted 563 * @param fromIndex the index of the first element, inclusive, to be sorted 564 * @param toIndex the index of the last element, exclusive, to be sorted 565 * 566 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 567 * @throws ArrayIndexOutOfBoundsException 568 * if {@code fromIndex < 0} or {@code toIndex > a.length} 569 * 570 * @since 1.8 571 */ 572 public static void parallelSort(char[] a, int fromIndex, int toIndex) { 573 rangeCheck(a.length, fromIndex, toIndex); 574 int n = toIndex - fromIndex, p, g; 575 if (n <= MIN_ARRAY_SORT_GRAN || 576 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 577 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 578 else 579 new ArraysParallelSortHelpers.FJChar.Sorter 580 (null, a, new char[n], fromIndex, n, 0, 581 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 582 MIN_ARRAY_SORT_GRAN : g).invoke(); 583 } 584 585 /** 586 * Sorts the specified array into ascending numerical order. 587 * 588 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 589 * array into sub-arrays that are themselves sorted and then merged. When 590 * the sub-array length reaches a minimum granularity, the sub-array is 591 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 592 * method. If the length of the specified array is less than the minimum 593 * granularity, then it is sorted using the appropriate {@link 594 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a 595 * working space no greater than the size of the original array. The 596 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 597 * execute any parallel tasks. 598 * 599 * @param a the array to be sorted 600 * 601 * @since 1.8 602 */ 603 public static void parallelSort(short[] a) { 604 int n = a.length, p, g; 605 if (n <= MIN_ARRAY_SORT_GRAN || 606 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 607 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 608 else 609 new ArraysParallelSortHelpers.FJShort.Sorter 610 (null, a, new short[n], 0, n, 0, 611 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 612 MIN_ARRAY_SORT_GRAN : g).invoke(); 613 } 614 615 /** 616 * Sorts the specified range of the array into ascending numerical order. 617 * The range to be sorted extends from the index {@code fromIndex}, 618 * inclusive, to the index {@code toIndex}, exclusive. If 619 * {@code fromIndex == toIndex}, the range to be sorted is empty. 620 * 621 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 622 * array into sub-arrays that are themselves sorted and then merged. When 623 * the sub-array length reaches a minimum granularity, the sub-array is 624 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 625 * method. If the length of the specified array is less than the minimum 626 * granularity, then it is sorted using the appropriate {@link 627 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working 628 * space no greater than the size of the specified range of the original 629 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 630 * used to execute any parallel tasks. 631 * 632 * @param a the array to be sorted 633 * @param fromIndex the index of the first element, inclusive, to be sorted 634 * @param toIndex the index of the last element, exclusive, to be sorted 635 * 636 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 637 * @throws ArrayIndexOutOfBoundsException 638 * if {@code fromIndex < 0} or {@code toIndex > a.length} 639 * 640 * @since 1.8 641 */ 642 public static void parallelSort(short[] a, int fromIndex, int toIndex) { 643 rangeCheck(a.length, fromIndex, toIndex); 644 int n = toIndex - fromIndex, p, g; 645 if (n <= MIN_ARRAY_SORT_GRAN || 646 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 647 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 648 else 649 new ArraysParallelSortHelpers.FJShort.Sorter 650 (null, a, new short[n], fromIndex, n, 0, 651 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 652 MIN_ARRAY_SORT_GRAN : g).invoke(); 653 } 654 655 /** 656 * Sorts the specified array into ascending numerical order. 657 * 658 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 659 * array into sub-arrays that are themselves sorted and then merged. When 660 * the sub-array length reaches a minimum granularity, the sub-array is 661 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 662 * method. If the length of the specified array is less than the minimum 663 * granularity, then it is sorted using the appropriate {@link 664 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a 665 * working space no greater than the size of the original array. The 666 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 667 * execute any parallel tasks. 668 * 669 * @param a the array to be sorted 670 * 671 * @since 1.8 672 */ 673 public static void parallelSort(int[] a) { 674 int n = a.length, p, g; 675 if (n <= MIN_ARRAY_SORT_GRAN || 676 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 677 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 678 else 679 new ArraysParallelSortHelpers.FJInt.Sorter 680 (null, a, new int[n], 0, n, 0, 681 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 682 MIN_ARRAY_SORT_GRAN : g).invoke(); 683 } 684 685 /** 686 * Sorts the specified range of the array into ascending numerical order. 687 * The range to be sorted extends from the index {@code fromIndex}, 688 * inclusive, to the index {@code toIndex}, exclusive. If 689 * {@code fromIndex == toIndex}, the range to be sorted is empty. 690 * 691 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 692 * array into sub-arrays that are themselves sorted and then merged. When 693 * the sub-array length reaches a minimum granularity, the sub-array is 694 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 695 * method. If the length of the specified array is less than the minimum 696 * granularity, then it is sorted using the appropriate {@link 697 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working 698 * space no greater than the size of the specified range of the original 699 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 700 * used to execute any parallel tasks. 701 * 702 * @param a the array to be sorted 703 * @param fromIndex the index of the first element, inclusive, to be sorted 704 * @param toIndex the index of the last element, exclusive, to be sorted 705 * 706 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 707 * @throws ArrayIndexOutOfBoundsException 708 * if {@code fromIndex < 0} or {@code toIndex > a.length} 709 * 710 * @since 1.8 711 */ 712 public static void parallelSort(int[] a, int fromIndex, int toIndex) { 713 rangeCheck(a.length, fromIndex, toIndex); 714 int n = toIndex - fromIndex, p, g; 715 if (n <= MIN_ARRAY_SORT_GRAN || 716 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 717 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 718 else 719 new ArraysParallelSortHelpers.FJInt.Sorter 720 (null, a, new int[n], fromIndex, n, 0, 721 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 722 MIN_ARRAY_SORT_GRAN : g).invoke(); 723 } 724 725 /** 726 * Sorts the specified array into ascending numerical order. 727 * 728 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 729 * array into sub-arrays that are themselves sorted and then merged. When 730 * the sub-array length reaches a minimum granularity, the sub-array is 731 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 732 * method. If the length of the specified array is less than the minimum 733 * granularity, then it is sorted using the appropriate {@link 734 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a 735 * working space no greater than the size of the original array. The 736 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 737 * execute any parallel tasks. 738 * 739 * @param a the array to be sorted 740 * 741 * @since 1.8 742 */ 743 public static void parallelSort(long[] a) { 744 int n = a.length, p, g; 745 if (n <= MIN_ARRAY_SORT_GRAN || 746 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 747 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 748 else 749 new ArraysParallelSortHelpers.FJLong.Sorter 750 (null, a, new long[n], 0, n, 0, 751 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 752 MIN_ARRAY_SORT_GRAN : g).invoke(); 753 } 754 755 /** 756 * Sorts the specified range of the array into ascending numerical order. 757 * The range to be sorted extends from the index {@code fromIndex}, 758 * inclusive, to the index {@code toIndex}, exclusive. If 759 * {@code fromIndex == toIndex}, the range to be sorted is empty. 760 * 761 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 762 * array into sub-arrays that are themselves sorted and then merged. When 763 * the sub-array length reaches a minimum granularity, the sub-array is 764 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 765 * method. If the length of the specified array is less than the minimum 766 * granularity, then it is sorted using the appropriate {@link 767 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working 768 * space no greater than the size of the specified range of the original 769 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 770 * used to execute any parallel tasks. 771 * 772 * @param a the array to be sorted 773 * @param fromIndex the index of the first element, inclusive, to be sorted 774 * @param toIndex the index of the last element, exclusive, to be sorted 775 * 776 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 777 * @throws ArrayIndexOutOfBoundsException 778 * if {@code fromIndex < 0} or {@code toIndex > a.length} 779 * 780 * @since 1.8 781 */ 782 public static void parallelSort(long[] a, int fromIndex, int toIndex) { 783 rangeCheck(a.length, fromIndex, toIndex); 784 int n = toIndex - fromIndex, p, g; 785 if (n <= MIN_ARRAY_SORT_GRAN || 786 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 787 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 788 else 789 new ArraysParallelSortHelpers.FJLong.Sorter 790 (null, a, new long[n], fromIndex, n, 0, 791 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 792 MIN_ARRAY_SORT_GRAN : g).invoke(); 793 } 794 795 /** 796 * Sorts the specified array into ascending numerical order. 797 * 798 * <p>The {@code <} relation does not provide a total order on all float 799 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 800 * value compares neither less than, greater than, nor equal to any value, 801 * even itself. This method uses the total order imposed by the method 802 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 803 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 804 * other value and all {@code Float.NaN} values are considered equal. 805 * 806 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 807 * array into sub-arrays that are themselves sorted and then merged. When 808 * the sub-array length reaches a minimum granularity, the sub-array is 809 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 810 * method. If the length of the specified array is less than the minimum 811 * granularity, then it is sorted using the appropriate {@link 812 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a 813 * working space no greater than the size of the original array. The 814 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 815 * execute any parallel tasks. 816 * 817 * @param a the array to be sorted 818 * 819 * @since 1.8 820 */ 821 public static void parallelSort(float[] a) { 822 int n = a.length, p, g; 823 if (n <= MIN_ARRAY_SORT_GRAN || 824 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 825 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 826 else 827 new ArraysParallelSortHelpers.FJFloat.Sorter 828 (null, a, new float[n], 0, n, 0, 829 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 830 MIN_ARRAY_SORT_GRAN : g).invoke(); 831 } 832 833 /** 834 * Sorts the specified range of the array into ascending numerical order. 835 * The range to be sorted extends from the index {@code fromIndex}, 836 * inclusive, to the index {@code toIndex}, exclusive. If 837 * {@code fromIndex == toIndex}, the range to be sorted is empty. 838 * 839 * <p>The {@code <} relation does not provide a total order on all float 840 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 841 * value compares neither less than, greater than, nor equal to any value, 842 * even itself. This method uses the total order imposed by the method 843 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 844 * {@code 0.0f} and {@code Float.NaN} is considered greater than any 845 * other value and all {@code Float.NaN} values are considered equal. 846 * 847 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 848 * array into sub-arrays that are themselves sorted and then merged. When 849 * the sub-array length reaches a minimum granularity, the sub-array is 850 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 851 * method. If the length of the specified array is less than the minimum 852 * granularity, then it is sorted using the appropriate {@link 853 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working 854 * space no greater than the size of the specified range of the original 855 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 856 * used to execute any parallel tasks. 857 * 858 * @param a the array to be sorted 859 * @param fromIndex the index of the first element, inclusive, to be sorted 860 * @param toIndex the index of the last element, exclusive, to be sorted 861 * 862 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 863 * @throws ArrayIndexOutOfBoundsException 864 * if {@code fromIndex < 0} or {@code toIndex > a.length} 865 * 866 * @since 1.8 867 */ 868 public static void parallelSort(float[] a, int fromIndex, int toIndex) { 869 rangeCheck(a.length, fromIndex, toIndex); 870 int n = toIndex - fromIndex, p, g; 871 if (n <= MIN_ARRAY_SORT_GRAN || 872 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 873 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 874 else 875 new ArraysParallelSortHelpers.FJFloat.Sorter 876 (null, a, new float[n], fromIndex, n, 0, 877 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 878 MIN_ARRAY_SORT_GRAN : g).invoke(); 879 } 880 881 /** 882 * Sorts the specified array into ascending numerical order. 883 * 884 * <p>The {@code <} relation does not provide a total order on all double 885 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 886 * value compares neither less than, greater than, nor equal to any value, 887 * even itself. This method uses the total order imposed by the method 888 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 889 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 890 * other value and all {@code Double.NaN} values are considered equal. 891 * 892 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 893 * array into sub-arrays that are themselves sorted and then merged. When 894 * the sub-array length reaches a minimum granularity, the sub-array is 895 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 896 * method. If the length of the specified array is less than the minimum 897 * granularity, then it is sorted using the appropriate {@link 898 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a 899 * working space no greater than the size of the original array. The 900 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 901 * execute any parallel tasks. 902 * 903 * @param a the array to be sorted 904 * 905 * @since 1.8 906 */ 907 public static void parallelSort(double[] a) { 908 int n = a.length, p, g; 909 if (n <= MIN_ARRAY_SORT_GRAN || 910 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 911 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 912 else 913 new ArraysParallelSortHelpers.FJDouble.Sorter 914 (null, a, new double[n], 0, n, 0, 915 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 916 MIN_ARRAY_SORT_GRAN : g).invoke(); 917 } 918 919 /** 920 * Sorts the specified range of the array into ascending numerical order. 921 * The range to be sorted extends from the index {@code fromIndex}, 922 * inclusive, to the index {@code toIndex}, exclusive. If 923 * {@code fromIndex == toIndex}, the range to be sorted is empty. 924 * 925 * <p>The {@code <} relation does not provide a total order on all double 926 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 927 * value compares neither less than, greater than, nor equal to any value, 928 * even itself. This method uses the total order imposed by the method 929 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 930 * {@code 0.0d} and {@code Double.NaN} is considered greater than any 931 * other value and all {@code Double.NaN} values are considered equal. 932 * 933 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 934 * array into sub-arrays that are themselves sorted and then merged. When 935 * the sub-array length reaches a minimum granularity, the sub-array is 936 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 937 * method. If the length of the specified array is less than the minimum 938 * granularity, then it is sorted using the appropriate {@link 939 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working 940 * space no greater than the size of the specified range of the original 941 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 942 * used to execute any parallel tasks. 943 * 944 * @param a the array to be sorted 945 * @param fromIndex the index of the first element, inclusive, to be sorted 946 * @param toIndex the index of the last element, exclusive, to be sorted 947 * 948 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 949 * @throws ArrayIndexOutOfBoundsException 950 * if {@code fromIndex < 0} or {@code toIndex > a.length} 951 * 952 * @since 1.8 953 */ 954 public static void parallelSort(double[] a, int fromIndex, int toIndex) { 955 rangeCheck(a.length, fromIndex, toIndex); 956 int n = toIndex - fromIndex, p, g; 957 if (n <= MIN_ARRAY_SORT_GRAN || 958 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 959 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 960 else 961 new ArraysParallelSortHelpers.FJDouble.Sorter 962 (null, a, new double[n], fromIndex, n, 0, 963 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 964 MIN_ARRAY_SORT_GRAN : g).invoke(); 965 } 966 967 /** 968 * Sorts the specified array of objects into ascending order, according 969 * to the {@linkplain Comparable natural ordering} of its elements. 970 * All elements in the array must implement the {@link Comparable} 971 * interface. Furthermore, all elements in the array must be 972 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 973 * not throw a {@code ClassCastException} for any elements {@code e1} 974 * and {@code e2} in the array). 975 * 976 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 977 * not be reordered as a result of the sort. 978 * 979 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 980 * array into sub-arrays that are themselves sorted and then merged. When 981 * the sub-array length reaches a minimum granularity, the sub-array is 982 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 983 * method. If the length of the specified array is less than the minimum 984 * granularity, then it is sorted using the appropriate {@link 985 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 986 * working space no greater than the size of the original array. The 987 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 988 * execute any parallel tasks. 989 * 990 * @param <T> the class of the objects to be sorted 991 * @param a the array to be sorted 992 * 993 * @throws ClassCastException if the array contains elements that are not 994 * <i>mutually comparable</i> (for example, strings and integers) 995 * @throws IllegalArgumentException (optional) if the natural 996 * ordering of the array elements is found to violate the 997 * {@link Comparable} contract 998 * 999 * @since 1.8 1000 */ 1001 @SuppressWarnings("unchecked") 1002 public static <T extends Comparable<? super T>> void parallelSort(T[] a) { 1003 int n = a.length, p, g; 1004 if (n <= MIN_ARRAY_SORT_GRAN || 1005 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1006 TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0); 1007 else 1008 new ArraysParallelSortHelpers.FJObject.Sorter<> 1009 (null, a, 1010 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1011 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1012 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1013 } 1014 1015 /** 1016 * Sorts the specified range of the specified array of objects into 1017 * ascending order, according to the 1018 * {@linkplain Comparable natural ordering} of its 1019 * elements. The range to be sorted extends from index 1020 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 1021 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 1022 * elements in this range must implement the {@link Comparable} 1023 * interface. Furthermore, all elements in this range must be <i>mutually 1024 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1025 * {@code ClassCastException} for any elements {@code e1} and 1026 * {@code e2} in the array). 1027 * 1028 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1029 * not be reordered as a result of the sort. 1030 * 1031 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1032 * array into sub-arrays that are themselves sorted and then merged. When 1033 * the sub-array length reaches a minimum granularity, the sub-array is 1034 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1035 * method. If the length of the specified array is less than the minimum 1036 * granularity, then it is sorted using the appropriate {@link 1037 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1038 * space no greater than the size of the specified range of the original 1039 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1040 * used to execute any parallel tasks. 1041 * 1042 * @param <T> the class of the objects to be sorted 1043 * @param a the array to be sorted 1044 * @param fromIndex the index of the first element (inclusive) to be 1045 * sorted 1046 * @param toIndex the index of the last element (exclusive) to be sorted 1047 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1048 * (optional) if the natural ordering of the array elements is 1049 * found to violate the {@link Comparable} contract 1050 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1051 * {@code toIndex > a.length} 1052 * @throws ClassCastException if the array contains elements that are 1053 * not <i>mutually comparable</i> (for example, strings and 1054 * integers). 1055 * 1056 * @since 1.8 1057 */ 1058 @SuppressWarnings("unchecked") 1059 public static <T extends Comparable<? super T>> 1060 void parallelSort(T[] a, int fromIndex, int toIndex) { 1061 rangeCheck(a.length, fromIndex, toIndex); 1062 int n = toIndex - fromIndex, p, g; 1063 if (n <= MIN_ARRAY_SORT_GRAN || 1064 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1065 TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0); 1066 else 1067 new ArraysParallelSortHelpers.FJObject.Sorter<> 1068 (null, a, 1069 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1070 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1071 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1072 } 1073 1074 /** 1075 * Sorts the specified array of objects according to the order induced by 1076 * the specified comparator. All elements in the array must be 1077 * <i>mutually comparable</i> by the specified comparator (that is, 1078 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1079 * for any elements {@code e1} and {@code e2} in the array). 1080 * 1081 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1082 * not be reordered as a result of the sort. 1083 * 1084 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1085 * array into sub-arrays that are themselves sorted and then merged. When 1086 * the sub-array length reaches a minimum granularity, the sub-array is 1087 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1088 * method. If the length of the specified array is less than the minimum 1089 * granularity, then it is sorted using the appropriate {@link 1090 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 1091 * working space no greater than the size of the original array. The 1092 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 1093 * execute any parallel tasks. 1094 * 1095 * @param <T> the class of the objects to be sorted 1096 * @param a the array to be sorted 1097 * @param cmp the comparator to determine the order of the array. A 1098 * {@code null} value indicates that the elements' 1099 * {@linkplain Comparable natural ordering} should be used. 1100 * @throws ClassCastException if the array contains elements that are 1101 * not <i>mutually comparable</i> using the specified comparator 1102 * @throws IllegalArgumentException (optional) if the comparator is 1103 * found to violate the {@link java.util.Comparator} contract 1104 * 1105 * @since 1.8 1106 */ 1107 @SuppressWarnings("unchecked") 1108 public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) { 1109 if (cmp == null) 1110 cmp = NaturalOrder.INSTANCE; 1111 int n = a.length, p, g; 1112 if (n <= MIN_ARRAY_SORT_GRAN || 1113 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1114 TimSort.sort(a, 0, n, cmp, null, 0, 0); 1115 else 1116 new ArraysParallelSortHelpers.FJObject.Sorter<> 1117 (null, a, 1118 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1119 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1120 MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1121 } 1122 1123 /** 1124 * Sorts the specified range of the specified array of objects according 1125 * to the order induced by the specified comparator. The range to be 1126 * sorted extends from index {@code fromIndex}, inclusive, to index 1127 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1128 * range to be sorted is empty.) All elements in the range must be 1129 * <i>mutually comparable</i> by the specified comparator (that is, 1130 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1131 * for any elements {@code e1} and {@code e2} in the range). 1132 * 1133 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1134 * not be reordered as a result of the sort. 1135 * 1136 * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1137 * array into sub-arrays that are themselves sorted and then merged. When 1138 * the sub-array length reaches a minimum granularity, the sub-array is 1139 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1140 * method. If the length of the specified array is less than the minimum 1141 * granularity, then it is sorted using the appropriate {@link 1142 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1143 * space no greater than the size of the specified range of the original 1144 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1145 * used to execute any parallel tasks. 1146 * 1147 * @param <T> the class of the objects to be sorted 1148 * @param a the array to be sorted 1149 * @param fromIndex the index of the first element (inclusive) to be 1150 * sorted 1151 * @param toIndex the index of the last element (exclusive) to be sorted 1152 * @param cmp the comparator to determine the order of the array. A 1153 * {@code null} value indicates that the elements' 1154 * {@linkplain Comparable natural ordering} should be used. 1155 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1156 * (optional) if the natural ordering of the array elements is 1157 * found to violate the {@link Comparable} contract 1158 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1159 * {@code toIndex > a.length} 1160 * @throws ClassCastException if the array contains elements that are 1161 * not <i>mutually comparable</i> (for example, strings and 1162 * integers). 1163 * 1164 * @since 1.8 1165 */ 1166 @SuppressWarnings("unchecked") 1167 public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, 1168 Comparator<? super T> cmp) { 1169 rangeCheck(a.length, fromIndex, toIndex); 1170 if (cmp == null) 1171 cmp = NaturalOrder.INSTANCE; 1172 int n = toIndex - fromIndex, p, g; 1173 if (n <= MIN_ARRAY_SORT_GRAN || 1174 (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1175 TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0); 1176 else 1177 new ArraysParallelSortHelpers.FJObject.Sorter<> 1178 (null, a, 1179 (T[])Array.newInstance(a.getClass().getComponentType(), n), 1180 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1181 MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1182 } 1183 1184 /* 1185 * Sorting of complex type arrays. 1186 */ 1187 1188 /** 1189 * Old merge sort implementation can be selected (for 1190 * compatibility with broken comparators) using a system property. 1191 * Cannot be a static boolean in the enclosing class due to 1192 * circular dependencies. To be removed in a future release. 1193 */ 1194 static final class LegacyMergeSort { 1195 private static final boolean userRequested = 1196 java.security.AccessController.doPrivileged( 1197 new sun.security.action.GetBooleanAction( 1198 "java.util.Arrays.useLegacyMergeSort")).booleanValue(); 1199 } 1200 1201 /** 1202 * Sorts the specified array of objects into ascending order, according 1203 * to the {@linkplain Comparable natural ordering} of its elements. 1204 * All elements in the array must implement the {@link Comparable} 1205 * interface. Furthermore, all elements in the array must be 1206 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 1207 * not throw a {@code ClassCastException} for any elements {@code e1} 1208 * and {@code e2} in the array). 1209 * 1210 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1211 * not be reordered as a result of the sort. 1212 * 1213 * <p>Implementation note: This implementation is a stable, adaptive, 1214 * iterative mergesort that requires far fewer than n lg(n) comparisons 1215 * when the input array is partially sorted, while offering the 1216 * performance of a traditional mergesort when the input array is 1217 * randomly ordered. If the input array is nearly sorted, the 1218 * implementation requires approximately n comparisons. Temporary 1219 * storage requirements vary from a small constant for nearly sorted 1220 * input arrays to n/2 object references for randomly ordered input 1221 * arrays. 1222 * 1223 * <p>The implementation takes equal advantage of ascending and 1224 * descending order in its input array, and can take advantage of 1225 * ascending and descending order in different parts of the same 1226 * input array. It is well-suited to merging two or more sorted arrays: 1227 * simply concatenate the arrays and sort the resulting array. 1228 * 1229 * <p>The implementation was adapted from Tim Peters's list sort for Python 1230 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1231 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1232 * Sorting and Information Theoretic Complexity", in Proceedings of the 1233 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1234 * January 1993. 1235 * 1236 * @param a the array to be sorted 1237 * @throws ClassCastException if the array contains elements that are not 1238 * <i>mutually comparable</i> (for example, strings and integers) 1239 * @throws IllegalArgumentException (optional) if the natural 1240 * ordering of the array elements is found to violate the 1241 * {@link Comparable} contract 1242 */ 1243 public static void sort(Object[] a) { 1244 if (LegacyMergeSort.userRequested) 1245 legacyMergeSort(a); 1246 else 1247 ComparableTimSort.sort(a, 0, a.length, null, 0, 0); 1248 } 1249 1250 /** To be removed in a future release. */ 1251 private static void legacyMergeSort(Object[] a) { 1252 Object[] aux = a.clone(); 1253 mergeSort(aux, a, 0, a.length, 0); 1254 } 1255 1256 /** 1257 * Sorts the specified range of the specified array of objects into 1258 * ascending order, according to the 1259 * {@linkplain Comparable natural ordering} of its 1260 * elements. The range to be sorted extends from index 1261 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 1262 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 1263 * elements in this range must implement the {@link Comparable} 1264 * interface. Furthermore, all elements in this range must be <i>mutually 1265 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1266 * {@code ClassCastException} for any elements {@code e1} and 1267 * {@code e2} in the array). 1268 * 1269 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1270 * not be reordered as a result of the sort. 1271 * 1272 * <p>Implementation note: This implementation is a stable, adaptive, 1273 * iterative mergesort that requires far fewer than n lg(n) comparisons 1274 * when the input array is partially sorted, while offering the 1275 * performance of a traditional mergesort when the input array is 1276 * randomly ordered. If the input array is nearly sorted, the 1277 * implementation requires approximately n comparisons. Temporary 1278 * storage requirements vary from a small constant for nearly sorted 1279 * input arrays to n/2 object references for randomly ordered input 1280 * arrays. 1281 * 1282 * <p>The implementation takes equal advantage of ascending and 1283 * descending order in its input array, and can take advantage of 1284 * ascending and descending order in different parts of the same 1285 * input array. It is well-suited to merging two or more sorted arrays: 1286 * simply concatenate the arrays and sort the resulting array. 1287 * 1288 * <p>The implementation was adapted from Tim Peters's list sort for Python 1289 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1290 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1291 * Sorting and Information Theoretic Complexity", in Proceedings of the 1292 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1293 * January 1993. 1294 * 1295 * @param a the array to be sorted 1296 * @param fromIndex the index of the first element (inclusive) to be 1297 * sorted 1298 * @param toIndex the index of the last element (exclusive) to be sorted 1299 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1300 * (optional) if the natural ordering of the array elements is 1301 * found to violate the {@link Comparable} contract 1302 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1303 * {@code toIndex > a.length} 1304 * @throws ClassCastException if the array contains elements that are 1305 * not <i>mutually comparable</i> (for example, strings and 1306 * integers). 1307 */ 1308 public static void sort(Object[] a, int fromIndex, int toIndex) { 1309 rangeCheck(a.length, fromIndex, toIndex); 1310 if (LegacyMergeSort.userRequested) 1311 legacyMergeSort(a, fromIndex, toIndex); 1312 else 1313 ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0); 1314 } 1315 1316 /** To be removed in a future release. */ 1317 private static void legacyMergeSort(Object[] a, 1318 int fromIndex, int toIndex) { 1319 Object[] aux = copyOfRange(a, fromIndex, toIndex); 1320 mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 1321 } 1322 1323 /** 1324 * Tuning parameter: list size at or below which insertion sort will be 1325 * used in preference to mergesort. 1326 * To be removed in a future release. 1327 */ 1328 private static final int INSERTIONSORT_THRESHOLD = 7; 1329 1330 /** 1331 * Src is the source array that starts at index 0 1332 * Dest is the (possibly larger) array destination with a possible offset 1333 * low is the index in dest to start sorting 1334 * high is the end index in dest to end sorting 1335 * off is the offset to generate corresponding low, high in src 1336 * To be removed in a future release. 1337 */ 1338 @SuppressWarnings({"unchecked", "rawtypes"}) 1339 private static void mergeSort(Object[] src, 1340 Object[] dest, 1341 int low, 1342 int high, 1343 int off) { 1344 int length = high - low; 1345 1346 // Insertion sort on smallest arrays 1347 if (length < INSERTIONSORT_THRESHOLD) { 1348 for (int i=low; i<high; i++) 1349 for (int j=i; j>low && 1350 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) 1351 swap(dest, j, j-1); 1352 return; 1353 } 1354 1355 // Recursively sort halves of dest into src 1356 int destLow = low; 1357 int destHigh = high; 1358 low += off; 1359 high += off; 1360 int mid = (low + high) >>> 1; 1361 mergeSort(dest, src, low, mid, -off); 1362 mergeSort(dest, src, mid, high, -off); 1363 1364 // If list is already sorted, just copy from src to dest. This is an 1365 // optimization that results in faster sorts for nearly ordered lists. 1366 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { 1367 System.arraycopy(src, low, dest, destLow, length); 1368 return; 1369 } 1370 1371 // Merge sorted halves (now in src) into dest 1372 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 1373 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) 1374 dest[i] = src[p++]; 1375 else 1376 dest[i] = src[q++]; 1377 } 1378 } 1379 1380 /** 1381 * Swaps x[a] with x[b]. 1382 */ 1383 private static void swap(Object[] x, int a, int b) { 1384 Object t = x[a]; 1385 x[a] = x[b]; 1386 x[b] = t; 1387 } 1388 1389 /** 1390 * Sorts the specified array of objects according to the order induced by 1391 * the specified comparator. All elements in the array must be 1392 * <i>mutually comparable</i> by the specified comparator (that is, 1393 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1394 * for any elements {@code e1} and {@code e2} in the array). 1395 * 1396 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1397 * not be reordered as a result of the sort. 1398 * 1399 * <p>Implementation note: This implementation is a stable, adaptive, 1400 * iterative mergesort that requires far fewer than n lg(n) comparisons 1401 * when the input array is partially sorted, while offering the 1402 * performance of a traditional mergesort when the input array is 1403 * randomly ordered. If the input array is nearly sorted, the 1404 * implementation requires approximately n comparisons. Temporary 1405 * storage requirements vary from a small constant for nearly sorted 1406 * input arrays to n/2 object references for randomly ordered input 1407 * arrays. 1408 * 1409 * <p>The implementation takes equal advantage of ascending and 1410 * descending order in its input array, and can take advantage of 1411 * ascending and descending order in different parts of the same 1412 * input array. It is well-suited to merging two or more sorted arrays: 1413 * simply concatenate the arrays and sort the resulting array. 1414 * 1415 * <p>The implementation was adapted from Tim Peters's list sort for Python 1416 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1417 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1418 * Sorting and Information Theoretic Complexity", in Proceedings of the 1419 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1420 * January 1993. 1421 * 1422 * @param <T> the class of the objects to be sorted 1423 * @param a the array to be sorted 1424 * @param c the comparator to determine the order of the array. A 1425 * {@code null} value indicates that the elements' 1426 * {@linkplain Comparable natural ordering} should be used. 1427 * @throws ClassCastException if the array contains elements that are 1428 * not <i>mutually comparable</i> using the specified comparator 1429 * @throws IllegalArgumentException (optional) if the comparator is 1430 * found to violate the {@link Comparator} contract 1431 */ 1432 public static <T> void sort(T[] a, Comparator<? super T> c) { 1433 if (c == null) { 1434 sort(a); 1435 } else { 1436 if (LegacyMergeSort.userRequested) 1437 legacyMergeSort(a, c); 1438 else 1439 TimSort.sort(a, 0, a.length, c, null, 0, 0); 1440 } 1441 } 1442 1443 /** To be removed in a future release. */ 1444 private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { 1445 T[] aux = a.clone(); 1446 if (c==null) 1447 mergeSort(aux, a, 0, a.length, 0); 1448 else 1449 mergeSort(aux, a, 0, a.length, 0, c); 1450 } 1451 1452 /** 1453 * Sorts the specified range of the specified array of objects according 1454 * to the order induced by the specified comparator. The range to be 1455 * sorted extends from index {@code fromIndex}, inclusive, to index 1456 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1457 * range to be sorted is empty.) All elements in the range must be 1458 * <i>mutually comparable</i> by the specified comparator (that is, 1459 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1460 * for any elements {@code e1} and {@code e2} in the range). 1461 * 1462 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1463 * not be reordered as a result of the sort. 1464 * 1465 * <p>Implementation note: This implementation is a stable, adaptive, 1466 * iterative mergesort that requires far fewer than n lg(n) comparisons 1467 * when the input array is partially sorted, while offering the 1468 * performance of a traditional mergesort when the input array is 1469 * randomly ordered. If the input array is nearly sorted, the 1470 * implementation requires approximately n comparisons. Temporary 1471 * storage requirements vary from a small constant for nearly sorted 1472 * input arrays to n/2 object references for randomly ordered input 1473 * arrays. 1474 * 1475 * <p>The implementation takes equal advantage of ascending and 1476 * descending order in its input array, and can take advantage of 1477 * ascending and descending order in different parts of the same 1478 * input array. It is well-suited to merging two or more sorted arrays: 1479 * simply concatenate the arrays and sort the resulting array. 1480 * 1481 * <p>The implementation was adapted from Tim Peters's list sort for Python 1482 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 1483 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic 1484 * Sorting and Information Theoretic Complexity", in Proceedings of the 1485 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 1486 * January 1993. 1487 * 1488 * @param <T> the class of the objects to be sorted 1489 * @param a the array to be sorted 1490 * @param fromIndex the index of the first element (inclusive) to be 1491 * sorted 1492 * @param toIndex the index of the last element (exclusive) to be sorted 1493 * @param c the comparator to determine the order of the array. A 1494 * {@code null} value indicates that the elements' 1495 * {@linkplain Comparable natural ordering} should be used. 1496 * @throws ClassCastException if the array contains elements that are not 1497 * <i>mutually comparable</i> using the specified comparator. 1498 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1499 * (optional) if the comparator is found to violate the 1500 * {@link Comparator} contract 1501 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1502 * {@code toIndex > a.length} 1503 */ 1504 public static <T> void sort(T[] a, int fromIndex, int toIndex, 1505 Comparator<? super T> c) { 1506 if (c == null) { 1507 sort(a, fromIndex, toIndex); 1508 } else { 1509 rangeCheck(a.length, fromIndex, toIndex); 1510 if (LegacyMergeSort.userRequested) 1511 legacyMergeSort(a, fromIndex, toIndex, c); 1512 else 1513 TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0); 1514 } 1515 } 1516 1517 /** To be removed in a future release. */ 1518 private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex, 1519 Comparator<? super T> c) { 1520 T[] aux = copyOfRange(a, fromIndex, toIndex); 1521 if (c==null) 1522 mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 1523 else 1524 mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c); 1525 } 1526 1527 /** 1528 * Src is the source array that starts at index 0 1529 * Dest is the (possibly larger) array destination with a possible offset 1530 * low is the index in dest to start sorting 1531 * high is the end index in dest to end sorting 1532 * off is the offset into src corresponding to low in dest 1533 * To be removed in a future release. 1534 */ 1535 @SuppressWarnings({"rawtypes", "unchecked"}) 1536 private static void mergeSort(Object[] src, 1537 Object[] dest, 1538 int low, int high, int off, 1539 Comparator c) { 1540 int length = high - low; 1541 1542 // Insertion sort on smallest arrays 1543 if (length < INSERTIONSORT_THRESHOLD) { 1544 for (int i=low; i<high; i++) 1545 for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) 1546 swap(dest, j, j-1); 1547 return; 1548 } 1549 1550 // Recursively sort halves of dest into src 1551 int destLow = low; 1552 int destHigh = high; 1553 low += off; 1554 high += off; 1555 int mid = (low + high) >>> 1; 1556 mergeSort(dest, src, low, mid, -off, c); 1557 mergeSort(dest, src, mid, high, -off, c); 1558 1559 // If list is already sorted, just copy from src to dest. This is an 1560 // optimization that results in faster sorts for nearly ordered lists. 1561 if (c.compare(src[mid-1], src[mid]) <= 0) { 1562 System.arraycopy(src, low, dest, destLow, length); 1563 return; 1564 } 1565 1566 // Merge sorted halves (now in src) into dest 1567 for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 1568 if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) 1569 dest[i] = src[p++]; 1570 else 1571 dest[i] = src[q++]; 1572 } 1573 } 1574 1575 // Parallel prefix 1576 1577 /** 1578 * Cumulates, in parallel, each element of the given array in place, 1579 * using the supplied function. For example if the array initially 1580 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1581 * then upon return the array holds {@code [2, 3, 3, 6]}. 1582 * Parallel prefix computation is usually more efficient than 1583 * sequential loops for large arrays. 1584 * 1585 * @param <T> the class of the objects in the array 1586 * @param array the array, which is modified in-place by this method 1587 * @param op a side-effect-free, associative function to perform the 1588 * cumulation 1589 * @throws NullPointerException if the specified array or function is null 1590 * @since 1.8 1591 */ 1592 public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) { 1593 Objects.requireNonNull(op); 1594 if (array.length > 0) 1595 new ArrayPrefixHelpers.CumulateTask<> 1596 (null, op, array, 0, array.length).invoke(); 1597 } 1598 1599 /** 1600 * Performs {@link #parallelPrefix(Object[], BinaryOperator)} 1601 * for the given subrange of the array. 1602 * 1603 * @param <T> the class of the objects in the array 1604 * @param array the array 1605 * @param fromIndex the index of the first element, inclusive 1606 * @param toIndex the index of the last element, exclusive 1607 * @param op a side-effect-free, associative function to perform the 1608 * cumulation 1609 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1610 * @throws ArrayIndexOutOfBoundsException 1611 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1612 * @throws NullPointerException if the specified array or function is null 1613 * @since 1.8 1614 */ 1615 public static <T> void parallelPrefix(T[] array, int fromIndex, 1616 int toIndex, BinaryOperator<T> op) { 1617 Objects.requireNonNull(op); 1618 rangeCheck(array.length, fromIndex, toIndex); 1619 if (fromIndex < toIndex) 1620 new ArrayPrefixHelpers.CumulateTask<> 1621 (null, op, array, fromIndex, toIndex).invoke(); 1622 } 1623 1624 /** 1625 * Cumulates, in parallel, each element of the given array in place, 1626 * using the supplied function. For example if the array initially 1627 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1628 * then upon return the array holds {@code [2, 3, 3, 6]}. 1629 * Parallel prefix computation is usually more efficient than 1630 * sequential loops for large arrays. 1631 * 1632 * @param array the array, which is modified in-place by this method 1633 * @param op a side-effect-free, associative function to perform the 1634 * cumulation 1635 * @throws NullPointerException if the specified array or function is null 1636 * @since 1.8 1637 */ 1638 public static void parallelPrefix(long[] array, LongBinaryOperator op) { 1639 Objects.requireNonNull(op); 1640 if (array.length > 0) 1641 new ArrayPrefixHelpers.LongCumulateTask 1642 (null, op, array, 0, array.length).invoke(); 1643 } 1644 1645 /** 1646 * Performs {@link #parallelPrefix(long[], LongBinaryOperator)} 1647 * for the given subrange of the array. 1648 * 1649 * @param array the array 1650 * @param fromIndex the index of the first element, inclusive 1651 * @param toIndex the index of the last element, exclusive 1652 * @param op a side-effect-free, associative function to perform the 1653 * cumulation 1654 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1655 * @throws ArrayIndexOutOfBoundsException 1656 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1657 * @throws NullPointerException if the specified array or function is null 1658 * @since 1.8 1659 */ 1660 public static void parallelPrefix(long[] array, int fromIndex, 1661 int toIndex, LongBinaryOperator op) { 1662 Objects.requireNonNull(op); 1663 rangeCheck(array.length, fromIndex, toIndex); 1664 if (fromIndex < toIndex) 1665 new ArrayPrefixHelpers.LongCumulateTask 1666 (null, op, array, fromIndex, toIndex).invoke(); 1667 } 1668 1669 /** 1670 * Cumulates, in parallel, each element of the given array in place, 1671 * using the supplied function. For example if the array initially 1672 * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition, 1673 * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}. 1674 * Parallel prefix computation is usually more efficient than 1675 * sequential loops for large arrays. 1676 * 1677 * <p> Because floating-point operations may not be strictly associative, 1678 * the returned result may not be identical to the value that would be 1679 * obtained if the operation was performed sequentially. 1680 * 1681 * @param array the array, which is modified in-place by this method 1682 * @param op a side-effect-free function to perform the cumulation 1683 * @throws NullPointerException if the specified array or function is null 1684 * @since 1.8 1685 */ 1686 public static void parallelPrefix(double[] array, DoubleBinaryOperator op) { 1687 Objects.requireNonNull(op); 1688 if (array.length > 0) 1689 new ArrayPrefixHelpers.DoubleCumulateTask 1690 (null, op, array, 0, array.length).invoke(); 1691 } 1692 1693 /** 1694 * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)} 1695 * for the given subrange of the array. 1696 * 1697 * @param array the array 1698 * @param fromIndex the index of the first element, inclusive 1699 * @param toIndex the index of the last element, exclusive 1700 * @param op a side-effect-free, associative function to perform the 1701 * cumulation 1702 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1703 * @throws ArrayIndexOutOfBoundsException 1704 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1705 * @throws NullPointerException if the specified array or function is null 1706 * @since 1.8 1707 */ 1708 public static void parallelPrefix(double[] array, int fromIndex, 1709 int toIndex, DoubleBinaryOperator op) { 1710 Objects.requireNonNull(op); 1711 rangeCheck(array.length, fromIndex, toIndex); 1712 if (fromIndex < toIndex) 1713 new ArrayPrefixHelpers.DoubleCumulateTask 1714 (null, op, array, fromIndex, toIndex).invoke(); 1715 } 1716 1717 /** 1718 * Cumulates, in parallel, each element of the given array in place, 1719 * using the supplied function. For example if the array initially 1720 * holds {@code [2, 1, 0, 3]} and the operation performs addition, 1721 * then upon return the array holds {@code [2, 3, 3, 6]}. 1722 * Parallel prefix computation is usually more efficient than 1723 * sequential loops for large arrays. 1724 * 1725 * @param array the array, which is modified in-place by this method 1726 * @param op a side-effect-free, associative function to perform the 1727 * cumulation 1728 * @throws NullPointerException if the specified array or function is null 1729 * @since 1.8 1730 */ 1731 public static void parallelPrefix(int[] array, IntBinaryOperator op) { 1732 Objects.requireNonNull(op); 1733 if (array.length > 0) 1734 new ArrayPrefixHelpers.IntCumulateTask 1735 (null, op, array, 0, array.length).invoke(); 1736 } 1737 1738 /** 1739 * Performs {@link #parallelPrefix(int[], IntBinaryOperator)} 1740 * for the given subrange of the array. 1741 * 1742 * @param array the array 1743 * @param fromIndex the index of the first element, inclusive 1744 * @param toIndex the index of the last element, exclusive 1745 * @param op a side-effect-free, associative function to perform the 1746 * cumulation 1747 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 1748 * @throws ArrayIndexOutOfBoundsException 1749 * if {@code fromIndex < 0} or {@code toIndex > array.length} 1750 * @throws NullPointerException if the specified array or function is null 1751 * @since 1.8 1752 */ 1753 public static void parallelPrefix(int[] array, int fromIndex, 1754 int toIndex, IntBinaryOperator op) { 1755 Objects.requireNonNull(op); 1756 rangeCheck(array.length, fromIndex, toIndex); 1757 if (fromIndex < toIndex) 1758 new ArrayPrefixHelpers.IntCumulateTask 1759 (null, op, array, fromIndex, toIndex).invoke(); 1760 } 1761 1762 // Searching 1763 1764 /** 1765 * Searches the specified array of longs for the specified value using the 1766 * binary search algorithm. The array must be sorted (as 1767 * by the {@link #sort(long[])} method) prior to making this call. If it 1768 * is not sorted, the results are undefined. If the array contains 1769 * multiple elements with the specified value, there is no guarantee which 1770 * one will be found. 1771 * 1772 * @param a the array to be searched 1773 * @param key the value to be searched for 1774 * @return index of the search key, if it is contained in the array; 1775 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1776 * <i>insertion point</i> is defined as the point at which the 1777 * key would be inserted into the array: the index of the first 1778 * element greater than the key, or <tt>a.length</tt> if all 1779 * elements in the array are less than the specified key. Note 1780 * that this guarantees that the return value will be >= 0 if 1781 * and only if the key is found. 1782 */ 1783 public static int binarySearch(long[] a, long key) { 1784 return binarySearch0(a, 0, a.length, key); 1785 } 1786 1787 /** 1788 * Searches a range of 1789 * the specified array of longs for the specified value using the 1790 * binary search algorithm. 1791 * The range must be sorted (as 1792 * by the {@link #sort(long[], int, int)} method) 1793 * prior to making this call. If it 1794 * is not sorted, the results are undefined. If the range contains 1795 * multiple elements with the specified value, there is no guarantee which 1796 * one will be found. 1797 * 1798 * @param a the array to be searched 1799 * @param fromIndex the index of the first element (inclusive) to be 1800 * searched 1801 * @param toIndex the index of the last element (exclusive) to be searched 1802 * @param key the value to be searched for 1803 * @return index of the search key, if it is contained in the array 1804 * within the specified range; 1805 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1806 * <i>insertion point</i> is defined as the point at which the 1807 * key would be inserted into the array: the index of the first 1808 * element in the range greater than the key, 1809 * or <tt>toIndex</tt> if all 1810 * elements in the range are less than the specified key. Note 1811 * that this guarantees that the return value will be >= 0 if 1812 * and only if the key is found. 1813 * @throws IllegalArgumentException 1814 * if {@code fromIndex > toIndex} 1815 * @throws ArrayIndexOutOfBoundsException 1816 * if {@code fromIndex < 0 or toIndex > a.length} 1817 * @since 1.6 1818 */ 1819 public static int binarySearch(long[] a, int fromIndex, int toIndex, 1820 long key) { 1821 rangeCheck(a.length, fromIndex, toIndex); 1822 return binarySearch0(a, fromIndex, toIndex, key); 1823 } 1824 1825 // Like public version, but without range checks. 1826 private static int binarySearch0(long[] a, int fromIndex, int toIndex, 1827 long key) { 1828 int low = fromIndex; 1829 int high = toIndex - 1; 1830 1831 while (low <= high) { 1832 int mid = (low + high) >>> 1; 1833 long midVal = a[mid]; 1834 1835 if (midVal < key) 1836 low = mid + 1; 1837 else if (midVal > key) 1838 high = mid - 1; 1839 else 1840 return mid; // key found 1841 } 1842 return -(low + 1); // key not found. 1843 } 1844 1845 /** 1846 * Searches the specified array of ints for the specified value using the 1847 * binary search algorithm. The array must be sorted (as 1848 * by the {@link #sort(int[])} method) prior to making this call. If it 1849 * is not sorted, the results are undefined. If the array contains 1850 * multiple elements with the specified value, there is no guarantee which 1851 * one will be found. 1852 * 1853 * @param a the array to be searched 1854 * @param key the value to be searched for 1855 * @return index of the search key, if it is contained in the array; 1856 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1857 * <i>insertion point</i> is defined as the point at which the 1858 * key would be inserted into the array: the index of the first 1859 * element greater than the key, or <tt>a.length</tt> if all 1860 * elements in the array are less than the specified key. Note 1861 * that this guarantees that the return value will be >= 0 if 1862 * and only if the key is found. 1863 */ 1864 public static int binarySearch(int[] a, int key) { 1865 return binarySearch0(a, 0, a.length, key); 1866 } 1867 1868 /** 1869 * Searches a range of 1870 * the specified array of ints for the specified value using the 1871 * binary search algorithm. 1872 * The range must be sorted (as 1873 * by the {@link #sort(int[], int, int)} method) 1874 * prior to making this call. If it 1875 * is not sorted, the results are undefined. If the range contains 1876 * multiple elements with the specified value, there is no guarantee which 1877 * one will be found. 1878 * 1879 * @param a the array to be searched 1880 * @param fromIndex the index of the first element (inclusive) to be 1881 * searched 1882 * @param toIndex the index of the last element (exclusive) to be searched 1883 * @param key the value to be searched for 1884 * @return index of the search key, if it is contained in the array 1885 * within the specified range; 1886 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1887 * <i>insertion point</i> is defined as the point at which the 1888 * key would be inserted into the array: the index of the first 1889 * element in the range greater than the key, 1890 * or <tt>toIndex</tt> if all 1891 * elements in the range are less than the specified key. Note 1892 * that this guarantees that the return value will be >= 0 if 1893 * and only if the key is found. 1894 * @throws IllegalArgumentException 1895 * if {@code fromIndex > toIndex} 1896 * @throws ArrayIndexOutOfBoundsException 1897 * if {@code fromIndex < 0 or toIndex > a.length} 1898 * @since 1.6 1899 */ 1900 public static int binarySearch(int[] a, int fromIndex, int toIndex, 1901 int key) { 1902 rangeCheck(a.length, fromIndex, toIndex); 1903 return binarySearch0(a, fromIndex, toIndex, key); 1904 } 1905 1906 // Like public version, but without range checks. 1907 private static int binarySearch0(int[] a, int fromIndex, int toIndex, 1908 int key) { 1909 int low = fromIndex; 1910 int high = toIndex - 1; 1911 1912 while (low <= high) { 1913 int mid = (low + high) >>> 1; 1914 int midVal = a[mid]; 1915 1916 if (midVal < key) 1917 low = mid + 1; 1918 else if (midVal > key) 1919 high = mid - 1; 1920 else 1921 return mid; // key found 1922 } 1923 return -(low + 1); // key not found. 1924 } 1925 1926 /** 1927 * Searches the specified array of shorts for the specified value using 1928 * the binary search algorithm. The array must be sorted 1929 * (as by the {@link #sort(short[])} method) prior to making this call. If 1930 * it is not sorted, the results are undefined. If the array contains 1931 * multiple elements with the specified value, there is no guarantee which 1932 * one will be found. 1933 * 1934 * @param a the array to be searched 1935 * @param key the value to be searched for 1936 * @return index of the search key, if it is contained in the array; 1937 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1938 * <i>insertion point</i> is defined as the point at which the 1939 * key would be inserted into the array: the index of the first 1940 * element greater than the key, or <tt>a.length</tt> if all 1941 * elements in the array are less than the specified key. Note 1942 * that this guarantees that the return value will be >= 0 if 1943 * and only if the key is found. 1944 */ 1945 public static int binarySearch(short[] a, short key) { 1946 return binarySearch0(a, 0, a.length, key); 1947 } 1948 1949 /** 1950 * Searches a range of 1951 * the specified array of shorts for the specified value using 1952 * the binary search algorithm. 1953 * The range must be sorted 1954 * (as by the {@link #sort(short[], int, int)} method) 1955 * prior to making this call. If 1956 * it is not sorted, the results are undefined. If the range contains 1957 * multiple elements with the specified value, there is no guarantee which 1958 * one will be found. 1959 * 1960 * @param a the array to be searched 1961 * @param fromIndex the index of the first element (inclusive) to be 1962 * searched 1963 * @param toIndex the index of the last element (exclusive) to be searched 1964 * @param key the value to be searched for 1965 * @return index of the search key, if it is contained in the array 1966 * within the specified range; 1967 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 1968 * <i>insertion point</i> is defined as the point at which the 1969 * key would be inserted into the array: the index of the first 1970 * element in the range greater than the key, 1971 * or <tt>toIndex</tt> if all 1972 * elements in the range are less than the specified key. Note 1973 * that this guarantees that the return value will be >= 0 if 1974 * and only if the key is found. 1975 * @throws IllegalArgumentException 1976 * if {@code fromIndex > toIndex} 1977 * @throws ArrayIndexOutOfBoundsException 1978 * if {@code fromIndex < 0 or toIndex > a.length} 1979 * @since 1.6 1980 */ 1981 public static int binarySearch(short[] a, int fromIndex, int toIndex, 1982 short key) { 1983 rangeCheck(a.length, fromIndex, toIndex); 1984 return binarySearch0(a, fromIndex, toIndex, key); 1985 } 1986 1987 // Like public version, but without range checks. 1988 private static int binarySearch0(short[] a, int fromIndex, int toIndex, 1989 short key) { 1990 int low = fromIndex; 1991 int high = toIndex - 1; 1992 1993 while (low <= high) { 1994 int mid = (low + high) >>> 1; 1995 short midVal = a[mid]; 1996 1997 if (midVal < key) 1998 low = mid + 1; 1999 else if (midVal > key) 2000 high = mid - 1; 2001 else 2002 return mid; // key found 2003 } 2004 return -(low + 1); // key not found. 2005 } 2006 2007 /** 2008 * Searches the specified array of chars for the specified value using the 2009 * binary search algorithm. The array must be sorted (as 2010 * by the {@link #sort(char[])} method) prior to making this call. If it 2011 * is not sorted, the results are undefined. If the array contains 2012 * multiple elements with the specified value, there is no guarantee which 2013 * one will be found. 2014 * 2015 * @param a the array to be searched 2016 * @param key the value to be searched for 2017 * @return index of the search key, if it is contained in the array; 2018 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2019 * <i>insertion point</i> is defined as the point at which the 2020 * key would be inserted into the array: the index of the first 2021 * element greater than the key, or <tt>a.length</tt> if all 2022 * elements in the array are less than the specified key. Note 2023 * that this guarantees that the return value will be >= 0 if 2024 * and only if the key is found. 2025 */ 2026 public static int binarySearch(char[] a, char key) { 2027 return binarySearch0(a, 0, a.length, key); 2028 } 2029 2030 /** 2031 * Searches a range of 2032 * the specified array of chars for the specified value using the 2033 * binary search algorithm. 2034 * The range must be sorted (as 2035 * by the {@link #sort(char[], int, int)} method) 2036 * prior to making this call. If it 2037 * is not sorted, the results are undefined. If the range contains 2038 * multiple elements with the specified value, there is no guarantee which 2039 * one will be found. 2040 * 2041 * @param a the array to be searched 2042 * @param fromIndex the index of the first element (inclusive) to be 2043 * searched 2044 * @param toIndex the index of the last element (exclusive) to be searched 2045 * @param key the value to be searched for 2046 * @return index of the search key, if it is contained in the array 2047 * within the specified range; 2048 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2049 * <i>insertion point</i> is defined as the point at which the 2050 * key would be inserted into the array: the index of the first 2051 * element in the range greater than the key, 2052 * or <tt>toIndex</tt> if all 2053 * elements in the range are less than the specified key. Note 2054 * that this guarantees that the return value will be >= 0 if 2055 * and only if the key is found. 2056 * @throws IllegalArgumentException 2057 * if {@code fromIndex > toIndex} 2058 * @throws ArrayIndexOutOfBoundsException 2059 * if {@code fromIndex < 0 or toIndex > a.length} 2060 * @since 1.6 2061 */ 2062 public static int binarySearch(char[] a, int fromIndex, int toIndex, 2063 char key) { 2064 rangeCheck(a.length, fromIndex, toIndex); 2065 return binarySearch0(a, fromIndex, toIndex, key); 2066 } 2067 2068 // Like public version, but without range checks. 2069 private static int binarySearch0(char[] a, int fromIndex, int toIndex, 2070 char key) { 2071 int low = fromIndex; 2072 int high = toIndex - 1; 2073 2074 while (low <= high) { 2075 int mid = (low + high) >>> 1; 2076 char midVal = a[mid]; 2077 2078 if (midVal < key) 2079 low = mid + 1; 2080 else if (midVal > key) 2081 high = mid - 1; 2082 else 2083 return mid; // key found 2084 } 2085 return -(low + 1); // key not found. 2086 } 2087 2088 /** 2089 * Searches the specified array of bytes for the specified value using the 2090 * binary search algorithm. The array must be sorted (as 2091 * by the {@link #sort(byte[])} method) prior to making this call. If it 2092 * is not sorted, the results are undefined. If the array contains 2093 * multiple elements with the specified value, there is no guarantee which 2094 * one will be found. 2095 * 2096 * @param a the array to be searched 2097 * @param key the value to be searched for 2098 * @return index of the search key, if it is contained in the array; 2099 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2100 * <i>insertion point</i> is defined as the point at which the 2101 * key would be inserted into the array: the index of the first 2102 * element greater than the key, or <tt>a.length</tt> if all 2103 * elements in the array are less than the specified key. Note 2104 * that this guarantees that the return value will be >= 0 if 2105 * and only if the key is found. 2106 */ 2107 public static int binarySearch(byte[] a, byte key) { 2108 return binarySearch0(a, 0, a.length, key); 2109 } 2110 2111 /** 2112 * Searches a range of 2113 * the specified array of bytes for the specified value using the 2114 * binary search algorithm. 2115 * The range must be sorted (as 2116 * by the {@link #sort(byte[], int, int)} method) 2117 * prior to making this call. If it 2118 * is not sorted, the results are undefined. If the range contains 2119 * multiple elements with the specified value, there is no guarantee which 2120 * one will be found. 2121 * 2122 * @param a the array to be searched 2123 * @param fromIndex the index of the first element (inclusive) to be 2124 * searched 2125 * @param toIndex the index of the last element (exclusive) to be searched 2126 * @param key the value to be searched for 2127 * @return index of the search key, if it is contained in the array 2128 * within the specified range; 2129 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2130 * <i>insertion point</i> is defined as the point at which the 2131 * key would be inserted into the array: the index of the first 2132 * element in the range greater than the key, 2133 * or <tt>toIndex</tt> if all 2134 * elements in the range are less than the specified key. Note 2135 * that this guarantees that the return value will be >= 0 if 2136 * and only if the key is found. 2137 * @throws IllegalArgumentException 2138 * if {@code fromIndex > toIndex} 2139 * @throws ArrayIndexOutOfBoundsException 2140 * if {@code fromIndex < 0 or toIndex > a.length} 2141 * @since 1.6 2142 */ 2143 public static int binarySearch(byte[] a, int fromIndex, int toIndex, 2144 byte key) { 2145 rangeCheck(a.length, fromIndex, toIndex); 2146 return binarySearch0(a, fromIndex, toIndex, key); 2147 } 2148 2149 // Like public version, but without range checks. 2150 private static int binarySearch0(byte[] a, int fromIndex, int toIndex, 2151 byte key) { 2152 int low = fromIndex; 2153 int high = toIndex - 1; 2154 2155 while (low <= high) { 2156 int mid = (low + high) >>> 1; 2157 byte midVal = a[mid]; 2158 2159 if (midVal < key) 2160 low = mid + 1; 2161 else if (midVal > key) 2162 high = mid - 1; 2163 else 2164 return mid; // key found 2165 } 2166 return -(low + 1); // key not found. 2167 } 2168 2169 /** 2170 * Searches the specified array of doubles for the specified value using 2171 * the binary search algorithm. The array must be sorted 2172 * (as by the {@link #sort(double[])} method) prior to making this call. 2173 * If it is not sorted, the results are undefined. If the array contains 2174 * multiple elements with the specified value, there is no guarantee which 2175 * one will be found. This method considers all NaN values to be 2176 * equivalent and equal. 2177 * 2178 * @param a the array to be searched 2179 * @param key the value to be searched for 2180 * @return index of the search key, if it is contained in the array; 2181 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2182 * <i>insertion point</i> is defined as the point at which the 2183 * key would be inserted into the array: the index of the first 2184 * element greater than the key, or <tt>a.length</tt> if all 2185 * elements in the array are less than the specified key. Note 2186 * that this guarantees that the return value will be >= 0 if 2187 * and only if the key is found. 2188 */ 2189 public static int binarySearch(double[] a, double key) { 2190 return binarySearch0(a, 0, a.length, key); 2191 } 2192 2193 /** 2194 * Searches a range of 2195 * the specified array of doubles for the specified value using 2196 * the binary search algorithm. 2197 * The range must be sorted 2198 * (as by the {@link #sort(double[], int, int)} method) 2199 * prior to making this call. 2200 * If it is not sorted, the results are undefined. If the range contains 2201 * multiple elements with the specified value, there is no guarantee which 2202 * one will be found. This method considers all NaN values to be 2203 * equivalent and equal. 2204 * 2205 * @param a the array to be searched 2206 * @param fromIndex the index of the first element (inclusive) to be 2207 * searched 2208 * @param toIndex the index of the last element (exclusive) to be searched 2209 * @param key the value to be searched for 2210 * @return index of the search key, if it is contained in the array 2211 * within the specified range; 2212 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2213 * <i>insertion point</i> is defined as the point at which the 2214 * key would be inserted into the array: the index of the first 2215 * element in the range greater than the key, 2216 * or <tt>toIndex</tt> if all 2217 * elements in the range are less than the specified key. Note 2218 * that this guarantees that the return value will be >= 0 if 2219 * and only if the key is found. 2220 * @throws IllegalArgumentException 2221 * if {@code fromIndex > toIndex} 2222 * @throws ArrayIndexOutOfBoundsException 2223 * if {@code fromIndex < 0 or toIndex > a.length} 2224 * @since 1.6 2225 */ 2226 public static int binarySearch(double[] a, int fromIndex, int toIndex, 2227 double key) { 2228 rangeCheck(a.length, fromIndex, toIndex); 2229 return binarySearch0(a, fromIndex, toIndex, key); 2230 } 2231 2232 // Like public version, but without range checks. 2233 private static int binarySearch0(double[] a, int fromIndex, int toIndex, 2234 double key) { 2235 int low = fromIndex; 2236 int high = toIndex - 1; 2237 2238 while (low <= high) { 2239 int mid = (low + high) >>> 1; 2240 double midVal = a[mid]; 2241 2242 if (midVal < key) 2243 low = mid + 1; // Neither val is NaN, thisVal is smaller 2244 else if (midVal > key) 2245 high = mid - 1; // Neither val is NaN, thisVal is larger 2246 else { 2247 long midBits = Double.doubleToLongBits(midVal); 2248 long keyBits = Double.doubleToLongBits(key); 2249 if (midBits == keyBits) // Values are equal 2250 return mid; // Key found 2251 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 2252 low = mid + 1; 2253 else // (0.0, -0.0) or (NaN, !NaN) 2254 high = mid - 1; 2255 } 2256 } 2257 return -(low + 1); // key not found. 2258 } 2259 2260 /** 2261 * Searches the specified array of floats for the specified value using 2262 * the binary search algorithm. The array must be sorted 2263 * (as by the {@link #sort(float[])} method) prior to making this call. If 2264 * it is not sorted, the results are undefined. If the array contains 2265 * multiple elements with the specified value, there is no guarantee which 2266 * one will be found. This method considers all NaN values to be 2267 * equivalent and equal. 2268 * 2269 * @param a the array to be searched 2270 * @param key the value to be searched for 2271 * @return index of the search key, if it is contained in the array; 2272 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2273 * <i>insertion point</i> is defined as the point at which the 2274 * key would be inserted into the array: the index of the first 2275 * element greater than the key, or <tt>a.length</tt> if all 2276 * elements in the array are less than the specified key. Note 2277 * that this guarantees that the return value will be >= 0 if 2278 * and only if the key is found. 2279 */ 2280 public static int binarySearch(float[] a, float key) { 2281 return binarySearch0(a, 0, a.length, key); 2282 } 2283 2284 /** 2285 * Searches a range of 2286 * the specified array of floats for the specified value using 2287 * the binary search algorithm. 2288 * The range must be sorted 2289 * (as by the {@link #sort(float[], int, int)} method) 2290 * prior to making this call. If 2291 * it is not sorted, the results are undefined. If the range contains 2292 * multiple elements with the specified value, there is no guarantee which 2293 * one will be found. This method considers all NaN values to be 2294 * equivalent and equal. 2295 * 2296 * @param a the array to be searched 2297 * @param fromIndex the index of the first element (inclusive) to be 2298 * searched 2299 * @param toIndex the index of the last element (exclusive) to be searched 2300 * @param key the value to be searched for 2301 * @return index of the search key, if it is contained in the array 2302 * within the specified range; 2303 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2304 * <i>insertion point</i> is defined as the point at which the 2305 * key would be inserted into the array: the index of the first 2306 * element in the range greater than the key, 2307 * or <tt>toIndex</tt> if all 2308 * elements in the range are less than the specified key. Note 2309 * that this guarantees that the return value will be >= 0 if 2310 * and only if the key is found. 2311 * @throws IllegalArgumentException 2312 * if {@code fromIndex > toIndex} 2313 * @throws ArrayIndexOutOfBoundsException 2314 * if {@code fromIndex < 0 or toIndex > a.length} 2315 * @since 1.6 2316 */ 2317 public static int binarySearch(float[] a, int fromIndex, int toIndex, 2318 float key) { 2319 rangeCheck(a.length, fromIndex, toIndex); 2320 return binarySearch0(a, fromIndex, toIndex, key); 2321 } 2322 2323 // Like public version, but without range checks. 2324 private static int binarySearch0(float[] a, int fromIndex, int toIndex, 2325 float key) { 2326 int low = fromIndex; 2327 int high = toIndex - 1; 2328 2329 while (low <= high) { 2330 int mid = (low + high) >>> 1; 2331 float midVal = a[mid]; 2332 2333 if (midVal < key) 2334 low = mid + 1; // Neither val is NaN, thisVal is smaller 2335 else if (midVal > key) 2336 high = mid - 1; // Neither val is NaN, thisVal is larger 2337 else { 2338 int midBits = Float.floatToIntBits(midVal); 2339 int keyBits = Float.floatToIntBits(key); 2340 if (midBits == keyBits) // Values are equal 2341 return mid; // Key found 2342 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 2343 low = mid + 1; 2344 else // (0.0, -0.0) or (NaN, !NaN) 2345 high = mid - 1; 2346 } 2347 } 2348 return -(low + 1); // key not found. 2349 } 2350 2351 /** 2352 * Searches the specified array for the specified object using the binary 2353 * search algorithm. The array must be sorted into ascending order 2354 * according to the 2355 * {@linkplain Comparable natural ordering} 2356 * of its elements (as by the 2357 * {@link #sort(Object[])} method) prior to making this call. 2358 * If it is not sorted, the results are undefined. 2359 * (If the array contains elements that are not mutually comparable (for 2360 * example, strings and integers), it <i>cannot</i> be sorted according 2361 * to the natural ordering of its elements, hence results are undefined.) 2362 * If the array contains multiple 2363 * elements equal to the specified object, there is no guarantee which 2364 * one will be found. 2365 * 2366 * @param a the array to be searched 2367 * @param key the value to be searched for 2368 * @return index of the search key, if it is contained in the array; 2369 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2370 * <i>insertion point</i> is defined as the point at which the 2371 * key would be inserted into the array: the index of the first 2372 * element greater than the key, or <tt>a.length</tt> if all 2373 * elements in the array are less than the specified key. Note 2374 * that this guarantees that the return value will be >= 0 if 2375 * and only if the key is found. 2376 * @throws ClassCastException if the search key is not comparable to the 2377 * elements of the array. 2378 */ 2379 public static int binarySearch(Object[] a, Object key) { 2380 return binarySearch0(a, 0, a.length, key); 2381 } 2382 2383 /** 2384 * Searches a range of 2385 * the specified array for the specified object using the binary 2386 * search algorithm. 2387 * The range must be sorted into ascending order 2388 * according to the 2389 * {@linkplain Comparable natural ordering} 2390 * of its elements (as by the 2391 * {@link #sort(Object[], int, int)} method) prior to making this 2392 * call. If it is not sorted, the results are undefined. 2393 * (If the range contains elements that are not mutually comparable (for 2394 * example, strings and integers), it <i>cannot</i> be sorted according 2395 * to the natural ordering of its elements, hence results are undefined.) 2396 * If the range contains multiple 2397 * elements equal to the specified object, there is no guarantee which 2398 * one will be found. 2399 * 2400 * @param a the array to be searched 2401 * @param fromIndex the index of the first element (inclusive) to be 2402 * searched 2403 * @param toIndex the index of the last element (exclusive) to be searched 2404 * @param key the value to be searched for 2405 * @return index of the search key, if it is contained in the array 2406 * within the specified range; 2407 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2408 * <i>insertion point</i> is defined as the point at which the 2409 * key would be inserted into the array: the index of the first 2410 * element in the range greater than the key, 2411 * or <tt>toIndex</tt> if all 2412 * elements in the range are less than the specified key. Note 2413 * that this guarantees that the return value will be >= 0 if 2414 * and only if the key is found. 2415 * @throws ClassCastException if the search key is not comparable to the 2416 * elements of the array within the specified range. 2417 * @throws IllegalArgumentException 2418 * if {@code fromIndex > toIndex} 2419 * @throws ArrayIndexOutOfBoundsException 2420 * if {@code fromIndex < 0 or toIndex > a.length} 2421 * @since 1.6 2422 */ 2423 public static int binarySearch(Object[] a, int fromIndex, int toIndex, 2424 Object key) { 2425 rangeCheck(a.length, fromIndex, toIndex); 2426 return binarySearch0(a, fromIndex, toIndex, key); 2427 } 2428 2429 // Like public version, but without range checks. 2430 private static int binarySearch0(Object[] a, int fromIndex, int toIndex, 2431 Object key) { 2432 int low = fromIndex; 2433 int high = toIndex - 1; 2434 2435 while (low <= high) { 2436 int mid = (low + high) >>> 1; 2437 @SuppressWarnings("rawtypes") 2438 Comparable midVal = (Comparable)a[mid]; 2439 @SuppressWarnings("unchecked") 2440 int cmp = midVal.compareTo(key); 2441 2442 if (cmp < 0) 2443 low = mid + 1; 2444 else if (cmp > 0) 2445 high = mid - 1; 2446 else 2447 return mid; // key found 2448 } 2449 return -(low + 1); // key not found. 2450 } 2451 2452 /** 2453 * Searches the specified array for the specified object using the binary 2454 * search algorithm. The array must be sorted into ascending order 2455 * according to the specified comparator (as by the 2456 * {@link #sort(Object[], Comparator) sort(T[], Comparator)} 2457 * method) prior to making this call. If it is 2458 * not sorted, the results are undefined. 2459 * If the array contains multiple 2460 * elements equal to the specified object, there is no guarantee which one 2461 * will be found. 2462 * 2463 * @param <T> the class of the objects in the array 2464 * @param a the array to be searched 2465 * @param key the value to be searched for 2466 * @param c the comparator by which the array is ordered. A 2467 * <tt>null</tt> value indicates that the elements' 2468 * {@linkplain Comparable natural ordering} should be used. 2469 * @return index of the search key, if it is contained in the array; 2470 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2471 * <i>insertion point</i> is defined as the point at which the 2472 * key would be inserted into the array: the index of the first 2473 * element greater than the key, or <tt>a.length</tt> if all 2474 * elements in the array are less than the specified key. Note 2475 * that this guarantees that the return value will be >= 0 if 2476 * and only if the key is found. 2477 * @throws ClassCastException if the array contains elements that are not 2478 * <i>mutually comparable</i> using the specified comparator, 2479 * or the search key is not comparable to the 2480 * elements of the array using this comparator. 2481 */ 2482 public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) { 2483 return binarySearch0(a, 0, a.length, key, c); 2484 } 2485 2486 /** 2487 * Searches a range of 2488 * the specified array for the specified object using the binary 2489 * search algorithm. 2490 * The range must be sorted into ascending order 2491 * according to the specified comparator (as by the 2492 * {@link #sort(Object[], int, int, Comparator) 2493 * sort(T[], int, int, Comparator)} 2494 * method) prior to making this call. 2495 * If it is not sorted, the results are undefined. 2496 * If the range contains multiple elements equal to the specified object, 2497 * there is no guarantee which one will be found. 2498 * 2499 * @param <T> the class of the objects in the array 2500 * @param a the array to be searched 2501 * @param fromIndex the index of the first element (inclusive) to be 2502 * searched 2503 * @param toIndex the index of the last element (exclusive) to be searched 2504 * @param key the value to be searched for 2505 * @param c the comparator by which the array is ordered. A 2506 * <tt>null</tt> value indicates that the elements' 2507 * {@linkplain Comparable natural ordering} should be used. 2508 * @return index of the search key, if it is contained in the array 2509 * within the specified range; 2510 * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 2511 * <i>insertion point</i> is defined as the point at which the 2512 * key would be inserted into the array: the index of the first 2513 * element in the range greater than the key, 2514 * or <tt>toIndex</tt> if all 2515 * elements in the range are less than the specified key. Note 2516 * that this guarantees that the return value will be >= 0 if 2517 * and only if the key is found. 2518 * @throws ClassCastException if the range contains elements that are not 2519 * <i>mutually comparable</i> using the specified comparator, 2520 * or the search key is not comparable to the 2521 * elements in the range using this comparator. 2522 * @throws IllegalArgumentException 2523 * if {@code fromIndex > toIndex} 2524 * @throws ArrayIndexOutOfBoundsException 2525 * if {@code fromIndex < 0 or toIndex > a.length} 2526 * @since 1.6 2527 */ 2528 public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, 2529 T key, Comparator<? super T> c) { 2530 rangeCheck(a.length, fromIndex, toIndex); 2531 return binarySearch0(a, fromIndex, toIndex, key, c); 2532 } 2533 2534 // Like public version, but without range checks. 2535 private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex, 2536 T key, Comparator<? super T> c) { 2537 if (c == null) { 2538 return binarySearch0(a, fromIndex, toIndex, key); 2539 } 2540 int low = fromIndex; 2541 int high = toIndex - 1; 2542 2543 while (low <= high) { 2544 int mid = (low + high) >>> 1; 2545 T midVal = a[mid]; 2546 int cmp = c.compare(midVal, key); 2547 if (cmp < 0) 2548 low = mid + 1; 2549 else if (cmp > 0) 2550 high = mid - 1; 2551 else 2552 return mid; // key found 2553 } 2554 return -(low + 1); // key not found. 2555 } 2556 2557 // Equality Testing 2558 2559 /** 2560 * Returns <tt>true</tt> if the two specified arrays of longs are 2561 * <i>equal</i> to one another. Two arrays are considered equal if both 2562 * arrays contain the same number of elements, and all corresponding pairs 2563 * of elements in the two arrays are equal. In other words, two arrays 2564 * are equal if they contain the same elements in the same order. Also, 2565 * two array references are considered equal if both are <tt>null</tt>. 2566 * 2567 * @param a one array to be tested for equality 2568 * @param a2 the other array to be tested for equality 2569 * @return <tt>true</tt> if the two arrays are equal 2570 */ 2571 public static boolean equals(long[] a, long[] a2) { 2572 if (a==a2) 2573 return true; 2574 if (a==null || a2==null) 2575 return false; 2576 2577 int length = a.length; 2578 if (a2.length != length) 2579 return false; 2580 2581 for (int i=0; i<length; i++) 2582 if (a[i] != a2[i]) 2583 return false; 2584 2585 return true; 2586 } 2587 2588 /** 2589 * Returns <tt>true</tt> if the two specified arrays of ints are 2590 * <i>equal</i> to one another. Two arrays are considered equal if both 2591 * arrays contain the same number of elements, and all corresponding pairs 2592 * of elements in the two arrays are equal. In other words, two arrays 2593 * are equal if they contain the same elements in the same order. Also, 2594 * two array references are considered equal if both are <tt>null</tt>. 2595 * 2596 * @param a one array to be tested for equality 2597 * @param a2 the other array to be tested for equality 2598 * @return <tt>true</tt> if the two arrays are equal 2599 */ 2600 public static boolean equals(int[] a, int[] a2) { 2601 if (a==a2) 2602 return true; 2603 if (a==null || a2==null) 2604 return false; 2605 2606 int length = a.length; 2607 if (a2.length != length) 2608 return false; 2609 2610 for (int i=0; i<length; i++) 2611 if (a[i] != a2[i]) 2612 return false; 2613 2614 return true; 2615 } 2616 2617 /** 2618 * Returns <tt>true</tt> if the two specified arrays of shorts are 2619 * <i>equal</i> to one another. Two arrays are considered equal if both 2620 * arrays contain the same number of elements, and all corresponding pairs 2621 * of elements in the two arrays are equal. In other words, two arrays 2622 * are equal if they contain the same elements in the same order. Also, 2623 * two array references are considered equal if both are <tt>null</tt>. 2624 * 2625 * @param a one array to be tested for equality 2626 * @param a2 the other array to be tested for equality 2627 * @return <tt>true</tt> if the two arrays are equal 2628 */ 2629 public static boolean equals(short[] a, short a2[]) { 2630 if (a==a2) 2631 return true; 2632 if (a==null || a2==null) 2633 return false; 2634 2635 int length = a.length; 2636 if (a2.length != length) 2637 return false; 2638 2639 for (int i=0; i<length; i++) 2640 if (a[i] != a2[i]) 2641 return false; 2642 2643 return true; 2644 } 2645 2646 /** 2647 * Returns <tt>true</tt> if the two specified arrays of chars are 2648 * <i>equal</i> to one another. Two arrays are considered equal if both 2649 * arrays contain the same number of elements, and all corresponding pairs 2650 * of elements in the two arrays are equal. In other words, two arrays 2651 * are equal if they contain the same elements in the same order. Also, 2652 * two array references are considered equal if both are <tt>null</tt>. 2653 * 2654 * @param a one array to be tested for equality 2655 * @param a2 the other array to be tested for equality 2656 * @return <tt>true</tt> if the two arrays are equal 2657 */ 2658 @HotSpotIntrinsicCandidate 2659 public static boolean equals(char[] a, char[] a2) { 2660 if (a==a2) 2661 return true; 2662 if (a==null || a2==null) 2663 return false; 2664 2665 int length = a.length; 2666 if (a2.length != length) 2667 return false; 2668 2669 for (int i=0; i<length; i++) 2670 if (a[i] != a2[i]) 2671 return false; 2672 2673 return true; 2674 } 2675 2676 /** 2677 * Returns <tt>true</tt> if the two specified arrays of bytes are 2678 * <i>equal</i> to one another. Two arrays are considered equal if both 2679 * arrays contain the same number of elements, and all corresponding pairs 2680 * of elements in the two arrays are equal. In other words, two arrays 2681 * are equal if they contain the same elements in the same order. Also, 2682 * two array references are considered equal if both are <tt>null</tt>. 2683 * 2684 * @param a one array to be tested for equality 2685 * @param a2 the other array to be tested for equality 2686 * @return <tt>true</tt> if the two arrays are equal 2687 */ 2688 public static boolean equals(byte[] a, byte[] a2) { 2689 if (a==a2) 2690 return true; 2691 if (a==null || a2==null) 2692 return false; 2693 2694 int length = a.length; 2695 if (a2.length != length) 2696 return false; 2697 2698 for (int i=0; i<length; i++) 2699 if (a[i] != a2[i]) 2700 return false; 2701 2702 return true; 2703 } 2704 2705 /** 2706 * Returns <tt>true</tt> if the two specified arrays of booleans are 2707 * <i>equal</i> to one another. Two arrays are considered equal if both 2708 * arrays contain the same number of elements, and all corresponding pairs 2709 * of elements in the two arrays are equal. In other words, two arrays 2710 * are equal if they contain the same elements in the same order. Also, 2711 * two array references are considered equal if both are <tt>null</tt>. 2712 * 2713 * @param a one array to be tested for equality 2714 * @param a2 the other array to be tested for equality 2715 * @return <tt>true</tt> if the two arrays are equal 2716 */ 2717 public static boolean equals(boolean[] a, boolean[] a2) { 2718 if (a==a2) 2719 return true; 2720 if (a==null || a2==null) 2721 return false; 2722 2723 int length = a.length; 2724 if (a2.length != length) 2725 return false; 2726 2727 for (int i=0; i<length; i++) 2728 if (a[i] != a2[i]) 2729 return false; 2730 2731 return true; 2732 } 2733 2734 /** 2735 * Returns <tt>true</tt> if the two specified arrays of doubles are 2736 * <i>equal</i> to one another. Two arrays are considered equal if both 2737 * arrays contain the same number of elements, and all corresponding pairs 2738 * of elements in the two arrays are equal. In other words, two arrays 2739 * are equal if they contain the same elements in the same order. Also, 2740 * two array references are considered equal if both are <tt>null</tt>. 2741 * 2742 * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if: 2743 * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre> 2744 * (Unlike the <tt>==</tt> operator, this method considers 2745 * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.) 2746 * 2747 * @param a one array to be tested for equality 2748 * @param a2 the other array to be tested for equality 2749 * @return <tt>true</tt> if the two arrays are equal 2750 * @see Double#equals(Object) 2751 */ 2752 public static boolean equals(double[] a, double[] a2) { 2753 if (a==a2) 2754 return true; 2755 if (a==null || a2==null) 2756 return false; 2757 2758 int length = a.length; 2759 if (a2.length != length) 2760 return false; 2761 2762 for (int i=0; i<length; i++) 2763 if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i])) 2764 return false; 2765 2766 return true; 2767 } 2768 2769 /** 2770 * Returns <tt>true</tt> if the two specified arrays of floats are 2771 * <i>equal</i> to one another. Two arrays are considered equal if both 2772 * arrays contain the same number of elements, and all corresponding pairs 2773 * of elements in the two arrays are equal. In other words, two arrays 2774 * are equal if they contain the same elements in the same order. Also, 2775 * two array references are considered equal if both are <tt>null</tt>. 2776 * 2777 * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if: 2778 * <pre> <tt>new Float(f1).equals(new Float(f2))</tt></pre> 2779 * (Unlike the <tt>==</tt> operator, this method considers 2780 * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.) 2781 * 2782 * @param a one array to be tested for equality 2783 * @param a2 the other array to be tested for equality 2784 * @return <tt>true</tt> if the two arrays are equal 2785 * @see Float#equals(Object) 2786 */ 2787 public static boolean equals(float[] a, float[] a2) { 2788 if (a==a2) 2789 return true; 2790 if (a==null || a2==null) 2791 return false; 2792 2793 int length = a.length; 2794 if (a2.length != length) 2795 return false; 2796 2797 for (int i=0; i<length; i++) 2798 if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i])) 2799 return false; 2800 2801 return true; 2802 } 2803 2804 /** 2805 * Returns <tt>true</tt> if the two specified arrays of Objects are 2806 * <i>equal</i> to one another. The two arrays are considered equal if 2807 * both arrays contain the same number of elements, and all corresponding 2808 * pairs of elements in the two arrays are equal. Two objects <tt>e1</tt> 2809 * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null 2810 * : e1.equals(e2))</tt>. In other words, the two arrays are equal if 2811 * they contain the same elements in the same order. Also, two array 2812 * references are considered equal if both are <tt>null</tt>. 2813 * 2814 * @param a one array to be tested for equality 2815 * @param a2 the other array to be tested for equality 2816 * @return <tt>true</tt> if the two arrays are equal 2817 */ 2818 public static boolean equals(Object[] a, Object[] a2) { 2819 if (a==a2) 2820 return true; 2821 if (a==null || a2==null) 2822 return false; 2823 2824 int length = a.length; 2825 if (a2.length != length) 2826 return false; 2827 2828 for (int i=0; i<length; i++) { 2829 Object o1 = a[i]; 2830 Object o2 = a2[i]; 2831 if (!(o1==null ? o2==null : o1.equals(o2))) 2832 return false; 2833 } 2834 2835 return true; 2836 } 2837 2838 // Filling 2839 2840 /** 2841 * Assigns the specified long value to each element of the specified array 2842 * of longs. 2843 * 2844 * @param a the array to be filled 2845 * @param val the value to be stored in all elements of the array 2846 */ 2847 public static void fill(long[] a, long val) { 2848 for (int i = 0, len = a.length; i < len; i++) 2849 a[i] = val; 2850 } 2851 2852 /** 2853 * Assigns the specified long value to each element of the specified 2854 * range of the specified array of longs. The range to be filled 2855 * extends from index <tt>fromIndex</tt>, inclusive, to index 2856 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 2857 * range to be filled is empty.) 2858 * 2859 * @param a the array to be filled 2860 * @param fromIndex the index of the first element (inclusive) to be 2861 * filled with the specified value 2862 * @param toIndex the index of the last element (exclusive) to be 2863 * filled with the specified value 2864 * @param val the value to be stored in all elements of the array 2865 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 2866 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 2867 * <tt>toIndex > a.length</tt> 2868 */ 2869 public static void fill(long[] a, int fromIndex, int toIndex, long val) { 2870 rangeCheck(a.length, fromIndex, toIndex); 2871 for (int i = fromIndex; i < toIndex; i++) 2872 a[i] = val; 2873 } 2874 2875 /** 2876 * Assigns the specified int value to each element of the specified array 2877 * of ints. 2878 * 2879 * @param a the array to be filled 2880 * @param val the value to be stored in all elements of the array 2881 */ 2882 public static void fill(int[] a, int val) { 2883 for (int i = 0, len = a.length; i < len; i++) 2884 a[i] = val; 2885 } 2886 2887 /** 2888 * Assigns the specified int value to each element of the specified 2889 * range of the specified array of ints. The range to be filled 2890 * extends from index <tt>fromIndex</tt>, inclusive, to index 2891 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 2892 * range to be filled is empty.) 2893 * 2894 * @param a the array to be filled 2895 * @param fromIndex the index of the first element (inclusive) to be 2896 * filled with the specified value 2897 * @param toIndex the index of the last element (exclusive) to be 2898 * filled with the specified value 2899 * @param val the value to be stored in all elements of the array 2900 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 2901 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 2902 * <tt>toIndex > a.length</tt> 2903 */ 2904 public static void fill(int[] a, int fromIndex, int toIndex, int val) { 2905 rangeCheck(a.length, fromIndex, toIndex); 2906 for (int i = fromIndex; i < toIndex; i++) 2907 a[i] = val; 2908 } 2909 2910 /** 2911 * Assigns the specified short value to each element of the specified array 2912 * of shorts. 2913 * 2914 * @param a the array to be filled 2915 * @param val the value to be stored in all elements of the array 2916 */ 2917 public static void fill(short[] a, short val) { 2918 for (int i = 0, len = a.length; i < len; i++) 2919 a[i] = val; 2920 } 2921 2922 /** 2923 * Assigns the specified short value to each element of the specified 2924 * range of the specified array of shorts. The range to be filled 2925 * extends from index <tt>fromIndex</tt>, inclusive, to index 2926 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 2927 * range to be filled is empty.) 2928 * 2929 * @param a the array to be filled 2930 * @param fromIndex the index of the first element (inclusive) to be 2931 * filled with the specified value 2932 * @param toIndex the index of the last element (exclusive) to be 2933 * filled with the specified value 2934 * @param val the value to be stored in all elements of the array 2935 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 2936 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 2937 * <tt>toIndex > a.length</tt> 2938 */ 2939 public static void fill(short[] a, int fromIndex, int toIndex, short val) { 2940 rangeCheck(a.length, fromIndex, toIndex); 2941 for (int i = fromIndex; i < toIndex; i++) 2942 a[i] = val; 2943 } 2944 2945 /** 2946 * Assigns the specified char value to each element of the specified array 2947 * of chars. 2948 * 2949 * @param a the array to be filled 2950 * @param val the value to be stored in all elements of the array 2951 */ 2952 public static void fill(char[] a, char val) { 2953 for (int i = 0, len = a.length; i < len; i++) 2954 a[i] = val; 2955 } 2956 2957 /** 2958 * Assigns the specified char value to each element of the specified 2959 * range of the specified array of chars. The range to be filled 2960 * extends from index <tt>fromIndex</tt>, inclusive, to index 2961 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 2962 * range to be filled is empty.) 2963 * 2964 * @param a the array to be filled 2965 * @param fromIndex the index of the first element (inclusive) to be 2966 * filled with the specified value 2967 * @param toIndex the index of the last element (exclusive) to be 2968 * filled with the specified value 2969 * @param val the value to be stored in all elements of the array 2970 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 2971 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 2972 * <tt>toIndex > a.length</tt> 2973 */ 2974 public static void fill(char[] a, int fromIndex, int toIndex, char val) { 2975 rangeCheck(a.length, fromIndex, toIndex); 2976 for (int i = fromIndex; i < toIndex; i++) 2977 a[i] = val; 2978 } 2979 2980 /** 2981 * Assigns the specified byte value to each element of the specified array 2982 * of bytes. 2983 * 2984 * @param a the array to be filled 2985 * @param val the value to be stored in all elements of the array 2986 */ 2987 public static void fill(byte[] a, byte val) { 2988 for (int i = 0, len = a.length; i < len; i++) 2989 a[i] = val; 2990 } 2991 2992 /** 2993 * Assigns the specified byte value to each element of the specified 2994 * range of the specified array of bytes. The range to be filled 2995 * extends from index <tt>fromIndex</tt>, inclusive, to index 2996 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 2997 * range to be filled is empty.) 2998 * 2999 * @param a the array to be filled 3000 * @param fromIndex the index of the first element (inclusive) to be 3001 * filled with the specified value 3002 * @param toIndex the index of the last element (exclusive) to be 3003 * filled with the specified value 3004 * @param val the value to be stored in all elements of the array 3005 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 3006 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 3007 * <tt>toIndex > a.length</tt> 3008 */ 3009 public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { 3010 rangeCheck(a.length, fromIndex, toIndex); 3011 for (int i = fromIndex; i < toIndex; i++) 3012 a[i] = val; 3013 } 3014 3015 /** 3016 * Assigns the specified boolean value to each element of the specified 3017 * array of booleans. 3018 * 3019 * @param a the array to be filled 3020 * @param val the value to be stored in all elements of the array 3021 */ 3022 public static void fill(boolean[] a, boolean val) { 3023 for (int i = 0, len = a.length; i < len; i++) 3024 a[i] = val; 3025 } 3026 3027 /** 3028 * Assigns the specified boolean value to each element of the specified 3029 * range of the specified array of booleans. The range to be filled 3030 * extends from index <tt>fromIndex</tt>, inclusive, to index 3031 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 3032 * range to be filled is empty.) 3033 * 3034 * @param a the array to be filled 3035 * @param fromIndex the index of the first element (inclusive) to be 3036 * filled with the specified value 3037 * @param toIndex the index of the last element (exclusive) to be 3038 * filled with the specified value 3039 * @param val the value to be stored in all elements of the array 3040 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 3041 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 3042 * <tt>toIndex > a.length</tt> 3043 */ 3044 public static void fill(boolean[] a, int fromIndex, int toIndex, 3045 boolean val) { 3046 rangeCheck(a.length, fromIndex, toIndex); 3047 for (int i = fromIndex; i < toIndex; i++) 3048 a[i] = val; 3049 } 3050 3051 /** 3052 * Assigns the specified double value to each element of the specified 3053 * array of doubles. 3054 * 3055 * @param a the array to be filled 3056 * @param val the value to be stored in all elements of the array 3057 */ 3058 public static void fill(double[] a, double val) { 3059 for (int i = 0, len = a.length; i < len; i++) 3060 a[i] = val; 3061 } 3062 3063 /** 3064 * Assigns the specified double value to each element of the specified 3065 * range of the specified array of doubles. The range to be filled 3066 * extends from index <tt>fromIndex</tt>, inclusive, to index 3067 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 3068 * range to be filled is empty.) 3069 * 3070 * @param a the array to be filled 3071 * @param fromIndex the index of the first element (inclusive) to be 3072 * filled with the specified value 3073 * @param toIndex the index of the last element (exclusive) to be 3074 * filled with the specified value 3075 * @param val the value to be stored in all elements of the array 3076 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 3077 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 3078 * <tt>toIndex > a.length</tt> 3079 */ 3080 public static void fill(double[] a, int fromIndex, int toIndex,double val){ 3081 rangeCheck(a.length, fromIndex, toIndex); 3082 for (int i = fromIndex; i < toIndex; i++) 3083 a[i] = val; 3084 } 3085 3086 /** 3087 * Assigns the specified float value to each element of the specified array 3088 * of floats. 3089 * 3090 * @param a the array to be filled 3091 * @param val the value to be stored in all elements of the array 3092 */ 3093 public static void fill(float[] a, float val) { 3094 for (int i = 0, len = a.length; i < len; i++) 3095 a[i] = val; 3096 } 3097 3098 /** 3099 * Assigns the specified float value to each element of the specified 3100 * range of the specified array of floats. The range to be filled 3101 * extends from index <tt>fromIndex</tt>, inclusive, to index 3102 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 3103 * range to be filled is empty.) 3104 * 3105 * @param a the array to be filled 3106 * @param fromIndex the index of the first element (inclusive) to be 3107 * filled with the specified value 3108 * @param toIndex the index of the last element (exclusive) to be 3109 * filled with the specified value 3110 * @param val the value to be stored in all elements of the array 3111 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 3112 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 3113 * <tt>toIndex > a.length</tt> 3114 */ 3115 public static void fill(float[] a, int fromIndex, int toIndex, float val) { 3116 rangeCheck(a.length, fromIndex, toIndex); 3117 for (int i = fromIndex; i < toIndex; i++) 3118 a[i] = val; 3119 } 3120 3121 /** 3122 * Assigns the specified Object reference to each element of the specified 3123 * array of Objects. 3124 * 3125 * @param a the array to be filled 3126 * @param val the value to be stored in all elements of the array 3127 * @throws ArrayStoreException if the specified value is not of a 3128 * runtime type that can be stored in the specified array 3129 */ 3130 public static void fill(Object[] a, Object val) { 3131 for (int i = 0, len = a.length; i < len; i++) 3132 a[i] = val; 3133 } 3134 3135 /** 3136 * Assigns the specified Object reference to each element of the specified 3137 * range of the specified array of Objects. The range to be filled 3138 * extends from index <tt>fromIndex</tt>, inclusive, to index 3139 * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 3140 * range to be filled is empty.) 3141 * 3142 * @param a the array to be filled 3143 * @param fromIndex the index of the first element (inclusive) to be 3144 * filled with the specified value 3145 * @param toIndex the index of the last element (exclusive) to be 3146 * filled with the specified value 3147 * @param val the value to be stored in all elements of the array 3148 * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 3149 * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 3150 * <tt>toIndex > a.length</tt> 3151 * @throws ArrayStoreException if the specified value is not of a 3152 * runtime type that can be stored in the specified array 3153 */ 3154 public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { 3155 rangeCheck(a.length, fromIndex, toIndex); 3156 for (int i = fromIndex; i < toIndex; i++) 3157 a[i] = val; 3158 } 3159 3160 // Cloning 3161 3162 /** 3163 * Copies the specified array, truncating or padding with nulls (if necessary) 3164 * so the copy has the specified length. For all indices that are 3165 * valid in both the original array and the copy, the two arrays will 3166 * contain identical values. For any indices that are valid in the 3167 * copy but not the original, the copy will contain <tt>null</tt>. 3168 * Such indices will exist if and only if the specified length 3169 * is greater than that of the original array. 3170 * The resulting array is of exactly the same class as the original array. 3171 * 3172 * @param <T> the class of the objects in the array 3173 * @param original the array to be copied 3174 * @param newLength the length of the copy to be returned 3175 * @return a copy of the original array, truncated or padded with nulls 3176 * to obtain the specified length 3177 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3178 * @throws NullPointerException if <tt>original</tt> is null 3179 * @since 1.6 3180 */ 3181 @SuppressWarnings("unchecked") 3182 public static <T> T[] copyOf(T[] original, int newLength) { 3183 return (T[]) copyOf(original, newLength, original.getClass()); 3184 } 3185 3186 /** 3187 * Copies the specified array, truncating or padding with nulls (if necessary) 3188 * so the copy has the specified length. For all indices that are 3189 * valid in both the original array and the copy, the two arrays will 3190 * contain identical values. For any indices that are valid in the 3191 * copy but not the original, the copy will contain <tt>null</tt>. 3192 * Such indices will exist if and only if the specified length 3193 * is greater than that of the original array. 3194 * The resulting array is of the class <tt>newType</tt>. 3195 * 3196 * @param <U> the class of the objects in the original array 3197 * @param <T> the class of the objects in the returned array 3198 * @param original the array to be copied 3199 * @param newLength the length of the copy to be returned 3200 * @param newType the class of the copy to be returned 3201 * @return a copy of the original array, truncated or padded with nulls 3202 * to obtain the specified length 3203 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3204 * @throws NullPointerException if <tt>original</tt> is null 3205 * @throws ArrayStoreException if an element copied from 3206 * <tt>original</tt> is not of a runtime type that can be stored in 3207 * an array of class <tt>newType</tt> 3208 * @since 1.6 3209 */ 3210 @HotSpotIntrinsicCandidate 3211 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { 3212 @SuppressWarnings("unchecked") 3213 T[] copy = ((Object)newType == (Object)Object[].class) 3214 ? (T[]) new Object[newLength] 3215 : (T[]) Array.newInstance(newType.getComponentType(), newLength); 3216 System.arraycopy(original, 0, copy, 0, 3217 Math.min(original.length, newLength)); 3218 return copy; 3219 } 3220 3221 /** 3222 * Copies the specified array, truncating or padding with zeros (if necessary) 3223 * so the copy has the specified length. For all indices that are 3224 * valid in both the original array and the copy, the two arrays will 3225 * contain identical values. For any indices that are valid in the 3226 * copy but not the original, the copy will contain <tt>(byte)0</tt>. 3227 * Such indices will exist if and only if the specified length 3228 * is greater than that of the original array. 3229 * 3230 * @param original the array to be copied 3231 * @param newLength the length of the copy to be returned 3232 * @return a copy of the original array, truncated or padded with zeros 3233 * to obtain the specified length 3234 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3235 * @throws NullPointerException if <tt>original</tt> is null 3236 * @since 1.6 3237 */ 3238 public static byte[] copyOf(byte[] original, int newLength) { 3239 byte[] copy = new byte[newLength]; 3240 System.arraycopy(original, 0, copy, 0, 3241 Math.min(original.length, newLength)); 3242 return copy; 3243 } 3244 3245 /** 3246 * Copies the specified array, truncating or padding with zeros (if necessary) 3247 * so the copy has the specified length. For all indices that are 3248 * valid in both the original array and the copy, the two arrays will 3249 * contain identical values. For any indices that are valid in the 3250 * copy but not the original, the copy will contain <tt>(short)0</tt>. 3251 * Such indices will exist if and only if the specified length 3252 * is greater than that of the original array. 3253 * 3254 * @param original the array to be copied 3255 * @param newLength the length of the copy to be returned 3256 * @return a copy of the original array, truncated or padded with zeros 3257 * to obtain the specified length 3258 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3259 * @throws NullPointerException if <tt>original</tt> is null 3260 * @since 1.6 3261 */ 3262 public static short[] copyOf(short[] original, int newLength) { 3263 short[] copy = new short[newLength]; 3264 System.arraycopy(original, 0, copy, 0, 3265 Math.min(original.length, newLength)); 3266 return copy; 3267 } 3268 3269 /** 3270 * Copies the specified array, truncating or padding with zeros (if necessary) 3271 * so the copy has the specified length. For all indices that are 3272 * valid in both the original array and the copy, the two arrays will 3273 * contain identical values. For any indices that are valid in the 3274 * copy but not the original, the copy will contain <tt>0</tt>. 3275 * Such indices will exist if and only if the specified length 3276 * is greater than that of the original array. 3277 * 3278 * @param original the array to be copied 3279 * @param newLength the length of the copy to be returned 3280 * @return a copy of the original array, truncated or padded with zeros 3281 * to obtain the specified length 3282 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3283 * @throws NullPointerException if <tt>original</tt> is null 3284 * @since 1.6 3285 */ 3286 public static int[] copyOf(int[] original, int newLength) { 3287 int[] copy = new int[newLength]; 3288 System.arraycopy(original, 0, copy, 0, 3289 Math.min(original.length, newLength)); 3290 return copy; 3291 } 3292 3293 /** 3294 * Copies the specified array, truncating or padding with zeros (if necessary) 3295 * so the copy has the specified length. For all indices that are 3296 * valid in both the original array and the copy, the two arrays will 3297 * contain identical values. For any indices that are valid in the 3298 * copy but not the original, the copy will contain <tt>0L</tt>. 3299 * Such indices will exist if and only if the specified length 3300 * is greater than that of the original array. 3301 * 3302 * @param original the array to be copied 3303 * @param newLength the length of the copy to be returned 3304 * @return a copy of the original array, truncated or padded with zeros 3305 * to obtain the specified length 3306 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3307 * @throws NullPointerException if <tt>original</tt> is null 3308 * @since 1.6 3309 */ 3310 public static long[] copyOf(long[] original, int newLength) { 3311 long[] copy = new long[newLength]; 3312 System.arraycopy(original, 0, copy, 0, 3313 Math.min(original.length, newLength)); 3314 return copy; 3315 } 3316 3317 /** 3318 * Copies the specified array, truncating or padding with null characters (if necessary) 3319 * so the copy has the specified length. For all indices that are valid 3320 * in both the original array and the copy, the two arrays will contain 3321 * identical values. For any indices that are valid in the copy but not 3322 * the original, the copy will contain <tt>'\\u000'</tt>. Such indices 3323 * will exist if and only if the specified length is greater than that of 3324 * the original array. 3325 * 3326 * @param original the array to be copied 3327 * @param newLength the length of the copy to be returned 3328 * @return a copy of the original array, truncated or padded with null characters 3329 * to obtain the specified length 3330 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3331 * @throws NullPointerException if <tt>original</tt> is null 3332 * @since 1.6 3333 */ 3334 public static char[] copyOf(char[] original, int newLength) { 3335 char[] copy = new char[newLength]; 3336 System.arraycopy(original, 0, copy, 0, 3337 Math.min(original.length, newLength)); 3338 return copy; 3339 } 3340 3341 /** 3342 * Copies the specified array, truncating or padding with zeros (if necessary) 3343 * so the copy has the specified length. For all indices that are 3344 * valid in both the original array and the copy, the two arrays will 3345 * contain identical values. For any indices that are valid in the 3346 * copy but not the original, the copy will contain <tt>0f</tt>. 3347 * Such indices will exist if and only if the specified length 3348 * is greater than that of the original array. 3349 * 3350 * @param original the array to be copied 3351 * @param newLength the length of the copy to be returned 3352 * @return a copy of the original array, truncated or padded with zeros 3353 * to obtain the specified length 3354 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3355 * @throws NullPointerException if <tt>original</tt> is null 3356 * @since 1.6 3357 */ 3358 public static float[] copyOf(float[] original, int newLength) { 3359 float[] copy = new float[newLength]; 3360 System.arraycopy(original, 0, copy, 0, 3361 Math.min(original.length, newLength)); 3362 return copy; 3363 } 3364 3365 /** 3366 * Copies the specified array, truncating or padding with zeros (if necessary) 3367 * so the copy has the specified length. For all indices that are 3368 * valid in both the original array and the copy, the two arrays will 3369 * contain identical values. For any indices that are valid in the 3370 * copy but not the original, the copy will contain <tt>0d</tt>. 3371 * Such indices will exist if and only if the specified length 3372 * is greater than that of the original array. 3373 * 3374 * @param original the array to be copied 3375 * @param newLength the length of the copy to be returned 3376 * @return a copy of the original array, truncated or padded with zeros 3377 * to obtain the specified length 3378 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3379 * @throws NullPointerException if <tt>original</tt> is null 3380 * @since 1.6 3381 */ 3382 public static double[] copyOf(double[] original, int newLength) { 3383 double[] copy = new double[newLength]; 3384 System.arraycopy(original, 0, copy, 0, 3385 Math.min(original.length, newLength)); 3386 return copy; 3387 } 3388 3389 /** 3390 * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary) 3391 * so the copy has the specified length. For all indices that are 3392 * valid in both the original array and the copy, the two arrays will 3393 * contain identical values. For any indices that are valid in the 3394 * copy but not the original, the copy will contain <tt>false</tt>. 3395 * Such indices will exist if and only if the specified length 3396 * is greater than that of the original array. 3397 * 3398 * @param original the array to be copied 3399 * @param newLength the length of the copy to be returned 3400 * @return a copy of the original array, truncated or padded with false elements 3401 * to obtain the specified length 3402 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 3403 * @throws NullPointerException if <tt>original</tt> is null 3404 * @since 1.6 3405 */ 3406 public static boolean[] copyOf(boolean[] original, int newLength) { 3407 boolean[] copy = new boolean[newLength]; 3408 System.arraycopy(original, 0, copy, 0, 3409 Math.min(original.length, newLength)); 3410 return copy; 3411 } 3412 3413 /** 3414 * Copies the specified range of the specified array into a new array. 3415 * The initial index of the range (<tt>from</tt>) must lie between zero 3416 * and <tt>original.length</tt>, inclusive. The value at 3417 * <tt>original[from]</tt> is placed into the initial element of the copy 3418 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3419 * Values from subsequent elements in the original array are placed into 3420 * subsequent elements in the copy. The final index of the range 3421 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3422 * may be greater than <tt>original.length</tt>, in which case 3423 * <tt>null</tt> is placed in all elements of the copy whose index is 3424 * greater than or equal to <tt>original.length - from</tt>. The length 3425 * of the returned array will be <tt>to - from</tt>. 3426 * <p> 3427 * The resulting array is of exactly the same class as the original array. 3428 * 3429 * @param <T> the class of the objects in the array 3430 * @param original the array from which a range is to be copied 3431 * @param from the initial index of the range to be copied, inclusive 3432 * @param to the final index of the range to be copied, exclusive. 3433 * (This index may lie outside the array.) 3434 * @return a new array containing the specified range from the original array, 3435 * truncated or padded with nulls to obtain the required length 3436 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3437 * or {@code from > original.length} 3438 * @throws IllegalArgumentException if <tt>from > to</tt> 3439 * @throws NullPointerException if <tt>original</tt> is null 3440 * @since 1.6 3441 */ 3442 @SuppressWarnings("unchecked") 3443 public static <T> T[] copyOfRange(T[] original, int from, int to) { 3444 return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass()); 3445 } 3446 3447 /** 3448 * Copies the specified range of the specified array into a new array. 3449 * The initial index of the range (<tt>from</tt>) must lie between zero 3450 * and <tt>original.length</tt>, inclusive. The value at 3451 * <tt>original[from]</tt> is placed into the initial element of the copy 3452 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3453 * Values from subsequent elements in the original array are placed into 3454 * subsequent elements in the copy. The final index of the range 3455 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3456 * may be greater than <tt>original.length</tt>, in which case 3457 * <tt>null</tt> is placed in all elements of the copy whose index is 3458 * greater than or equal to <tt>original.length - from</tt>. The length 3459 * of the returned array will be <tt>to - from</tt>. 3460 * The resulting array is of the class <tt>newType</tt>. 3461 * 3462 * @param <U> the class of the objects in the original array 3463 * @param <T> the class of the objects in the returned array 3464 * @param original the array from which a range is to be copied 3465 * @param from the initial index of the range to be copied, inclusive 3466 * @param to the final index of the range to be copied, exclusive. 3467 * (This index may lie outside the array.) 3468 * @param newType the class of the copy to be returned 3469 * @return a new array containing the specified range from the original array, 3470 * truncated or padded with nulls to obtain the required length 3471 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3472 * or {@code from > original.length} 3473 * @throws IllegalArgumentException if <tt>from > to</tt> 3474 * @throws NullPointerException if <tt>original</tt> is null 3475 * @throws ArrayStoreException if an element copied from 3476 * <tt>original</tt> is not of a runtime type that can be stored in 3477 * an array of class <tt>newType</tt>. 3478 * @since 1.6 3479 */ 3480 @HotSpotIntrinsicCandidate 3481 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { 3482 int newLength = to - from; 3483 if (newLength < 0) 3484 throw new IllegalArgumentException(from + " > " + to); 3485 @SuppressWarnings("unchecked") 3486 T[] copy = ((Object)newType == (Object)Object[].class) 3487 ? (T[]) new Object[newLength] 3488 : (T[]) Array.newInstance(newType.getComponentType(), newLength); 3489 System.arraycopy(original, from, copy, 0, 3490 Math.min(original.length - from, newLength)); 3491 return copy; 3492 } 3493 3494 /** 3495 * Copies the specified range of the specified array into a new array. 3496 * The initial index of the range (<tt>from</tt>) must lie between zero 3497 * and <tt>original.length</tt>, inclusive. The value at 3498 * <tt>original[from]</tt> is placed into the initial element of the copy 3499 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3500 * Values from subsequent elements in the original array are placed into 3501 * subsequent elements in the copy. The final index of the range 3502 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3503 * may be greater than <tt>original.length</tt>, in which case 3504 * <tt>(byte)0</tt> is placed in all elements of the copy whose index is 3505 * greater than or equal to <tt>original.length - from</tt>. The length 3506 * of the returned array will be <tt>to - from</tt>. 3507 * 3508 * @param original the array from which a range is to be copied 3509 * @param from the initial index of the range to be copied, inclusive 3510 * @param to the final index of the range to be copied, exclusive. 3511 * (This index may lie outside the array.) 3512 * @return a new array containing the specified range from the original array, 3513 * truncated or padded with zeros to obtain the required length 3514 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3515 * or {@code from > original.length} 3516 * @throws IllegalArgumentException if <tt>from > to</tt> 3517 * @throws NullPointerException if <tt>original</tt> is null 3518 * @since 1.6 3519 */ 3520 public static byte[] copyOfRange(byte[] original, int from, int to) { 3521 int newLength = to - from; 3522 if (newLength < 0) 3523 throw new IllegalArgumentException(from + " > " + to); 3524 byte[] copy = new byte[newLength]; 3525 System.arraycopy(original, from, copy, 0, 3526 Math.min(original.length - from, newLength)); 3527 return copy; 3528 } 3529 3530 /** 3531 * Copies the specified range of the specified array into a new array. 3532 * The initial index of the range (<tt>from</tt>) must lie between zero 3533 * and <tt>original.length</tt>, inclusive. The value at 3534 * <tt>original[from]</tt> is placed into the initial element of the copy 3535 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3536 * Values from subsequent elements in the original array are placed into 3537 * subsequent elements in the copy. The final index of the range 3538 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3539 * may be greater than <tt>original.length</tt>, in which case 3540 * <tt>(short)0</tt> is placed in all elements of the copy whose index is 3541 * greater than or equal to <tt>original.length - from</tt>. The length 3542 * of the returned array will be <tt>to - from</tt>. 3543 * 3544 * @param original the array from which a range is to be copied 3545 * @param from the initial index of the range to be copied, inclusive 3546 * @param to the final index of the range to be copied, exclusive. 3547 * (This index may lie outside the array.) 3548 * @return a new array containing the specified range from the original array, 3549 * truncated or padded with zeros to obtain the required length 3550 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3551 * or {@code from > original.length} 3552 * @throws IllegalArgumentException if <tt>from > to</tt> 3553 * @throws NullPointerException if <tt>original</tt> is null 3554 * @since 1.6 3555 */ 3556 public static short[] copyOfRange(short[] original, int from, int to) { 3557 int newLength = to - from; 3558 if (newLength < 0) 3559 throw new IllegalArgumentException(from + " > " + to); 3560 short[] copy = new short[newLength]; 3561 System.arraycopy(original, from, copy, 0, 3562 Math.min(original.length - from, newLength)); 3563 return copy; 3564 } 3565 3566 /** 3567 * Copies the specified range of the specified array into a new array. 3568 * The initial index of the range (<tt>from</tt>) must lie between zero 3569 * and <tt>original.length</tt>, inclusive. The value at 3570 * <tt>original[from]</tt> is placed into the initial element of the copy 3571 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3572 * Values from subsequent elements in the original array are placed into 3573 * subsequent elements in the copy. The final index of the range 3574 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3575 * may be greater than <tt>original.length</tt>, in which case 3576 * <tt>0</tt> is placed in all elements of the copy whose index is 3577 * greater than or equal to <tt>original.length - from</tt>. The length 3578 * of the returned array will be <tt>to - from</tt>. 3579 * 3580 * @param original the array from which a range is to be copied 3581 * @param from the initial index of the range to be copied, inclusive 3582 * @param to the final index of the range to be copied, exclusive. 3583 * (This index may lie outside the array.) 3584 * @return a new array containing the specified range from the original array, 3585 * truncated or padded with zeros to obtain the required length 3586 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3587 * or {@code from > original.length} 3588 * @throws IllegalArgumentException if <tt>from > to</tt> 3589 * @throws NullPointerException if <tt>original</tt> is null 3590 * @since 1.6 3591 */ 3592 public static int[] copyOfRange(int[] original, int from, int to) { 3593 int newLength = to - from; 3594 if (newLength < 0) 3595 throw new IllegalArgumentException(from + " > " + to); 3596 int[] copy = new int[newLength]; 3597 System.arraycopy(original, from, copy, 0, 3598 Math.min(original.length - from, newLength)); 3599 return copy; 3600 } 3601 3602 /** 3603 * Copies the specified range of the specified array into a new array. 3604 * The initial index of the range (<tt>from</tt>) must lie between zero 3605 * and <tt>original.length</tt>, inclusive. The value at 3606 * <tt>original[from]</tt> is placed into the initial element of the copy 3607 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3608 * Values from subsequent elements in the original array are placed into 3609 * subsequent elements in the copy. The final index of the range 3610 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3611 * may be greater than <tt>original.length</tt>, in which case 3612 * <tt>0L</tt> is placed in all elements of the copy whose index is 3613 * greater than or equal to <tt>original.length - from</tt>. The length 3614 * of the returned array will be <tt>to - from</tt>. 3615 * 3616 * @param original the array from which a range is to be copied 3617 * @param from the initial index of the range to be copied, inclusive 3618 * @param to the final index of the range to be copied, exclusive. 3619 * (This index may lie outside the array.) 3620 * @return a new array containing the specified range from the original array, 3621 * truncated or padded with zeros to obtain the required length 3622 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3623 * or {@code from > original.length} 3624 * @throws IllegalArgumentException if <tt>from > to</tt> 3625 * @throws NullPointerException if <tt>original</tt> is null 3626 * @since 1.6 3627 */ 3628 public static long[] copyOfRange(long[] original, int from, int to) { 3629 int newLength = to - from; 3630 if (newLength < 0) 3631 throw new IllegalArgumentException(from + " > " + to); 3632 long[] copy = new long[newLength]; 3633 System.arraycopy(original, from, copy, 0, 3634 Math.min(original.length - from, newLength)); 3635 return copy; 3636 } 3637 3638 /** 3639 * Copies the specified range of the specified array into a new array. 3640 * The initial index of the range (<tt>from</tt>) must lie between zero 3641 * and <tt>original.length</tt>, inclusive. The value at 3642 * <tt>original[from]</tt> is placed into the initial element of the copy 3643 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3644 * Values from subsequent elements in the original array are placed into 3645 * subsequent elements in the copy. The final index of the range 3646 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3647 * may be greater than <tt>original.length</tt>, in which case 3648 * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is 3649 * greater than or equal to <tt>original.length - from</tt>. The length 3650 * of the returned array will be <tt>to - from</tt>. 3651 * 3652 * @param original the array from which a range is to be copied 3653 * @param from the initial index of the range to be copied, inclusive 3654 * @param to the final index of the range to be copied, exclusive. 3655 * (This index may lie outside the array.) 3656 * @return a new array containing the specified range from the original array, 3657 * truncated or padded with null characters to obtain the required length 3658 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3659 * or {@code from > original.length} 3660 * @throws IllegalArgumentException if <tt>from > to</tt> 3661 * @throws NullPointerException if <tt>original</tt> is null 3662 * @since 1.6 3663 */ 3664 public static char[] copyOfRange(char[] original, int from, int to) { 3665 int newLength = to - from; 3666 if (newLength < 0) 3667 throw new IllegalArgumentException(from + " > " + to); 3668 char[] copy = new char[newLength]; 3669 System.arraycopy(original, from, copy, 0, 3670 Math.min(original.length - from, newLength)); 3671 return copy; 3672 } 3673 3674 /** 3675 * Copies the specified range of the specified array into a new array. 3676 * The initial index of the range (<tt>from</tt>) must lie between zero 3677 * and <tt>original.length</tt>, inclusive. The value at 3678 * <tt>original[from]</tt> is placed into the initial element of the copy 3679 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3680 * Values from subsequent elements in the original array are placed into 3681 * subsequent elements in the copy. The final index of the range 3682 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3683 * may be greater than <tt>original.length</tt>, in which case 3684 * <tt>0f</tt> is placed in all elements of the copy whose index is 3685 * greater than or equal to <tt>original.length - from</tt>. The length 3686 * of the returned array will be <tt>to - from</tt>. 3687 * 3688 * @param original the array from which a range is to be copied 3689 * @param from the initial index of the range to be copied, inclusive 3690 * @param to the final index of the range to be copied, exclusive. 3691 * (This index may lie outside the array.) 3692 * @return a new array containing the specified range from the original array, 3693 * truncated or padded with zeros to obtain the required length 3694 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3695 * or {@code from > original.length} 3696 * @throws IllegalArgumentException if <tt>from > to</tt> 3697 * @throws NullPointerException if <tt>original</tt> is null 3698 * @since 1.6 3699 */ 3700 public static float[] copyOfRange(float[] original, int from, int to) { 3701 int newLength = to - from; 3702 if (newLength < 0) 3703 throw new IllegalArgumentException(from + " > " + to); 3704 float[] copy = new float[newLength]; 3705 System.arraycopy(original, from, copy, 0, 3706 Math.min(original.length - from, newLength)); 3707 return copy; 3708 } 3709 3710 /** 3711 * Copies the specified range of the specified array into a new array. 3712 * The initial index of the range (<tt>from</tt>) must lie between zero 3713 * and <tt>original.length</tt>, inclusive. The value at 3714 * <tt>original[from]</tt> is placed into the initial element of the copy 3715 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3716 * Values from subsequent elements in the original array are placed into 3717 * subsequent elements in the copy. The final index of the range 3718 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3719 * may be greater than <tt>original.length</tt>, in which case 3720 * <tt>0d</tt> is placed in all elements of the copy whose index is 3721 * greater than or equal to <tt>original.length - from</tt>. The length 3722 * of the returned array will be <tt>to - from</tt>. 3723 * 3724 * @param original the array from which a range is to be copied 3725 * @param from the initial index of the range to be copied, inclusive 3726 * @param to the final index of the range to be copied, exclusive. 3727 * (This index may lie outside the array.) 3728 * @return a new array containing the specified range from the original array, 3729 * truncated or padded with zeros to obtain the required length 3730 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3731 * or {@code from > original.length} 3732 * @throws IllegalArgumentException if <tt>from > to</tt> 3733 * @throws NullPointerException if <tt>original</tt> is null 3734 * @since 1.6 3735 */ 3736 public static double[] copyOfRange(double[] original, int from, int to) { 3737 int newLength = to - from; 3738 if (newLength < 0) 3739 throw new IllegalArgumentException(from + " > " + to); 3740 double[] copy = new double[newLength]; 3741 System.arraycopy(original, from, copy, 0, 3742 Math.min(original.length - from, newLength)); 3743 return copy; 3744 } 3745 3746 /** 3747 * Copies the specified range of the specified array into a new array. 3748 * The initial index of the range (<tt>from</tt>) must lie between zero 3749 * and <tt>original.length</tt>, inclusive. The value at 3750 * <tt>original[from]</tt> is placed into the initial element of the copy 3751 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 3752 * Values from subsequent elements in the original array are placed into 3753 * subsequent elements in the copy. The final index of the range 3754 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 3755 * may be greater than <tt>original.length</tt>, in which case 3756 * <tt>false</tt> is placed in all elements of the copy whose index is 3757 * greater than or equal to <tt>original.length - from</tt>. The length 3758 * of the returned array will be <tt>to - from</tt>. 3759 * 3760 * @param original the array from which a range is to be copied 3761 * @param from the initial index of the range to be copied, inclusive 3762 * @param to the final index of the range to be copied, exclusive. 3763 * (This index may lie outside the array.) 3764 * @return a new array containing the specified range from the original array, 3765 * truncated or padded with false elements to obtain the required length 3766 * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 3767 * or {@code from > original.length} 3768 * @throws IllegalArgumentException if <tt>from > to</tt> 3769 * @throws NullPointerException if <tt>original</tt> is null 3770 * @since 1.6 3771 */ 3772 public static boolean[] copyOfRange(boolean[] original, int from, int to) { 3773 int newLength = to - from; 3774 if (newLength < 0) 3775 throw new IllegalArgumentException(from + " > " + to); 3776 boolean[] copy = new boolean[newLength]; 3777 System.arraycopy(original, from, copy, 0, 3778 Math.min(original.length - from, newLength)); 3779 return copy; 3780 } 3781 3782 // Misc 3783 3784 /** 3785 * Returns a fixed-size list backed by the specified array. (Changes to 3786 * the returned list "write through" to the array.) This method acts 3787 * as bridge between array-based and collection-based APIs, in 3788 * combination with {@link Collection#toArray}. The returned list is 3789 * serializable and implements {@link RandomAccess}. 3790 * 3791 * <p>This method also provides a convenient way to create a fixed-size 3792 * list initialized to contain several elements: 3793 * <pre> 3794 * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 3795 * </pre> 3796 * 3797 * @param <T> the class of the objects in the array 3798 * @param a the array by which the list will be backed 3799 * @return a list view of the specified array 3800 */ 3801 @SafeVarargs 3802 @SuppressWarnings("varargs") 3803 public static <T> List<T> asList(T... a) { 3804 return new ArrayList<>(a); 3805 } 3806 3807 /** 3808 * @serial include 3809 */ 3810 private static class ArrayList<E> extends AbstractList<E> 3811 implements RandomAccess, java.io.Serializable 3812 { 3813 private static final long serialVersionUID = -2764017481108945198L; 3814 private final E[] a; 3815 3816 ArrayList(E[] array) { 3817 a = Objects.requireNonNull(array); 3818 } 3819 3820 @Override 3821 public int size() { 3822 return a.length; 3823 } 3824 3825 @Override 3826 public Object[] toArray() { 3827 return a.clone(); 3828 } 3829 3830 @Override 3831 @SuppressWarnings("unchecked") 3832 public <T> T[] toArray(T[] a) { 3833 int size = size(); 3834 if (a.length < size) 3835 return Arrays.copyOf(this.a, size, 3836 (Class<? extends T[]>) a.getClass()); 3837 System.arraycopy(this.a, 0, a, 0, size); 3838 if (a.length > size) 3839 a[size] = null; 3840 return a; 3841 } 3842 3843 @Override 3844 public E get(int index) { 3845 return a[index]; 3846 } 3847 3848 @Override 3849 public E set(int index, E element) { 3850 E oldValue = a[index]; 3851 a[index] = element; 3852 return oldValue; 3853 } 3854 3855 @Override 3856 public int indexOf(Object o) { 3857 E[] a = this.a; 3858 if (o == null) { 3859 for (int i = 0; i < a.length; i++) 3860 if (a[i] == null) 3861 return i; 3862 } else { 3863 for (int i = 0; i < a.length; i++) 3864 if (o.equals(a[i])) 3865 return i; 3866 } 3867 return -1; 3868 } 3869 3870 @Override 3871 public boolean contains(Object o) { 3872 return indexOf(o) >= 0; 3873 } 3874 3875 @Override 3876 public Spliterator<E> spliterator() { 3877 return Spliterators.spliterator(a, Spliterator.ORDERED); 3878 } 3879 3880 @Override 3881 public void forEach(Consumer<? super E> action) { 3882 Objects.requireNonNull(action); 3883 for (E e : a) { 3884 action.accept(e); 3885 } 3886 } 3887 3888 @Override 3889 public void replaceAll(UnaryOperator<E> operator) { 3890 Objects.requireNonNull(operator); 3891 E[] a = this.a; 3892 for (int i = 0; i < a.length; i++) { 3893 a[i] = operator.apply(a[i]); 3894 } 3895 } 3896 3897 @Override 3898 public void sort(Comparator<? super E> c) { 3899 Arrays.sort(a, c); 3900 } 3901 } 3902 3903 /** 3904 * Returns a hash code based on the contents of the specified array. 3905 * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt> 3906 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 3907 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 3908 * 3909 * <p>The value returned by this method is the same value that would be 3910 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 3911 * method on a {@link List} containing a sequence of {@link Long} 3912 * instances representing the elements of <tt>a</tt> in the same order. 3913 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 3914 * 3915 * @param a the array whose hash value to compute 3916 * @return a content-based hash code for <tt>a</tt> 3917 * @since 1.5 3918 */ 3919 public static int hashCode(long a[]) { 3920 if (a == null) 3921 return 0; 3922 3923 int result = 1; 3924 for (long element : a) { 3925 int elementHash = (int)(element ^ (element >>> 32)); 3926 result = 31 * result + elementHash; 3927 } 3928 3929 return result; 3930 } 3931 3932 /** 3933 * Returns a hash code based on the contents of the specified array. 3934 * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt> 3935 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 3936 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 3937 * 3938 * <p>The value returned by this method is the same value that would be 3939 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 3940 * method on a {@link List} containing a sequence of {@link Integer} 3941 * instances representing the elements of <tt>a</tt> in the same order. 3942 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 3943 * 3944 * @param a the array whose hash value to compute 3945 * @return a content-based hash code for <tt>a</tt> 3946 * @since 1.5 3947 */ 3948 public static int hashCode(int a[]) { 3949 if (a == null) 3950 return 0; 3951 3952 int result = 1; 3953 for (int element : a) 3954 result = 31 * result + element; 3955 3956 return result; 3957 } 3958 3959 /** 3960 * Returns a hash code based on the contents of the specified array. 3961 * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt> 3962 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 3963 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 3964 * 3965 * <p>The value returned by this method is the same value that would be 3966 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 3967 * method on a {@link List} containing a sequence of {@link Short} 3968 * instances representing the elements of <tt>a</tt> in the same order. 3969 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 3970 * 3971 * @param a the array whose hash value to compute 3972 * @return a content-based hash code for <tt>a</tt> 3973 * @since 1.5 3974 */ 3975 public static int hashCode(short a[]) { 3976 if (a == null) 3977 return 0; 3978 3979 int result = 1; 3980 for (short element : a) 3981 result = 31 * result + element; 3982 3983 return result; 3984 } 3985 3986 /** 3987 * Returns a hash code based on the contents of the specified array. 3988 * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt> 3989 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 3990 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 3991 * 3992 * <p>The value returned by this method is the same value that would be 3993 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 3994 * method on a {@link List} containing a sequence of {@link Character} 3995 * instances representing the elements of <tt>a</tt> in the same order. 3996 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 3997 * 3998 * @param a the array whose hash value to compute 3999 * @return a content-based hash code for <tt>a</tt> 4000 * @since 1.5 4001 */ 4002 public static int hashCode(char a[]) { 4003 if (a == null) 4004 return 0; 4005 4006 int result = 1; 4007 for (char element : a) 4008 result = 31 * result + element; 4009 4010 return result; 4011 } 4012 4013 /** 4014 * Returns a hash code based on the contents of the specified array. 4015 * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt> 4016 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 4017 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 4018 * 4019 * <p>The value returned by this method is the same value that would be 4020 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 4021 * method on a {@link List} containing a sequence of {@link Byte} 4022 * instances representing the elements of <tt>a</tt> in the same order. 4023 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 4024 * 4025 * @param a the array whose hash value to compute 4026 * @return a content-based hash code for <tt>a</tt> 4027 * @since 1.5 4028 */ 4029 public static int hashCode(byte a[]) { 4030 if (a == null) 4031 return 0; 4032 4033 int result = 1; 4034 for (byte element : a) 4035 result = 31 * result + element; 4036 4037 return result; 4038 } 4039 4040 /** 4041 * Returns a hash code based on the contents of the specified array. 4042 * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt> 4043 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 4044 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 4045 * 4046 * <p>The value returned by this method is the same value that would be 4047 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 4048 * method on a {@link List} containing a sequence of {@link Boolean} 4049 * instances representing the elements of <tt>a</tt> in the same order. 4050 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 4051 * 4052 * @param a the array whose hash value to compute 4053 * @return a content-based hash code for <tt>a</tt> 4054 * @since 1.5 4055 */ 4056 public static int hashCode(boolean a[]) { 4057 if (a == null) 4058 return 0; 4059 4060 int result = 1; 4061 for (boolean element : a) 4062 result = 31 * result + (element ? 1231 : 1237); 4063 4064 return result; 4065 } 4066 4067 /** 4068 * Returns a hash code based on the contents of the specified array. 4069 * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt> 4070 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 4071 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 4072 * 4073 * <p>The value returned by this method is the same value that would be 4074 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 4075 * method on a {@link List} containing a sequence of {@link Float} 4076 * instances representing the elements of <tt>a</tt> in the same order. 4077 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 4078 * 4079 * @param a the array whose hash value to compute 4080 * @return a content-based hash code for <tt>a</tt> 4081 * @since 1.5 4082 */ 4083 public static int hashCode(float a[]) { 4084 if (a == null) 4085 return 0; 4086 4087 int result = 1; 4088 for (float element : a) 4089 result = 31 * result + Float.floatToIntBits(element); 4090 4091 return result; 4092 } 4093 4094 /** 4095 * Returns a hash code based on the contents of the specified array. 4096 * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt> 4097 * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 4098 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 4099 * 4100 * <p>The value returned by this method is the same value that would be 4101 * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 4102 * method on a {@link List} containing a sequence of {@link Double} 4103 * instances representing the elements of <tt>a</tt> in the same order. 4104 * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 4105 * 4106 * @param a the array whose hash value to compute 4107 * @return a content-based hash code for <tt>a</tt> 4108 * @since 1.5 4109 */ 4110 public static int hashCode(double a[]) { 4111 if (a == null) 4112 return 0; 4113 4114 int result = 1; 4115 for (double element : a) { 4116 long bits = Double.doubleToLongBits(element); 4117 result = 31 * result + (int)(bits ^ (bits >>> 32)); 4118 } 4119 return result; 4120 } 4121 4122 /** 4123 * Returns a hash code based on the contents of the specified array. If 4124 * the array contains other arrays as elements, the hash code is based on 4125 * their identities rather than their contents. It is therefore 4126 * acceptable to invoke this method on an array that contains itself as an 4127 * element, either directly or indirectly through one or more levels of 4128 * arrays. 4129 * 4130 * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 4131 * <tt>Arrays.equals(a, b)</tt>, it is also the case that 4132 * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 4133 * 4134 * <p>The value returned by this method is equal to the value that would 4135 * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt> 4136 * is <tt>null</tt>, in which case <tt>0</tt> is returned. 4137 * 4138 * @param a the array whose content-based hash code to compute 4139 * @return a content-based hash code for <tt>a</tt> 4140 * @see #deepHashCode(Object[]) 4141 * @since 1.5 4142 */ 4143 public static int hashCode(Object a[]) { 4144 if (a == null) 4145 return 0; 4146 4147 int result = 1; 4148 4149 for (Object element : a) 4150 result = 31 * result + (element == null ? 0 : element.hashCode()); 4151 4152 return result; 4153 } 4154 4155 /** 4156 * Returns a hash code based on the "deep contents" of the specified 4157 * array. If the array contains other arrays as elements, the 4158 * hash code is based on their contents and so on, ad infinitum. 4159 * It is therefore unacceptable to invoke this method on an array that 4160 * contains itself as an element, either directly or indirectly through 4161 * one or more levels of arrays. The behavior of such an invocation is 4162 * undefined. 4163 * 4164 * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 4165 * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that 4166 * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>. 4167 * 4168 * <p>The computation of the value returned by this method is similar to 4169 * that of the value returned by {@link List#hashCode()} on a list 4170 * containing the same elements as <tt>a</tt> in the same order, with one 4171 * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array, 4172 * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as 4173 * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt> 4174 * if <tt>e</tt> is an array of a primitive type, or as by calling 4175 * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array 4176 * of a reference type. If <tt>a</tt> is <tt>null</tt>, this method 4177 * returns 0. 4178 * 4179 * @param a the array whose deep-content-based hash code to compute 4180 * @return a deep-content-based hash code for <tt>a</tt> 4181 * @see #hashCode(Object[]) 4182 * @since 1.5 4183 */ 4184 public static int deepHashCode(Object a[]) { 4185 if (a == null) 4186 return 0; 4187 4188 int result = 1; 4189 4190 for (Object element : a) { 4191 int elementHash = 0; 4192 if (element instanceof Object[]) 4193 elementHash = deepHashCode((Object[]) element); 4194 else if (element instanceof byte[]) 4195 elementHash = hashCode((byte[]) element); 4196 else if (element instanceof short[]) 4197 elementHash = hashCode((short[]) element); 4198 else if (element instanceof int[]) 4199 elementHash = hashCode((int[]) element); 4200 else if (element instanceof long[]) 4201 elementHash = hashCode((long[]) element); 4202 else if (element instanceof char[]) 4203 elementHash = hashCode((char[]) element); 4204 else if (element instanceof float[]) 4205 elementHash = hashCode((float[]) element); 4206 else if (element instanceof double[]) 4207 elementHash = hashCode((double[]) element); 4208 else if (element instanceof boolean[]) 4209 elementHash = hashCode((boolean[]) element); 4210 else if (element != null) 4211 elementHash = element.hashCode(); 4212 4213 result = 31 * result + elementHash; 4214 } 4215 4216 return result; 4217 } 4218 4219 /** 4220 * Returns <tt>true</tt> if the two specified arrays are <i>deeply 4221 * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} 4222 * method, this method is appropriate for use with nested arrays of 4223 * arbitrary depth. 4224 * 4225 * <p>Two array references are considered deeply equal if both 4226 * are <tt>null</tt>, or if they refer to arrays that contain the same 4227 * number of elements and all corresponding pairs of elements in the two 4228 * arrays are deeply equal. 4229 * 4230 * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are 4231 * deeply equal if any of the following conditions hold: 4232 * <ul> 4233 * <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference 4234 * types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt> 4235 * <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive 4236 * type, and the appropriate overloading of 4237 * <tt>Arrays.equals(e1, e2)</tt> would return true. 4238 * <li> <tt>e1 == e2</tt> 4239 * <li> <tt>e1.equals(e2)</tt> would return true. 4240 * </ul> 4241 * Note that this definition permits <tt>null</tt> elements at any depth. 4242 * 4243 * <p>If either of the specified arrays contain themselves as elements 4244 * either directly or indirectly through one or more levels of arrays, 4245 * the behavior of this method is undefined. 4246 * 4247 * @param a1 one array to be tested for equality 4248 * @param a2 the other array to be tested for equality 4249 * @return <tt>true</tt> if the two arrays are equal 4250 * @see #equals(Object[],Object[]) 4251 * @see Objects#deepEquals(Object, Object) 4252 * @since 1.5 4253 */ 4254 public static boolean deepEquals(Object[] a1, Object[] a2) { 4255 if (a1 == a2) 4256 return true; 4257 if (a1 == null || a2==null) 4258 return false; 4259 int length = a1.length; 4260 if (a2.length != length) 4261 return false; 4262 4263 for (int i = 0; i < length; i++) { 4264 Object e1 = a1[i]; 4265 Object e2 = a2[i]; 4266 4267 if (e1 == e2) 4268 continue; 4269 if (e1 == null) 4270 return false; 4271 4272 // Figure out whether the two elements are equal 4273 boolean eq = deepEquals0(e1, e2); 4274 4275 if (!eq) 4276 return false; 4277 } 4278 return true; 4279 } 4280 4281 static boolean deepEquals0(Object e1, Object e2) { 4282 assert e1 != null; 4283 boolean eq; 4284 if (e1 instanceof Object[] && e2 instanceof Object[]) 4285 eq = deepEquals ((Object[]) e1, (Object[]) e2); 4286 else if (e1 instanceof byte[] && e2 instanceof byte[]) 4287 eq = equals((byte[]) e1, (byte[]) e2); 4288 else if (e1 instanceof short[] && e2 instanceof short[]) 4289 eq = equals((short[]) e1, (short[]) e2); 4290 else if (e1 instanceof int[] && e2 instanceof int[]) 4291 eq = equals((int[]) e1, (int[]) e2); 4292 else if (e1 instanceof long[] && e2 instanceof long[]) 4293 eq = equals((long[]) e1, (long[]) e2); 4294 else if (e1 instanceof char[] && e2 instanceof char[]) 4295 eq = equals((char[]) e1, (char[]) e2); 4296 else if (e1 instanceof float[] && e2 instanceof float[]) 4297 eq = equals((float[]) e1, (float[]) e2); 4298 else if (e1 instanceof double[] && e2 instanceof double[]) 4299 eq = equals((double[]) e1, (double[]) e2); 4300 else if (e1 instanceof boolean[] && e2 instanceof boolean[]) 4301 eq = equals((boolean[]) e1, (boolean[]) e2); 4302 else 4303 eq = e1.equals(e2); 4304 return eq; 4305 } 4306 4307 /** 4308 * Returns a string representation of the contents of the specified array. 4309 * The string representation consists of a list of the array's elements, 4310 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4311 * separated by the characters <tt>", "</tt> (a comma followed by a 4312 * space). Elements are converted to strings as by 4313 * <tt>String.valueOf(long)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 4314 * is <tt>null</tt>. 4315 * 4316 * @param a the array whose string representation to return 4317 * @return a string representation of <tt>a</tt> 4318 * @since 1.5 4319 */ 4320 public static String toString(long[] a) { 4321 if (a == null) 4322 return "null"; 4323 int iMax = a.length - 1; 4324 if (iMax == -1) 4325 return "[]"; 4326 4327 StringBuilder b = new StringBuilder(); 4328 b.append('['); 4329 for (int i = 0; ; i++) { 4330 b.append(a[i]); 4331 if (i == iMax) 4332 return b.append(']').toString(); 4333 b.append(", "); 4334 } 4335 } 4336 4337 /** 4338 * Returns a string representation of the contents of the specified array. 4339 * The string representation consists of a list of the array's elements, 4340 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4341 * separated by the characters <tt>", "</tt> (a comma followed by a 4342 * space). Elements are converted to strings as by 4343 * <tt>String.valueOf(int)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> is 4344 * <tt>null</tt>. 4345 * 4346 * @param a the array whose string representation to return 4347 * @return a string representation of <tt>a</tt> 4348 * @since 1.5 4349 */ 4350 public static String toString(int[] a) { 4351 if (a == null) 4352 return "null"; 4353 int iMax = a.length - 1; 4354 if (iMax == -1) 4355 return "[]"; 4356 4357 StringBuilder b = new StringBuilder(); 4358 b.append('['); 4359 for (int i = 0; ; i++) { 4360 b.append(a[i]); 4361 if (i == iMax) 4362 return b.append(']').toString(); 4363 b.append(", "); 4364 } 4365 } 4366 4367 /** 4368 * Returns a string representation of the contents of the specified array. 4369 * The string representation consists of a list of the array's elements, 4370 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4371 * separated by the characters <tt>", "</tt> (a comma followed by a 4372 * space). Elements are converted to strings as by 4373 * <tt>String.valueOf(short)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 4374 * is <tt>null</tt>. 4375 * 4376 * @param a the array whose string representation to return 4377 * @return a string representation of <tt>a</tt> 4378 * @since 1.5 4379 */ 4380 public static String toString(short[] a) { 4381 if (a == null) 4382 return "null"; 4383 int iMax = a.length - 1; 4384 if (iMax == -1) 4385 return "[]"; 4386 4387 StringBuilder b = new StringBuilder(); 4388 b.append('['); 4389 for (int i = 0; ; i++) { 4390 b.append(a[i]); 4391 if (i == iMax) 4392 return b.append(']').toString(); 4393 b.append(", "); 4394 } 4395 } 4396 4397 /** 4398 * Returns a string representation of the contents of the specified array. 4399 * The string representation consists of a list of the array's elements, 4400 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4401 * separated by the characters <tt>", "</tt> (a comma followed by a 4402 * space). Elements are converted to strings as by 4403 * <tt>String.valueOf(char)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 4404 * is <tt>null</tt>. 4405 * 4406 * @param a the array whose string representation to return 4407 * @return a string representation of <tt>a</tt> 4408 * @since 1.5 4409 */ 4410 public static String toString(char[] a) { 4411 if (a == null) 4412 return "null"; 4413 int iMax = a.length - 1; 4414 if (iMax == -1) 4415 return "[]"; 4416 4417 StringBuilder b = new StringBuilder(); 4418 b.append('['); 4419 for (int i = 0; ; i++) { 4420 b.append(a[i]); 4421 if (i == iMax) 4422 return b.append(']').toString(); 4423 b.append(", "); 4424 } 4425 } 4426 4427 /** 4428 * Returns a string representation of the contents of the specified array. 4429 * The string representation consists of a list of the array's elements, 4430 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements 4431 * are separated by the characters <tt>", "</tt> (a comma followed 4432 * by a space). Elements are converted to strings as by 4433 * <tt>String.valueOf(byte)</tt>. Returns <tt>"null"</tt> if 4434 * <tt>a</tt> is <tt>null</tt>. 4435 * 4436 * @param a the array whose string representation to return 4437 * @return a string representation of <tt>a</tt> 4438 * @since 1.5 4439 */ 4440 public static String toString(byte[] a) { 4441 if (a == null) 4442 return "null"; 4443 int iMax = a.length - 1; 4444 if (iMax == -1) 4445 return "[]"; 4446 4447 StringBuilder b = new StringBuilder(); 4448 b.append('['); 4449 for (int i = 0; ; i++) { 4450 b.append(a[i]); 4451 if (i == iMax) 4452 return b.append(']').toString(); 4453 b.append(", "); 4454 } 4455 } 4456 4457 /** 4458 * Returns a string representation of the contents of the specified array. 4459 * The string representation consists of a list of the array's elements, 4460 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4461 * separated by the characters <tt>", "</tt> (a comma followed by a 4462 * space). Elements are converted to strings as by 4463 * <tt>String.valueOf(boolean)</tt>. Returns <tt>"null"</tt> if 4464 * <tt>a</tt> is <tt>null</tt>. 4465 * 4466 * @param a the array whose string representation to return 4467 * @return a string representation of <tt>a</tt> 4468 * @since 1.5 4469 */ 4470 public static String toString(boolean[] a) { 4471 if (a == null) 4472 return "null"; 4473 int iMax = a.length - 1; 4474 if (iMax == -1) 4475 return "[]"; 4476 4477 StringBuilder b = new StringBuilder(); 4478 b.append('['); 4479 for (int i = 0; ; i++) { 4480 b.append(a[i]); 4481 if (i == iMax) 4482 return b.append(']').toString(); 4483 b.append(", "); 4484 } 4485 } 4486 4487 /** 4488 * Returns a string representation of the contents of the specified array. 4489 * The string representation consists of a list of the array's elements, 4490 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4491 * separated by the characters <tt>", "</tt> (a comma followed by a 4492 * space). Elements are converted to strings as by 4493 * <tt>String.valueOf(float)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 4494 * is <tt>null</tt>. 4495 * 4496 * @param a the array whose string representation to return 4497 * @return a string representation of <tt>a</tt> 4498 * @since 1.5 4499 */ 4500 public static String toString(float[] a) { 4501 if (a == null) 4502 return "null"; 4503 4504 int iMax = a.length - 1; 4505 if (iMax == -1) 4506 return "[]"; 4507 4508 StringBuilder b = new StringBuilder(); 4509 b.append('['); 4510 for (int i = 0; ; i++) { 4511 b.append(a[i]); 4512 if (i == iMax) 4513 return b.append(']').toString(); 4514 b.append(", "); 4515 } 4516 } 4517 4518 /** 4519 * Returns a string representation of the contents of the specified array. 4520 * The string representation consists of a list of the array's elements, 4521 * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 4522 * separated by the characters <tt>", "</tt> (a comma followed by a 4523 * space). Elements are converted to strings as by 4524 * <tt>String.valueOf(double)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 4525 * is <tt>null</tt>. 4526 * 4527 * @param a the array whose string representation to return 4528 * @return a string representation of <tt>a</tt> 4529 * @since 1.5 4530 */ 4531 public static String toString(double[] a) { 4532 if (a == null) 4533 return "null"; 4534 int iMax = a.length - 1; 4535 if (iMax == -1) 4536 return "[]"; 4537 4538 StringBuilder b = new StringBuilder(); 4539 b.append('['); 4540 for (int i = 0; ; i++) { 4541 b.append(a[i]); 4542 if (i == iMax) 4543 return b.append(']').toString(); 4544 b.append(", "); 4545 } 4546 } 4547 4548 /** 4549 * Returns a string representation of the contents of the specified array. 4550 * If the array contains other arrays as elements, they are converted to 4551 * strings by the {@link Object#toString} method inherited from 4552 * <tt>Object</tt>, which describes their <i>identities</i> rather than 4553 * their contents. 4554 * 4555 * <p>The value returned by this method is equal to the value that would 4556 * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt> 4557 * is <tt>null</tt>, in which case <tt>"null"</tt> is returned. 4558 * 4559 * @param a the array whose string representation to return 4560 * @return a string representation of <tt>a</tt> 4561 * @see #deepToString(Object[]) 4562 * @since 1.5 4563 */ 4564 public static String toString(Object[] a) { 4565 if (a == null) 4566 return "null"; 4567 4568 int iMax = a.length - 1; 4569 if (iMax == -1) 4570 return "[]"; 4571 4572 StringBuilder b = new StringBuilder(); 4573 b.append('['); 4574 for (int i = 0; ; i++) { 4575 b.append(String.valueOf(a[i])); 4576 if (i == iMax) 4577 return b.append(']').toString(); 4578 b.append(", "); 4579 } 4580 } 4581 4582 /** 4583 * Returns a string representation of the "deep contents" of the specified 4584 * array. If the array contains other arrays as elements, the string 4585 * representation contains their contents and so on. This method is 4586 * designed for converting multidimensional arrays to strings. 4587 * 4588 * <p>The string representation consists of a list of the array's 4589 * elements, enclosed in square brackets (<tt>"[]"</tt>). Adjacent 4590 * elements are separated by the characters <tt>", "</tt> (a comma 4591 * followed by a space). Elements are converted to strings as by 4592 * <tt>String.valueOf(Object)</tt>, unless they are themselves 4593 * arrays. 4594 * 4595 * <p>If an element <tt>e</tt> is an array of a primitive type, it is 4596 * converted to a string as by invoking the appropriate overloading of 4597 * <tt>Arrays.toString(e)</tt>. If an element <tt>e</tt> is an array of a 4598 * reference type, it is converted to a string as by invoking 4599 * this method recursively. 4600 * 4601 * <p>To avoid infinite recursion, if the specified array contains itself 4602 * as an element, or contains an indirect reference to itself through one 4603 * or more levels of arrays, the self-reference is converted to the string 4604 * <tt>"[...]"</tt>. For example, an array containing only a reference 4605 * to itself would be rendered as <tt>"[[...]]"</tt>. 4606 * 4607 * <p>This method returns <tt>"null"</tt> if the specified array 4608 * is <tt>null</tt>. 4609 * 4610 * @param a the array whose string representation to return 4611 * @return a string representation of <tt>a</tt> 4612 * @see #toString(Object[]) 4613 * @since 1.5 4614 */ 4615 public static String deepToString(Object[] a) { 4616 if (a == null) 4617 return "null"; 4618 4619 int bufLen = 20 * a.length; 4620 if (a.length != 0 && bufLen <= 0) 4621 bufLen = Integer.MAX_VALUE; 4622 StringBuilder buf = new StringBuilder(bufLen); 4623 deepToString(a, buf, new HashSet<>()); 4624 return buf.toString(); 4625 } 4626 4627 private static void deepToString(Object[] a, StringBuilder buf, 4628 Set<Object[]> dejaVu) { 4629 if (a == null) { 4630 buf.append("null"); 4631 return; 4632 } 4633 int iMax = a.length - 1; 4634 if (iMax == -1) { 4635 buf.append("[]"); 4636 return; 4637 } 4638 4639 dejaVu.add(a); 4640 buf.append('['); 4641 for (int i = 0; ; i++) { 4642 4643 Object element = a[i]; 4644 if (element == null) { 4645 buf.append("null"); 4646 } else { 4647 Class<?> eClass = element.getClass(); 4648 4649 if (eClass.isArray()) { 4650 if (eClass == byte[].class) 4651 buf.append(toString((byte[]) element)); 4652 else if (eClass == short[].class) 4653 buf.append(toString((short[]) element)); 4654 else if (eClass == int[].class) 4655 buf.append(toString((int[]) element)); 4656 else if (eClass == long[].class) 4657 buf.append(toString((long[]) element)); 4658 else if (eClass == char[].class) 4659 buf.append(toString((char[]) element)); 4660 else if (eClass == float[].class) 4661 buf.append(toString((float[]) element)); 4662 else if (eClass == double[].class) 4663 buf.append(toString((double[]) element)); 4664 else if (eClass == boolean[].class) 4665 buf.append(toString((boolean[]) element)); 4666 else { // element is an array of object references 4667 if (dejaVu.contains(element)) 4668 buf.append("[...]"); 4669 else 4670 deepToString((Object[])element, buf, dejaVu); 4671 } 4672 } else { // element is non-null and not an array 4673 buf.append(element.toString()); 4674 } 4675 } 4676 if (i == iMax) 4677 break; 4678 buf.append(", "); 4679 } 4680 buf.append(']'); 4681 dejaVu.remove(a); 4682 } 4683 4684 4685 /** 4686 * Set all elements of the specified array, using the provided 4687 * generator function to compute each element. 4688 * 4689 * <p>If the generator function throws an exception, it is relayed to 4690 * the caller and the array is left in an indeterminate state. 4691 * 4692 * @apiNote 4693 * Setting a subrange of an array, using a generator function to compute 4694 * each element, can be written as follows: 4695 * <pre>{@code 4696 * IntStream.range(startInclusive, endExclusive) 4697 * .forEach(i -> array[i] = generator.apply(i)); 4698 * }</pre> 4699 * 4700 * @param <T> type of elements of the array 4701 * @param array array to be initialized 4702 * @param generator a function accepting an index and producing the desired 4703 * value for that position 4704 * @throws NullPointerException if the generator is null 4705 * @since 1.8 4706 */ 4707 public static <T> void setAll(T[] array, IntFunction<? extends T> generator) { 4708 Objects.requireNonNull(generator); 4709 for (int i = 0; i < array.length; i++) 4710 array[i] = generator.apply(i); 4711 } 4712 4713 /** 4714 * Set all elements of the specified array, in parallel, using the 4715 * provided generator function to compute each element. 4716 * 4717 * <p>If the generator function throws an exception, an unchecked exception 4718 * is thrown from {@code parallelSetAll} and the array is left in an 4719 * indeterminate state. 4720 * 4721 * @apiNote 4722 * Setting a subrange of an array, in parallel, using a generator function 4723 * to compute each element, can be written as follows: 4724 * <pre>{@code 4725 * IntStream.range(startInclusive, endExclusive) 4726 * .parallel() 4727 * .forEach(i -> array[i] = generator.apply(i)); 4728 * }</pre> 4729 * 4730 * @param <T> type of elements of the array 4731 * @param array array to be initialized 4732 * @param generator a function accepting an index and producing the desired 4733 * value for that position 4734 * @throws NullPointerException if the generator is null 4735 * @since 1.8 4736 */ 4737 public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) { 4738 Objects.requireNonNull(generator); 4739 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); }); 4740 } 4741 4742 /** 4743 * Set all elements of the specified array, using the provided 4744 * generator function to compute each element. 4745 * 4746 * <p>If the generator function throws an exception, it is relayed to 4747 * the caller and the array is left in an indeterminate state. 4748 * 4749 * @apiNote 4750 * Setting a subrange of an array, using a generator function to compute 4751 * each element, can be written as follows: 4752 * <pre>{@code 4753 * IntStream.range(startInclusive, endExclusive) 4754 * .forEach(i -> array[i] = generator.applyAsInt(i)); 4755 * }</pre> 4756 * 4757 * @param array array to be initialized 4758 * @param generator a function accepting an index and producing the desired 4759 * value for that position 4760 * @throws NullPointerException if the generator is null 4761 * @since 1.8 4762 */ 4763 public static void setAll(int[] array, IntUnaryOperator generator) { 4764 Objects.requireNonNull(generator); 4765 for (int i = 0; i < array.length; i++) 4766 array[i] = generator.applyAsInt(i); 4767 } 4768 4769 /** 4770 * Set all elements of the specified array, in parallel, using the 4771 * provided generator function to compute each element. 4772 * 4773 * <p>If the generator function throws an exception, an unchecked exception 4774 * is thrown from {@code parallelSetAll} and the array is left in an 4775 * indeterminate state. 4776 * 4777 * @apiNote 4778 * Setting a subrange of an array, in parallel, using a generator function 4779 * to compute each element, can be written as follows: 4780 * <pre>{@code 4781 * IntStream.range(startInclusive, endExclusive) 4782 * .parallel() 4783 * .forEach(i -> array[i] = generator.applyAsInt(i)); 4784 * }</pre> 4785 * 4786 * @param array array to be initialized 4787 * @param generator a function accepting an index and producing the desired 4788 * value for that position 4789 * @throws NullPointerException if the generator is null 4790 * @since 1.8 4791 */ 4792 public static void parallelSetAll(int[] array, IntUnaryOperator generator) { 4793 Objects.requireNonNull(generator); 4794 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); }); 4795 } 4796 4797 /** 4798 * Set all elements of the specified array, using the provided 4799 * generator function to compute each element. 4800 * 4801 * <p>If the generator function throws an exception, it is relayed to 4802 * the caller and the array is left in an indeterminate state. 4803 * 4804 * @apiNote 4805 * Setting a subrange of an array, using a generator function to compute 4806 * each element, can be written as follows: 4807 * <pre>{@code 4808 * IntStream.range(startInclusive, endExclusive) 4809 * .forEach(i -> array[i] = generator.applyAsLong(i)); 4810 * }</pre> 4811 * 4812 * @param array array to be initialized 4813 * @param generator a function accepting an index and producing the desired 4814 * value for that position 4815 * @throws NullPointerException if the generator is null 4816 * @since 1.8 4817 */ 4818 public static void setAll(long[] array, IntToLongFunction generator) { 4819 Objects.requireNonNull(generator); 4820 for (int i = 0; i < array.length; i++) 4821 array[i] = generator.applyAsLong(i); 4822 } 4823 4824 /** 4825 * Set all elements of the specified array, in parallel, using the 4826 * provided generator function to compute each element. 4827 * 4828 * <p>If the generator function throws an exception, an unchecked exception 4829 * is thrown from {@code parallelSetAll} and the array is left in an 4830 * indeterminate state. 4831 * 4832 * @apiNote 4833 * Setting a subrange of an array, in parallel, using a generator function 4834 * to compute each element, can be written as follows: 4835 * <pre>{@code 4836 * IntStream.range(startInclusive, endExclusive) 4837 * .parallel() 4838 * .forEach(i -> array[i] = generator.applyAsLong(i)); 4839 * }</pre> 4840 * 4841 * @param array array to be initialized 4842 * @param generator a function accepting an index and producing the desired 4843 * value for that position 4844 * @throws NullPointerException if the generator is null 4845 * @since 1.8 4846 */ 4847 public static void parallelSetAll(long[] array, IntToLongFunction generator) { 4848 Objects.requireNonNull(generator); 4849 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); }); 4850 } 4851 4852 /** 4853 * Set all elements of the specified array, using the provided 4854 * generator function to compute each element. 4855 * 4856 * <p>If the generator function throws an exception, it is relayed to 4857 * the caller and the array is left in an indeterminate state. 4858 * 4859 * @apiNote 4860 * Setting a subrange of an array, using a generator function to compute 4861 * each element, can be written as follows: 4862 * <pre>{@code 4863 * IntStream.range(startInclusive, endExclusive) 4864 * .forEach(i -> array[i] = generator.applyAsDouble(i)); 4865 * }</pre> 4866 * 4867 * @param array array to be initialized 4868 * @param generator a function accepting an index and producing the desired 4869 * value for that position 4870 * @throws NullPointerException if the generator is null 4871 * @since 1.8 4872 */ 4873 public static void setAll(double[] array, IntToDoubleFunction generator) { 4874 Objects.requireNonNull(generator); 4875 for (int i = 0; i < array.length; i++) 4876 array[i] = generator.applyAsDouble(i); 4877 } 4878 4879 /** 4880 * Set all elements of the specified array, in parallel, using the 4881 * provided generator function to compute each element. 4882 * 4883 * <p>If the generator function throws an exception, an unchecked exception 4884 * is thrown from {@code parallelSetAll} and the array is left in an 4885 * indeterminate state. 4886 * 4887 * @apiNote 4888 * Setting a subrange of an array, in parallel, using a generator function 4889 * to compute each element, can be written as follows: 4890 * <pre>{@code 4891 * IntStream.range(startInclusive, endExclusive) 4892 * .parallel() 4893 * .forEach(i -> array[i] = generator.applyAsDouble(i)); 4894 * }</pre> 4895 * 4896 * @param array array to be initialized 4897 * @param generator a function accepting an index and producing the desired 4898 * value for that position 4899 * @throws NullPointerException if the generator is null 4900 * @since 1.8 4901 */ 4902 public static void parallelSetAll(double[] array, IntToDoubleFunction generator) { 4903 Objects.requireNonNull(generator); 4904 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); }); 4905 } 4906 4907 /** 4908 * Returns a {@link Spliterator} covering all of the specified array. 4909 * 4910 * <p>The spliterator reports {@link Spliterator#SIZED}, 4911 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 4912 * {@link Spliterator#IMMUTABLE}. 4913 * 4914 * @param <T> type of elements 4915 * @param array the array, assumed to be unmodified during use 4916 * @return a spliterator for the array elements 4917 * @since 1.8 4918 */ 4919 public static <T> Spliterator<T> spliterator(T[] array) { 4920 return Spliterators.spliterator(array, 4921 Spliterator.ORDERED | Spliterator.IMMUTABLE); 4922 } 4923 4924 /** 4925 * Returns a {@link Spliterator} covering the specified range of the 4926 * specified array. 4927 * 4928 * <p>The spliterator reports {@link Spliterator#SIZED}, 4929 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 4930 * {@link Spliterator#IMMUTABLE}. 4931 * 4932 * @param <T> type of elements 4933 * @param array the array, assumed to be unmodified during use 4934 * @param startInclusive the first index to cover, inclusive 4935 * @param endExclusive index immediately past the last index to cover 4936 * @return a spliterator for the array elements 4937 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4938 * negative, {@code endExclusive} is less than 4939 * {@code startInclusive}, or {@code endExclusive} is greater than 4940 * the array size 4941 * @since 1.8 4942 */ 4943 public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) { 4944 return Spliterators.spliterator(array, startInclusive, endExclusive, 4945 Spliterator.ORDERED | Spliterator.IMMUTABLE); 4946 } 4947 4948 /** 4949 * Returns a {@link Spliterator.OfInt} covering all of the specified array. 4950 * 4951 * <p>The spliterator reports {@link Spliterator#SIZED}, 4952 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 4953 * {@link Spliterator#IMMUTABLE}. 4954 * 4955 * @param array the array, assumed to be unmodified during use 4956 * @return a spliterator for the array elements 4957 * @since 1.8 4958 */ 4959 public static Spliterator.OfInt spliterator(int[] array) { 4960 return Spliterators.spliterator(array, 4961 Spliterator.ORDERED | Spliterator.IMMUTABLE); 4962 } 4963 4964 /** 4965 * Returns a {@link Spliterator.OfInt} covering the specified range of the 4966 * specified array. 4967 * 4968 * <p>The spliterator reports {@link Spliterator#SIZED}, 4969 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 4970 * {@link Spliterator#IMMUTABLE}. 4971 * 4972 * @param array the array, assumed to be unmodified during use 4973 * @param startInclusive the first index to cover, inclusive 4974 * @param endExclusive index immediately past the last index to cover 4975 * @return a spliterator for the array elements 4976 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4977 * negative, {@code endExclusive} is less than 4978 * {@code startInclusive}, or {@code endExclusive} is greater than 4979 * the array size 4980 * @since 1.8 4981 */ 4982 public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) { 4983 return Spliterators.spliterator(array, startInclusive, endExclusive, 4984 Spliterator.ORDERED | Spliterator.IMMUTABLE); 4985 } 4986 4987 /** 4988 * Returns a {@link Spliterator.OfLong} covering all of the specified array. 4989 * 4990 * <p>The spliterator reports {@link Spliterator#SIZED}, 4991 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 4992 * {@link Spliterator#IMMUTABLE}. 4993 * 4994 * @param array the array, assumed to be unmodified during use 4995 * @return the spliterator for the array elements 4996 * @since 1.8 4997 */ 4998 public static Spliterator.OfLong spliterator(long[] array) { 4999 return Spliterators.spliterator(array, 5000 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5001 } 5002 5003 /** 5004 * Returns a {@link Spliterator.OfLong} covering the specified range of the 5005 * specified array. 5006 * 5007 * <p>The spliterator reports {@link Spliterator#SIZED}, 5008 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5009 * {@link Spliterator#IMMUTABLE}. 5010 * 5011 * @param array the array, assumed to be unmodified during use 5012 * @param startInclusive the first index to cover, inclusive 5013 * @param endExclusive index immediately past the last index to cover 5014 * @return a spliterator for the array elements 5015 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5016 * negative, {@code endExclusive} is less than 5017 * {@code startInclusive}, or {@code endExclusive} is greater than 5018 * the array size 5019 * @since 1.8 5020 */ 5021 public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) { 5022 return Spliterators.spliterator(array, startInclusive, endExclusive, 5023 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5024 } 5025 5026 /** 5027 * Returns a {@link Spliterator.OfDouble} covering all of the specified 5028 * array. 5029 * 5030 * <p>The spliterator reports {@link Spliterator#SIZED}, 5031 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5032 * {@link Spliterator#IMMUTABLE}. 5033 * 5034 * @param array the array, assumed to be unmodified during use 5035 * @return a spliterator for the array elements 5036 * @since 1.8 5037 */ 5038 public static Spliterator.OfDouble spliterator(double[] array) { 5039 return Spliterators.spliterator(array, 5040 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5041 } 5042 5043 /** 5044 * Returns a {@link Spliterator.OfDouble} covering the specified range of 5045 * the specified array. 5046 * 5047 * <p>The spliterator reports {@link Spliterator#SIZED}, 5048 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 5049 * {@link Spliterator#IMMUTABLE}. 5050 * 5051 * @param array the array, assumed to be unmodified during use 5052 * @param startInclusive the first index to cover, inclusive 5053 * @param endExclusive index immediately past the last index to cover 5054 * @return a spliterator for the array elements 5055 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5056 * negative, {@code endExclusive} is less than 5057 * {@code startInclusive}, or {@code endExclusive} is greater than 5058 * the array size 5059 * @since 1.8 5060 */ 5061 public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) { 5062 return Spliterators.spliterator(array, startInclusive, endExclusive, 5063 Spliterator.ORDERED | Spliterator.IMMUTABLE); 5064 } 5065 5066 /** 5067 * Returns a sequential {@link Stream} with the specified array as its 5068 * source. 5069 * 5070 * @param <T> The type of the array elements 5071 * @param array The array, assumed to be unmodified during use 5072 * @return a {@code Stream} for the array 5073 * @since 1.8 5074 */ 5075 public static <T> Stream<T> stream(T[] array) { 5076 return stream(array, 0, array.length); 5077 } 5078 5079 /** 5080 * Returns a sequential {@link Stream} with the specified range of the 5081 * specified array as its source. 5082 * 5083 * @param <T> the type of the array elements 5084 * @param array the array, assumed to be unmodified during use 5085 * @param startInclusive the first index to cover, inclusive 5086 * @param endExclusive index immediately past the last index to cover 5087 * @return a {@code Stream} for the array range 5088 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5089 * negative, {@code endExclusive} is less than 5090 * {@code startInclusive}, or {@code endExclusive} is greater than 5091 * the array size 5092 * @since 1.8 5093 */ 5094 public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) { 5095 return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false); 5096 } 5097 5098 /** 5099 * Returns a sequential {@link IntStream} with the specified array as its 5100 * source. 5101 * 5102 * @param array the array, assumed to be unmodified during use 5103 * @return an {@code IntStream} for the array 5104 * @since 1.8 5105 */ 5106 public static IntStream stream(int[] array) { 5107 return stream(array, 0, array.length); 5108 } 5109 5110 /** 5111 * Returns a sequential {@link IntStream} with the specified range of the 5112 * specified array as its source. 5113 * 5114 * @param array the array, assumed to be unmodified during use 5115 * @param startInclusive the first index to cover, inclusive 5116 * @param endExclusive index immediately past the last index to cover 5117 * @return an {@code IntStream} for the array range 5118 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5119 * negative, {@code endExclusive} is less than 5120 * {@code startInclusive}, or {@code endExclusive} is greater than 5121 * the array size 5122 * @since 1.8 5123 */ 5124 public static IntStream stream(int[] array, int startInclusive, int endExclusive) { 5125 return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false); 5126 } 5127 5128 /** 5129 * Returns a sequential {@link LongStream} with the specified array as its 5130 * source. 5131 * 5132 * @param array the array, assumed to be unmodified during use 5133 * @return a {@code LongStream} for the array 5134 * @since 1.8 5135 */ 5136 public static LongStream stream(long[] array) { 5137 return stream(array, 0, array.length); 5138 } 5139 5140 /** 5141 * Returns a sequential {@link LongStream} with the specified range of the 5142 * specified array as its source. 5143 * 5144 * @param array the array, assumed to be unmodified during use 5145 * @param startInclusive the first index to cover, inclusive 5146 * @param endExclusive index immediately past the last index to cover 5147 * @return a {@code LongStream} for the array range 5148 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5149 * negative, {@code endExclusive} is less than 5150 * {@code startInclusive}, or {@code endExclusive} is greater than 5151 * the array size 5152 * @since 1.8 5153 */ 5154 public static LongStream stream(long[] array, int startInclusive, int endExclusive) { 5155 return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false); 5156 } 5157 5158 /** 5159 * Returns a sequential {@link DoubleStream} with the specified array as its 5160 * source. 5161 * 5162 * @param array the array, assumed to be unmodified during use 5163 * @return a {@code DoubleStream} for the array 5164 * @since 1.8 5165 */ 5166 public static DoubleStream stream(double[] array) { 5167 return stream(array, 0, array.length); 5168 } 5169 5170 /** 5171 * Returns a sequential {@link DoubleStream} with the specified range of the 5172 * specified array as its source. 5173 * 5174 * @param array the array, assumed to be unmodified during use 5175 * @param startInclusive the first index to cover, inclusive 5176 * @param endExclusive index immediately past the last index to cover 5177 * @return a {@code DoubleStream} for the array range 5178 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 5179 * negative, {@code endExclusive} is less than 5180 * {@code startInclusive}, or {@code endExclusive} is greater than 5181 * the array size 5182 * @since 1.8 5183 */ 5184 public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) { 5185 return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false); 5186 } 5187 }