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