< prev index next >

src/java.base/share/classes/java/util/Arrays.java

Print this page

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import jdk.internal.util.ArraysSupport;

  29 import jdk.internal.vm.annotation.IntrinsicCandidate;
  30 
  31 import java.io.Serializable;
  32 import java.lang.reflect.Array;
  33 import java.util.concurrent.ForkJoinPool;
  34 import java.util.function.BinaryOperator;
  35 import java.util.function.Consumer;
  36 import java.util.function.DoubleBinaryOperator;
  37 import java.util.function.IntBinaryOperator;
  38 import java.util.function.IntFunction;
  39 import java.util.function.IntToDoubleFunction;
  40 import java.util.function.IntToLongFunction;
  41 import java.util.function.IntUnaryOperator;
  42 import java.util.function.LongBinaryOperator;
  43 import java.util.function.UnaryOperator;
  44 import java.util.stream.DoubleStream;
  45 import java.util.stream.IntStream;
  46 import java.util.stream.LongStream;
  47 import java.util.stream.Stream;
  48 import java.util.stream.StreamSupport;

3490      * Such indices will exist if and only if the specified length
3491      * is greater than that of the original array.
3492      * The resulting array is of the class {@code newType}.
3493      *
3494      * @param <U> the class of the objects in the original array
3495      * @param <T> the class of the objects in the returned array
3496      * @param original the array to be copied
3497      * @param newLength the length of the copy to be returned
3498      * @param newType the class of the copy to be returned
3499      * @return a copy of the original array, truncated or padded with nulls
3500      *     to obtain the specified length
3501      * @throws NegativeArraySizeException if {@code newLength} is negative
3502      * @throws NullPointerException if {@code original} is null
3503      * @throws ArrayStoreException if an element copied from
3504      *     {@code original} is not of a runtime type that can be stored in
3505      *     an array of class {@code newType}
3506      * @since 1.6
3507      */
3508     @IntrinsicCandidate
3509     public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {

3510         @SuppressWarnings("unchecked")
3511         T[] copy = ((Object)newType == (Object)Object[].class)
3512             ? (T[]) new Object[newLength]
3513             : (T[]) Array.newInstance(newType.getComponentType(), newLength);


3514         System.arraycopy(original, 0, copy, 0,
3515                          Math.min(original.length, newLength));
3516         return copy;
3517     }
3518 
3519     /**
3520      * Copies the specified array, truncating or padding with zeros (if necessary)
3521      * so the copy has the specified length.  For all indices that are
3522      * valid in both the original array and the copy, the two arrays will
3523      * contain identical values.  For any indices that are valid in the
3524      * copy but not the original, the copy will contain {@code (byte)0}.
3525      * Such indices will exist if and only if the specified length
3526      * is greater than that of the original array.
3527      *
3528      * @param original the array to be copied
3529      * @param newLength the length of the copy to be returned
3530      * @return a copy of the original array, truncated or padded with zeros
3531      *     to obtain the specified length
3532      * @throws NegativeArraySizeException if {@code newLength} is negative
3533      * @throws NullPointerException if {@code original} is null

3789      * @param to the final index of the range to be copied, exclusive.
3790      *     (This index may lie outside the array.)
3791      * @param newType the class of the copy to be returned
3792      * @return a new array containing the specified range from the original array,
3793      *     truncated or padded with nulls to obtain the required length
3794      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3795      *     or {@code from > original.length}
3796      * @throws IllegalArgumentException if {@code from > to}
3797      * @throws NullPointerException if {@code original} is null
3798      * @throws ArrayStoreException if an element copied from
3799      *     {@code original} is not of a runtime type that can be stored in
3800      *     an array of class {@code newType}.
3801      * @since 1.6
3802      */
3803     @IntrinsicCandidate
3804     public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3805         int newLength = to - from;
3806         if (newLength < 0) {
3807             throw new IllegalArgumentException(from + " > " + to);
3808         }

3809         @SuppressWarnings("unchecked")
3810         T[] copy = ((Object)newType == (Object)Object[].class)
3811             ? (T[]) new Object[newLength]
3812             : (T[]) Array.newInstance(newType.getComponentType(), newLength);


