Java Utililty Methods Array Sub Array

List of utility methods to do Array Sub Array

Description

The list of methods to do Array Sub Array are organized into topic(s).

Method

String[]sub(String[] a, String[] b)
subtract b from a
TreeSet<String> s = new TreeSet<>(Arrays.asList(a));
s.removeAll(Arrays.asList(b));
if (s.size() != a.length) {
    return s.toArray(new String[s.size()]);
} else {
    return a;
Listsub(T[] source, int first, int last)
sub
if (first < 0 || last < 0 || first >= source.length || last >= source.length)
    throw new ArrayIndexOutOfBoundsException();
List<T> result = new ArrayList<T>();
for (int i = first; i <= last; i++)
    result.add(source[i]);
return result;
boolean[]subarray(boolean[] array, int fromIndex, int length)
Building on the arraycopy function in System, this function returns a "subarray" of a larger array.
boolean[] tempArray = new boolean[length];
try {
    System.arraycopy(array, fromIndex, tempArray, 0, tempArray.length);
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
    System.out.println("Array of length:" + array.length);
    System.out.println("From index with length:" + fromIndex + "," + length);
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
...
byte[]subArray(byte[] a, int beginIndex, int endIndex)
Returns the new array that is the sub-array of the specified array .
if (a == null || a.length == 0 || beginIndex < 0 || endIndex > a.length || beginIndex > endIndex) {
    return new byte[0];
} else {
    byte[] b = new byte[endIndex - beginIndex];
    System.arraycopy(a, beginIndex, b, 0, b.length);
    return b;
byte[]subArray(byte[] array, int beginIndex, int endIndex)
Extract a sub array of bytes out of the byte array.
int length = endIndex - beginIndex;
byte[] subarray = new byte[length];
System.arraycopy(array, beginIndex, subarray, 0, length);
return subarray;
byte[]subarray(byte[] array, int offset, int length)
NOTE: subarray method implemented for arrays of type byte Extracts a sub array from an array of a primitive type.
validateOffsetLength(array, array.length, offset, length);
byte[] sub = new byte[length];
for (int i = 0; i < length; i++)
    sub[i] = array[offset + i];
return sub;
byte[]subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)

Produces a new byte array containing the elements between the start and end indices.

The start index is inclusive, the end index exclusive.

if (array == null) {
    return null;
if (startIndexInclusive < 0) {
    startIndexInclusive = 0;
if (endIndexExclusive > array.length) {
    endIndexExclusive = array.length;
...
byte[]subarray(byte[] array, int startIndexInclusive, int endIndexExclusive)
subarray
if (array == null) {
    return null;
if (startIndexInclusive < 0) {
    startIndexInclusive = 0;
if (endIndexExclusive > array.length) {
    endIndexExclusive = array.length;
...
byte[]subArray(byte[] b, int offset, int length)
Return a subarray of the byte array in parameter.
byte[] sub = new byte[length];
for (int i = offset; i < offset + length; i++)
    sub[i - offset] = b[i];
return sub;
byte[]subarray(byte[] b, int ofs, int len)
subarray
byte[] out = new byte[len];
System.arraycopy(b, ofs, out, 0, len);
return out;