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