3813         System.arraycopy(original, from, copy, 0,
3814                          Math.min(original.length - from, newLength));
3815         return copy;
3816     }
3817 
3818     /**
3819      * Copies the specified range of the specified array into a new array.
3820      * The initial index of the range ({@code from}) must lie between zero
3821      * and {@code original.length}, inclusive.  The value at
3822      * {@code original[from]} is placed into the initial element of the copy
3823      * (unless {@code from == original.length} or {@code from == to}).
3824      * Values from subsequent elements in the original array are placed into
3825      * subsequent elements in the copy.  The final index of the range
3826      * ({@code to}), which must be greater than or equal to {@code from},
3827      * may be greater than {@code original.length}, in which case
3828      * {@code (byte)0} is placed in all elements of the copy whose index is
3829      * greater than or equal to {@code original.length - from}.  The length
3830      * of the returned array will be {@code to - from}.
3831      *
3832      * @param original the array from which a range is to be copied

   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import jdk.internal.util.ArraysSupport;
  29 import jdk.internal.value.ValueClass;
  30 import jdk.internal.vm.annotation.IntrinsicCandidate;
  31 
  32 import java.io.Serializable;
  33 import java.lang.reflect.Array;
  34 import java.util.concurrent.ForkJoinPool;
  35 import java.util.function.BinaryOperator;
  36 import java.util.function.Consumer;
  37 import java.util.function.DoubleBinaryOperator;
  38 import java.util.function.IntBinaryOperator;
  39 import java.util.function.IntFunction;
  40 import java.util.function.IntToDoubleFunction;
  41 import java.util.function.IntToLongFunction;
  42 import java.util.function.IntUnaryOperator;
  43 import java.util.function.LongBinaryOperator;
  44 import java.util.function.UnaryOperator;
  45 import java.util.stream.DoubleStream;
  46 import java.util.stream.IntStream;
  47 import java.util.stream.LongStream;
  48 import java.util.stream.Stream;
  49 import java.util.stream.StreamSupport;

3491      * Such indices will exist if and only if the specified length
3492      * is greater than that of the original array.
3493      * The resulting array is of the class {@code newType}.
3494      *
3495      * @param <U> the class of the objects in the original array
3496      * @param <T> the class of the objects in the returned array
3497      * @param original the array to be copied
3498      * @param newLength the length of the copy to be returned
3499      * @param newType the class of the copy to be returned
3500      * @return a copy of the original array, truncated or padded with nulls
3501      *     to obtain the specified length
3502      * @throws NegativeArraySizeException if {@code newLength} is negative
3503      * @throws NullPointerException if {@code original} is null
3504      * @throws ArrayStoreException if an element copied from
3505      *     {@code original} is not of a runtime type that can be stored in
3506      *     an array of class {@code newType}
3507      * @since 1.6
3508      */
3509     @IntrinsicCandidate
3510     public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3511         Class<?> componentType = newType.getComponentType();
3512         @SuppressWarnings("unchecked")
3513         T[] copy = ((Object)newType == (Object)Object[].class)
3514             ? (T[]) new Object[newLength]
3515             : (original.getClass() == newType && componentType.isValue() && ValueClass.isNullRestrictedArray(original)
3516                     ? (T[]) ValueClass.newNullRestrictedArray(newType.getComponentType(), newLength)
3517                     : (T[]) Array.newInstance(componentType, newLength));
3518         System.arraycopy(original, 0, copy, 0,
3519                          Math.min(original.length, newLength));
3520         return copy;
3521     }
3522 
3523     /**
3524      * Copies the specified array, truncating or padding with zeros (if necessary)
3525      * so the copy has the specified length.  For all indices that are
3526      * valid in both the original array and the copy, the two arrays will
3527      * contain identical values.  For any indices that are valid in the
3528      * copy but not the original, the copy will contain {@code (byte)0}.
3529      * Such indices will exist if and only if the specified length
3530      * is greater than that of the original array.
3531      *
3532      * @param original the array to be copied
3533      * @param newLength the length of the copy to be returned
3534      * @return a copy of the original array, truncated or padded with zeros
3535      *     to obtain the specified length
3536      * @throws NegativeArraySizeException if {@code newLength} is negative
3537      * @throws NullPointerException if {@code original} is null

3793      * @param to the final index of the range to be copied, exclusive.
3794      *     (This index may lie outside the array.)
3795      * @param newType the class of the copy to be returned
3796      * @return a new array containing the specified range from the original array,
3797      *     truncated or padded with nulls to obtain the required length
3798      * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3799      *     or {@code from > original.length}
3800      * @throws IllegalArgumentException if {@code from > to}
3801      * @throws NullPointerException if {@code original} is null
3802      * @throws ArrayStoreException if an element copied from
3803      *     {@code original} is not of a runtime type that can be stored in
3804      *     an array of class {@code newType}.
3805      * @since 1.6
3806      */
3807     @IntrinsicCandidate
3808     public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3809         int newLength = to - from;
3810         if (newLength < 0) {
3811             throw new IllegalArgumentException(from + " > " + to);
3812         }
3813         Class<?> componentType = newType.getComponentType();
3814         @SuppressWarnings("unchecked")
3815         T[] copy = ((Object)newType == (Object)Object[].class)
3816             ? (T[]) new Object[newLength]
3817             : (original.getClass() == newType && componentType.isValue() && ValueClass.isNullRestrictedArray(original)
3818                     ? (T[]) ValueClass.newNullRestrictedArray(componentType, newLength)
3819                     : (T[]) Array.newInstance(componentType, newLength));
3820         System.arraycopy(original, from, copy, 0,
3821                          Math.min(original.length - from, newLength));
3822         return copy;
3823     }
3824 
3825     /**
3826      * Copies the specified range of the specified array into a new array.
3827      * The initial index of the range ({@code from}) must lie between zero
3828      * and {@code original.length}, inclusive.  The value at
3829      * {@code original[from]} is placed into the initial element of the copy
3830      * (unless {@code from == original.length} or {@code from == to}).
3831      * Values from subsequent elements in the original array are placed into
3832      * subsequent elements in the copy.  The final index of the range
3833      * ({@code to}), which must be greater than or equal to {@code from},
3834      * may be greater than {@code original.length}, in which case
3835      * {@code (byte)0} is placed in all elements of the copy whose index is
3836      * greater than or equal to {@code original.length - from}.  The length
3837      * of the returned array will be {@code to - from}.
3838      *
3839      * @param original the array from which a range is to be copied
< prev index next >