Example usage for android.util SparseArray keyAt

List of usage examples for android.util SparseArray keyAt

Introduction

In this page you can find the example usage for android.util SparseArray keyAt.

Prototype

public int keyAt(int index) 

Source Link

Document

Given an index in the range 0...size()-1, returns the key from the indexth key-value mapping that this SparseArray stores.

Usage

From source file:Main.java

private static void print(SparseArray<String> strs) {
    for (int i = 0; i < strs.size(); i++) {
        Log.i("XJ", "key = " + strs.keyAt(i) + ",value = " + strs.valueAt(i));
    }/*from  ww w.  j  av a2  s .  c om*/
}

From source file:Main.java

public static void scaleBitmaps(SparseArray<Bitmap> bitmapMap, float scale) {
    for (int i = 0; i < bitmapMap.size(); i++) {
        int key = bitmapMap.keyAt(i);
        Log.d("BitmapUtil", "scaleBitmaps: " + key);
        Bitmap bitmap = bitmapMap.get(key);
        bitmapMap.put(i, scaleBitmap(bitmap, scale));
    }/*  w w w.  j a  va  2  s  .c  o  m*/
}

From source file:Main.java

/**
 * Converts the sparse array into {@link java.util.List}.
 *
 * @param array sparse array of some type
 * @param <T> type of object in sparse array
 * @return list of object of specific type
 *///www  .jav  a2 s  .  co  m
public static <T> List<T> sparseArrayAsList(final SparseArray<T> array) {
    final List<T> values = new ArrayList<>();

    for (int i = 0; i < array.size(); i++) {
        final int key = array.keyAt(i);

        values.add(array.get(key));
    }

    return values;
}

From source file:Main.java

public static <E> SparseArray<E> putAll(SparseArray<E> source, SparseArray<E> dest) {
    if (dest == null)
        dest = source;//from w  w  w. j  a  v  a 2s  .  c o m
    else if (source != null)
        for (int i = 0; i < source.size(); i++) {
            int key = source.keyAt(i);
            E value = source.valueAt(i);
            dest.put(key, value);
        }
    return dest;
}

From source file:Main.java

public static String mergeUri(String uri, Object[] args, SparseArray<String> paramKeys,
        Map<String, String> methodStaticUri, Map<String, String> globalParamKeys) {

    if (paramKeys != null && paramKeys.size() > 0 && args != null && args.length > 0) {
        for (int i = 0, key = paramKeys.keyAt(i); i < paramKeys.size(); key = paramKeys.keyAt(++i)) {
            uri = uri.replaceFirst(":" + paramKeys.get(key), String.valueOf(args[key]));
        }//from  www. j a v a  2  s .co  m
    }
    if (methodStaticUri != null && methodStaticUri.size() > 0) {
        for (Map.Entry<String, String> e : methodStaticUri.entrySet()) {
            uri = uri.replaceFirst(":" + e.getKey(), e.getValue());
        }
    }
    if (globalParamKeys != null && globalParamKeys.size() > 0) {
        for (Map.Entry<String, String> e : globalParamKeys.entrySet()) {
            uri = uri.replaceFirst(":" + e.getKey(), e.getValue());
        }
    }

    return uri;
}

From source file:net.sf.sprockets.util.SparseArrays.java

/**
 * Get the keys of the SparseArray./* w  w  w .  j a va2s. co m*/
 */
public static int[] keys(SparseArray<?> array) {
    int[] keys = new int[array.size()];
    for (int i = 0; i < keys.length; i++) {
        keys[i] = array.keyAt(i);
    }
    return keys;
}

From source file:Main.java

/**
 * Returns a string composed from a {@link SparseArray}.
 *///from www . j av  a  2s.co m
public static String toString(SparseArray<byte[]> array) {
    if (array == null) {
        return "null";
    }
    if (array.size() == 0) {
        return "{}";
    }
    StringBuilder buffer = new StringBuilder();
    buffer.append('{');
    for (int i = 0; i < array.size(); ++i) {
        buffer.append(array.keyAt(i)).append("=").append(Arrays.toString(array.valueAt(i)));
    }
    buffer.append('}');
    return buffer.toString();
}

