FileDocCategorySizeDatePackage
ArrayUtils.javaAPI DocAndroid 1.5 API4279Wed May 06 22:41:56 BST 2009com.android.internal.util

ArrayUtils

public class ArrayUtils extends Object
ArrayUtils contains some methods that you can call to find out the most efficient increments by which to grow arrays.

Fields Summary
private static Object[]
EMPTY
private static final int
CACHE_SIZE
private static Object[]
sCache
Constructors Summary
private ArrayUtils()


       /* cannot be instantiated */ 
Methods Summary
public static booleancontains(T[] array, T value)
Checks that value is present as at least one of the elements of the array.

param
array the array to check in
param
value the value to check for
return
true if the value is present in the array

        for (T element : array) {
            if (element == null) {
                if (value == null) return true;
            } else {
                if (value != null && element.equals(value)) return true;
            }
        }
        return false;
    
public static T[]emptyArray(java.lang.Class kind)
Returns an empty array of the specified type. The intent is that it will return the same empty array every time to avoid reallocation, although this is not guaranteed.

        if (kind == Object.class) {
            return (T[]) EMPTY;
        }

        int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
        Object cache = sCache[bucket];

        if (cache == null || cache.getClass().getComponentType() != kind) {
            cache = Array.newInstance(kind, 0);
            sCache[bucket] = cache;

            // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
        }

        return (T[]) cache;
    
public static booleanequals(byte[] array1, byte[] array2, int length)
Checks if the beginnings of two byte arrays are equal.

param
array1 the first byte array
param
array2 the second byte array
param
length the number of bytes to check
return
true if they're equal, false otherwise

        if (array1 == array2) {
            return true;
        }
        if (array1 == null || array2 == null || array1.length < length || array2.length < length) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    
public static intidealBooleanArraySize(int need)

        return idealByteArraySize(need);
    
public static intidealByteArraySize(int need)

        for (int i = 4; i < 32; i++)
            if (need <= (1 << i) - 12)
                return (1 << i) - 12;

        return need;
    
public static intidealCharArraySize(int need)

        return idealByteArraySize(need * 2) / 2;
    
public static intidealFloatArraySize(int need)

        return idealByteArraySize(need * 4) / 4;
    
public static intidealIntArraySize(int need)

        return idealByteArraySize(need * 4) / 4;
    
public static intidealLongArraySize(int need)

        return idealByteArraySize(need * 8) / 8;
    
public static intidealObjectArraySize(int need)

        return idealByteArraySize(need * 4) / 4;
    
public static intidealShortArraySize(int need)

        return idealByteArraySize(need * 2) / 2;