Java Collection Tutorial - Java Arrays.copyOfRange(U[] original, int from, int to, Class <? extends T[]> newType)








Syntax

Arrays.copyOfRange(U[] original, int from, int to, Class <? extends T[]> newType) has the following syntax.

public static <T,U> T[] copyOfRange(U[] original,   
                                                int from,
                                                int to,
                                                Class <? extends T[]> newType)

Example

In the following code shows how to use Arrays.copyOfRange(U[] original, int from, int to, Class <? extends T[]> newType) method.

//from ww w  .jav  a  2s . c om
import java.util.Arrays;

public class Main {

   public static void main(String[] args) {

      // intializing an array arr1
      Short arr1[] = new Short[]{15, 10, 45};

      System.out.println(Arrays.toString(arr1));

      // copying array arr1 to arr2 as Number with range of index from 1 to 3
      Number[] arr2 = Arrays.copyOfRange(arr1, 1, 3, Number[].class);

      System.out.println(Arrays.toString(arr2));
   }
}

The code above generates the following result.