Java array get sub array

Description

Java array get sub array

import java.util.Arrays;

public class Main {
  public static void main(String[] argv) {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    byte[] c = get(b, 3);
    byte[] d = get(b, 3, 3);
    /*from  www. j a  v  a 2s.c  o m*/
    System.out.println(Arrays.toString(b));
    System.out.println(Arrays.toString(c));
    System.out.println(Arrays.toString(d));
  }

  /**
   * Gets the subarray from <tt>array</tt> that starts at <tt>offset</tt>.
   */
  public static byte[] get(byte[] array, int offset) {
    return get(array, offset, array.length - offset);
  }

  /**
   * Gets the subarray of length <tt>length</tt> from <tt>array</tt> that starts
   * at <tt>offset</tt>.
   */
  public static byte[] get(byte[] array, int offset, int length) {
    byte[] result = new byte[length];
    System.arraycopy(array, offset, result, 0, length);
    return result;
  }
}



PreviousNext

Related