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








Syntax

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

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

Example

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

/* w  ww  . j a  v a2s .c  o  m*/
import java.util.Arrays;

public class Main {

  public static void main(String[] args) {
    
    // intializing an array arr1
    byte[] arr1 = new byte[] {5, 10, 25};

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

    // copying array arr1 to arr2 with range from index 1 to 7
    byte[] arr2 = Arrays.copyOfRange(arr1, 1, 7);

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

The code above generates the following result.