25
26 package java.util;
27
28 import java.lang.reflect.Array;
29 import java.util.concurrent.ForkJoinPool;
30 import java.util.function.BinaryOperator;
31 import java.util.function.Consumer;
32 import java.util.function.DoubleBinaryOperator;
33 import java.util.function.IntBinaryOperator;
34 import java.util.function.IntFunction;
35 import java.util.function.IntToDoubleFunction;
36 import java.util.function.IntToLongFunction;
37 import java.util.function.IntUnaryOperator;
38 import java.util.function.LongBinaryOperator;
39 import java.util.function.UnaryOperator;
40 import java.util.stream.DoubleStream;
41 import java.util.stream.IntStream;
42 import java.util.stream.LongStream;
43 import java.util.stream.Stream;
44 import java.util.stream.StreamSupport;
45
46 /**
47 * This class contains various methods for manipulating arrays (such as
48 * sorting and searching). This class also contains a static factory
49 * that allows arrays to be viewed as lists.
50 *
51 * <p>The methods in this class all throw a {@code NullPointerException},
52 * if the specified array reference is null, except where noted.
53 *
54 * <p>The documentation for the methods contained in this class includes
55 * brief descriptions of the <i>implementations</i>. Such descriptions should
56 * be regarded as <i>implementation notes</i>, rather than parts of the
57 * <i>specification</i>. Implementors should feel free to substitute other
58 * algorithms, so long as the specification itself is adhered to. (For
59 * example, the algorithm used by {@code sort(Object[])} does not have to be
60 * a MergeSort, but it does have to be <i>stable</i>.)
61 *
62 * <p>This class is a member of the
63 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
64 * Java Collections Framework</a>.
2637
2638 for (int i=0; i<length; i++)
2639 if (a[i] != a2[i])
2640 return false;
2641
2642 return true;
2643 }
2644
2645 /**
2646 * Returns <tt>true</tt> if the two specified arrays of chars are
2647 * <i>equal</i> to one another. Two arrays are considered equal if both
2648 * arrays contain the same number of elements, and all corresponding pairs
2649 * of elements in the two arrays are equal. In other words, two arrays
2650 * are equal if they contain the same elements in the same order. Also,
2651 * two array references are considered equal if both are <tt>null</tt>.
2652 *
2653 * @param a one array to be tested for equality
2654 * @param a2 the other array to be tested for equality
2655 * @return <tt>true</tt> if the two arrays are equal
2656 */
2657 public static boolean equals(char[] a, char[] a2) {
2658 if (a==a2)
2659 return true;
2660 if (a==null || a2==null)
2661 return false;
2662
2663 int length = a.length;
2664 if (a2.length != length)
2665 return false;
2666
2667 for (int i=0; i<length; i++)
2668 if (a[i] != a2[i])
2669 return false;
2670
2671 return true;
2672 }
2673
2674 /**
2675 * Returns <tt>true</tt> if the two specified arrays of bytes are
2676 * <i>equal</i> to one another. Two arrays are considered equal if both
3188 * contain identical values. For any indices that are valid in the
3189 * copy but not the original, the copy will contain <tt>null</tt>.
3190 * Such indices will exist if and only if the specified length
3191 * is greater than that of the original array.
3192 * The resulting array is of the class <tt>newType</tt>.
3193 *
3194 * @param <U> the class of the objects in the original array
3195 * @param <T> the class of the objects in the returned array
3196 * @param original the array to be copied
3197 * @param newLength the length of the copy to be returned
3198 * @param newType the class of the copy to be returned
3199 * @return a copy of the original array, truncated or padded with nulls
3200 * to obtain the specified length
3201 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
3202 * @throws NullPointerException if <tt>original</tt> is null
3203 * @throws ArrayStoreException if an element copied from
3204 * <tt>original</tt> is not of a runtime type that can be stored in
3205 * an array of class <tt>newType</tt>
3206 * @since 1.6
3207 */
3208 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3209 @SuppressWarnings("unchecked")
3210 T[] copy = ((Object)newType == (Object)Object[].class)
3211 ? (T[]) new Object[newLength]
3212 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3213 System.arraycopy(original, 0, copy, 0,
3214 Math.min(original.length, newLength));
3215 return copy;
3216 }
3217
3218 /**
3219 * Copies the specified array, truncating or padding with zeros (if necessary)
3220 * so the copy has the specified length. For all indices that are
3221 * valid in both the original array and the copy, the two arrays will
3222 * contain identical values. For any indices that are valid in the
3223 * copy but not the original, the copy will contain <tt>(byte)0</tt>.
3224 * Such indices will exist if and only if the specified length
3225 * is greater than that of the original array.
3226 *
3227 * @param original the array to be copied
3457 * The resulting array is of the class <tt>newType</tt>.
3458 *
3459 * @param <U> the class of the objects in the original array
3460 * @param <T> the class of the objects in the returned array
3461 * @param original the array from which a range is to be copied
3462 * @param from the initial index of the range to be copied, inclusive
3463 * @param to the final index of the range to be copied, exclusive.
3464 * (This index may lie outside the array.)
3465 * @param newType the class of the copy to be returned
3466 * @return a new array containing the specified range from the original array,
3467 * truncated or padded with nulls to obtain the required length
3468 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3469 * or {@code from > original.length}
3470 * @throws IllegalArgumentException if <tt>from > to</tt>
3471 * @throws NullPointerException if <tt>original</tt> is null
3472 * @throws ArrayStoreException if an element copied from
3473 * <tt>original</tt> is not of a runtime type that can be stored in
3474 * an array of class <tt>newType</tt>.
3475 * @since 1.6
3476 */
3477 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3478 int newLength = to - from;
3479 if (newLength < 0)
3480 throw new IllegalArgumentException(from + " > " + to);
3481 @SuppressWarnings("unchecked")
3482 T[] copy = ((Object)newType == (Object)Object[].class)
3483 ? (T[]) new Object[newLength]
3484 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3485 System.arraycopy(original, from, copy, 0,
3486 Math.min(original.length - from, newLength));
3487 return copy;
3488 }
3489
3490 /**
3491 * Copies the specified range of the specified array into a new array.
3492 * The initial index of the range (<tt>from</tt>) must lie between zero
3493 * and <tt>original.length</tt>, inclusive. The value at
3494 * <tt>original[from]</tt> is placed into the initial element of the copy
3495 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
3496 * Values from subsequent elements in the original array are placed into
|
25
26 package java.util;
27
28 import java.lang.reflect.Array;
29 import java.util.concurrent.ForkJoinPool;
30 import java.util.function.BinaryOperator;
31 import java.util.function.Consumer;
32 import java.util.function.DoubleBinaryOperator;
33 import java.util.function.IntBinaryOperator;
34 import java.util.function.IntFunction;
35 import java.util.function.IntToDoubleFunction;
36 import java.util.function.IntToLongFunction;
37 import java.util.function.IntUnaryOperator;
38 import java.util.function.LongBinaryOperator;
39 import java.util.function.UnaryOperator;
40 import java.util.stream.DoubleStream;
41 import java.util.stream.IntStream;
42 import java.util.stream.LongStream;
43 import java.util.stream.Stream;
44 import java.util.stream.StreamSupport;
45 import jdk.internal.HotSpotIntrinsicCandidate;
46
47 /**
48 * This class contains various methods for manipulating arrays (such as
49 * sorting and searching). This class also contains a static factory
50 * that allows arrays to be viewed as lists.
51 *
52 * <p>The methods in this class all throw a {@code NullPointerException},
53 * if the specified array reference is null, except where noted.
54 *
55 * <p>The documentation for the methods contained in this class includes
56 * brief descriptions of the <i>implementations</i>. Such descriptions should
57 * be regarded as <i>implementation notes</i>, rather than parts of the
58 * <i>specification</i>. Implementors should feel free to substitute other
59 * algorithms, so long as the specification itself is adhered to. (For
60 * example, the algorithm used by {@code sort(Object[])} does not have to be
61 * a MergeSort, but it does have to be <i>stable</i>.)
62 *
63 * <p>This class is a member of the
64 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
65 * Java Collections Framework</a>.
2638
2639 for (int i=0; i<length; i++)
2640 if (a[i] != a2[i])
2641 return false;
2642
2643 return true;
2644 }
2645
2646 /**
2647 * Returns <tt>true</tt> if the two specified arrays of chars are
2648 * <i>equal</i> to one another. Two arrays are considered equal if both
2649 * arrays contain the same number of elements, and all corresponding pairs
2650 * of elements in the two arrays are equal. In other words, two arrays
2651 * are equal if they contain the same elements in the same order. Also,
2652 * two array references are considered equal if both are <tt>null</tt>.
2653 *
2654 * @param a one array to be tested for equality
2655 * @param a2 the other array to be tested for equality
2656 * @return <tt>true</tt> if the two arrays are equal
2657 */
2658 @HotSpotIntrinsicCandidate
2659 public static boolean equals(char[] a, char[] a2) {
2660 if (a==a2)
2661 return true;
2662 if (a==null || a2==null)
2663 return false;
2664
2665 int length = a.length;
2666 if (a2.length != length)
2667 return false;
2668
2669 for (int i=0; i<length; i++)
2670 if (a[i] != a2[i])
2671 return false;
2672
2673 return true;
2674 }
2675
2676 /**
2677 * Returns <tt>true</tt> if the two specified arrays of bytes are
2678 * <i>equal</i> to one another. Two arrays are considered equal if both
3190 * contain identical values. For any indices that are valid in the
3191 * copy but not the original, the copy will contain <tt>null</tt>.
3192 * Such indices will exist if and only if the specified length
3193 * is greater than that of the original array.
3194 * The resulting array is of the class <tt>newType</tt>.
3195 *
3196 * @param <U> the class of the objects in the original array
3197 * @param <T> the class of the objects in the returned array
3198 * @param original the array to be copied
3199 * @param newLength the length of the copy to be returned
3200 * @param newType the class of the copy to be returned
3201 * @return a copy of the original array, truncated or padded with nulls
3202 * to obtain the specified length
3203 * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
3204 * @throws NullPointerException if <tt>original</tt> is null
3205 * @throws ArrayStoreException if an element copied from
3206 * <tt>original</tt> is not of a runtime type that can be stored in
3207 * an array of class <tt>newType</tt>
3208 * @since 1.6
3209 */
3210 @HotSpotIntrinsicCandidate
3211 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3212 @SuppressWarnings("unchecked")
3213 T[] copy = ((Object)newType == (Object)Object[].class)
3214 ? (T[]) new Object[newLength]
3215 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3216 System.arraycopy(original, 0, copy, 0,
3217 Math.min(original.length, newLength));
3218 return copy;
3219 }
3220
3221 /**
3222 * Copies the specified array, truncating or padding with zeros (if necessary)
3223 * so the copy has the specified length. For all indices that are
3224 * valid in both the original array and the copy, the two arrays will
3225 * contain identical values. For any indices that are valid in the
3226 * copy but not the original, the copy will contain <tt>(byte)0</tt>.
3227 * Such indices will exist if and only if the specified length
3228 * is greater than that of the original array.
3229 *
3230 * @param original the array to be copied
3460 * The resulting array is of the class <tt>newType</tt>.
3461 *
3462 * @param <U> the class of the objects in the original array
3463 * @param <T> the class of the objects in the returned array
3464 * @param original the array from which a range is to be copied
3465 * @param from the initial index of the range to be copied, inclusive
3466 * @param to the final index of the range to be copied, exclusive.
3467 * (This index may lie outside the array.)
3468 * @param newType the class of the copy to be returned
3469 * @return a new array containing the specified range from the original array,
3470 * truncated or padded with nulls to obtain the required length
3471 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3472 * or {@code from > original.length}
3473 * @throws IllegalArgumentException if <tt>from > to</tt>
3474 * @throws NullPointerException if <tt>original</tt> is null
3475 * @throws ArrayStoreException if an element copied from
3476 * <tt>original</tt> is not of a runtime type that can be stored in
3477 * an array of class <tt>newType</tt>.
3478 * @since 1.6
3479 */
3480 @HotSpotIntrinsicCandidate
3481 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3482 int newLength = to - from;
3483 if (newLength < 0)
3484 throw new IllegalArgumentException(from + " > " + to);
3485 @SuppressWarnings("unchecked")
3486 T[] copy = ((Object)newType == (Object)Object[].class)
3487 ? (T[]) new Object[newLength]
3488 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3489 System.arraycopy(original, from, copy, 0,
3490 Math.min(original.length - from, newLength));
3491 return copy;
3492 }
3493
3494 /**
3495 * Copies the specified range of the specified array into a new array.
3496 * The initial index of the range (<tt>from</tt>) must lie between zero
3497 * and <tt>original.length</tt>, inclusive. The value at
3498 * <tt>original[from]</tt> is placed into the initial element of the copy
3499 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
3500 * Values from subsequent elements in the original array are placed into
|