com.intel.gpe.util.arrays
Class ArrayUtil

java.lang.Object
  extended by com.intel.gpe.util.arrays.ArrayUtil

public class ArrayUtil
extends java.lang.Object

Constais some useful library functions related to arrays

Version:
$Id: ArrayUtil.java,v 1.2 2006/05/26 10:30:00 daivanov Exp $
Author:
Dmitry Ivanov

Constructor Summary
ArrayUtil()
           
 
Method Summary
static java.lang.Object castElement2Class(java.lang.Class elClazz, java.lang.Object[] array)
          Cast every element of array to class elClazz
static byte[][] divide(byte[] arr, int chunkSize)
          Divides one big array to a number of smaller arrays, each one with length of chunkSize.
static boolean equals(byte[][] a1, byte[][] a2)
          Compares two matrices for equality.
static byte[] merge(byte[]... arrs)
          Merges a number of arrays into one big array consequently.
static int[] searchIndices(java.lang.Object[] arr, java.lang.Object[] toFind)
          if exists t that arr[t] equals toFind[i] then result[i]=t, otherwise result[i]=-1
t is the smallest one if two such indexes exists.
static byte[] subarray(byte[] arr, int beginIdx, int endIdx)
          The same as substring.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtil

public ArrayUtil()
Method Detail

searchIndices

public static int[] searchIndices(java.lang.Object[] arr,
                                  java.lang.Object[] toFind)
if exists t that arr[t] equals toFind[i] then result[i]=t, otherwise result[i]=-1
t is the smallest one if two such indexes exists. null is equals only with null Method Object.equals(Object) is used as a comparator. An asymptotic complexity is arr.length*toFind.length

Parameters:
arr - - array where search is happening
toFind - - elements which indexes must be found
Returns:
int[tofind.lenght] : res[i] = search(arr, toFind[i]) if any of input array are null then null'll be returned

castElement2Class

public static java.lang.Object castElement2Class(java.lang.Class elClazz,
                                                 java.lang.Object[] array)
Cast every element of array to class elClazz

Parameters:
elClazz - class of elements in the output array
array - input array
Returns:
array with the same elements but it's class is elClazz[] if input array is null then null will be returned

subarray

public static byte[] subarray(byte[] arr,
                              int beginIdx,
                              int endIdx)
                       throws java.lang.IllegalArgumentException
The same as substring. begin - inclusive, end - exclusive

Parameters:
arr - input array
beginIdx - if less then zero then assign to zero
endIdx - if more then arr.length() then assign to length
Returns:
subarray of given array with indexes of range [beginIdx, endIdx) null if input array is null
Throws:
java.lang.IllegalArgumentException - if beginIdx > endIdx

merge

public static byte[] merge(byte[]... arrs)
Merges a number of arrays into one big array consequently. E.g. res = {arrs[0][0],arrs[0][1], ..., arrs[0][arrs[0].length-1], arrs[1][0],arrs[1][1], ..., arrs[1][arrs[1].length-1],... .... ...., arrs[arrs.length-1][0],arrs[arrs.length-1][1], ..., arrs[arrs.length-1][arrs[arrs.length-1].length-1] } Null arrays are threated as empty (0 elements in)

Parameters:
arrs - array of array
Returns:
merge of arrays with length = sum of lengths of arrays null if arrs is null

divide

public static byte[][] divide(byte[] arr,
                              int chunkSize)
                       throws java.lang.IllegalArgumentException
Divides one big array to a number of smaller arrays, each one with length of chunkSize. The length of the last array may be smaller then chunkSize. If you apply merge(byte[][]) to the result of divide(byte[], int) then you obtain the source array. So this method is inverse of merge(byte[][])

Parameters:
arr - big input array
chunkSize - the size of each resulting array (except, maybe, the last one)
Returns:
division of this array into small arrays zero length array if the input array's length is zero null if input array is null
Throws:
java.lang.IllegalArgumentException - if chunkSize isn't positive

equals

public static boolean equals(byte[][] a1,
                             byte[][] a2)
Compares two matrices for equality. Every element of array a1 must be equals to corresponding component in the array a2 as in method Arrays.equals(byte[], byte[]). Also true is returned when both source arrays are null

Parameters:
a1 -
a2 -
Returns:
true if matrices are equals, false otherwise


Copyright © 2008. All Rights Reserved.