From source file:Main.java

/**
 * Returns a string composed from a {@link SparseArray}.
 *///from  www.ja  v  a 2 s. c  o  m
static String toString(SparseArray<byte[]> array) {
    if (array == null) {
        return "null";
    }
    if (array.size() == 0) {
        return "{}";
    }
    StringBuilder buffer = new StringBuilder();
    buffer.append('{');
    for (int i = 0; i < array.size(); ++i) {
        buffer.append(array.keyAt(i)).append("=").append(Arrays.toString(array.valueAt(i)));
    }
    buffer.append('}');
    return buffer.toString();
}

From source file:Main.java

/**
 * Compare two dumps and get a list of all indices where
 * they differ from each other./*from www  . ja va  2  s  .  co  m*/
 * @param dump1 The first dump. The sector number is key and the
 * string array represents the blocks.
 * @param dump2 The second dump. The sector number is key and the
 * string array represents the blocks.
 * @return Indices where the two dumps differ. The key represents
 * the sector number. The first dimension of the value represents the
 * block number and the second is a list of indices where dump2 is
 * different from dump1. If the value is Integer[0][0] then the sector
 * exists only in dump1. If the value is Integer[1][0] then the sector
 * exists only in dump2.
 */
public static SparseArray<Integer[][]> diffIndices(SparseArray<String[]> dump1, SparseArray<String[]> dump2) {
    SparseArray<Integer[][]> ret = new SparseArray<Integer[][]>();
    // Walk through all sectors of dump1.
    for (int i = 0; i < dump1.size(); i++) {
        String[] sector1 = dump1.valueAt(i);
        int sectorNr = dump1.keyAt(i);
        String[] sector2 = dump2.get(sectorNr);

        // Check if dump2 has the current sector of dump1.
        if (sector2 == null) {
            ret.put(sectorNr, new Integer[0][0]);
            continue;
        }

        // Check the blocks.
        Integer[][] diffSector = new Integer[sector1.length][];
        // Walk through all blocks.
        for (int j = 0; j < sector1.length; j++) {
            ArrayList<Integer> diffIndices = new ArrayList<Integer>();
            // Walk through all symbols.
            for (int k = 0; k < sector1[j].length(); k++) {
                if (sector1[j].charAt(k) != sector2[j].charAt(k)) {
                    // Found different symbol at index k.
                    diffIndices.add(k);
                }
            }
            if (diffIndices.size() == 0) {
                // Block was identical.
                diffSector[j] = new Integer[0];
            } else {
                diffSector[j] = diffIndices.toArray(new Integer[diffIndices.size()]);
            }
        }
        ret.put(sectorNr, diffSector);
    }

    // Are there sectors that occur only in dump2?
    for (int i = 0; i < dump2.size(); i++) {
        int sectorNr = dump2.keyAt(i);
        if (dump1.get(sectorNr) == null) {
            // Sector only exists in dump2.
            ret.put(sectorNr, new Integer[1][0]);
        }
    }

    return ret;
}

From source file:Main.java

/**
 * Check whether two {@link SparseArray} equal.
 *///from  w w  w  . j a  v  a  2 s .  com
static boolean equals(SparseArray<byte[]> array, SparseArray<byte[]> otherArray) {
    if (array == otherArray) {
        return true;
    }
    if (array == null || otherArray == null) {
        return false;
    }
    if (array.size() != otherArray.size()) {
        return false;
    }

    // Keys are guaranteed in ascending order when indices are in ascending order.
    for (int i = 0; i < array.size(); ++i) {
        if (array.keyAt(i) != otherArray.keyAt(i) || !Arrays.equals(array.valueAt(i), otherArray.valueAt(i))) {
            return false;
        }
    }
    return true;
}