Java examples for Collection Framework:Array Sub Array
Truncates the given array to the request length.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; int newLength = 2; System.out.println(java.util.Arrays.toString(truncate(array, newLength)));// www .j a va2 s . c om } /** * Truncates the given array to the request length. * * @param array * the array to be truncated. * @param newLength * the new length in bytes. * @return the truncated array. */ public static byte[] truncate(byte[] array, int newLength) { if (array.length < newLength) { return array; } else { byte[] truncated = new byte[newLength]; System.arraycopy(array, 0, truncated, 0, newLength); return truncated; } } }