Java Collection Tutorial - Java Arrays.copyOfRange(int[] original, int from, int to)








Syntax

Arrays.copyOfRange(int[] original, int from, int to) has the following syntax.

public static int[] copyOfRange(int[] original,  int from,  int to)

Example

In the following code shows how to use Arrays.copyOfRange(int[] original, int from, int to) method.

/*from   w w  w. j a  v a2  s  .com*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    int[] arr1 = new int[] {5, 60, 75};

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

    // copying array arr1 to arr2 with range from index 2 to 6
    int[] arr2 = Arrays.copyOfRange(arr1, 2, 6);

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

The code above generates the following result.