/** * Removes the mapping from the specified key, if there was any. */ public void delete(int key) { int i = binarySearch(mKeys, 0, mSize, key); if (i >= 0) { removeAt(i); } }
/** * Gets the int mapped from the specified key, or <code>0</code> * if no such mapping has been made. */ public int get(int key) { return get(key, 0); }
int drawableId; String drawableName; SparseIntArray playerImages = new SparseIntArray(totalNumberOfPlayers); for (int n = 1; n < totalNumberOfPlayers; n++) { drawableName = "image_" + n; drawableId = getResources().getIdentifier(drawableName, "drawable", getPackageName()); playerImages.put(n, drawableId); }
/** * Gets the int mapped from the specified key, or the specified value * if no such mapping has been made. */ public int get(int key, int valueIfKeyNotFound) { int i = binarySearch(mKeys, 0, mSize, key); if (i < 0) { return valueIfKeyNotFound; } else { return mValues[i]; } }
int drawableId; String drawableName; SparseIntArray brownieNormal = new SparseIntArray(totalNumberOfBrownies); SparseIntArray browniePressed = new SparseIntArray(totalNumberOfBrownies); // iterate through all the required brownie pairs for (int i = 0; i < totalNumberOfBrownies; i++) { // generate the required file name drawableName = "brownie" + i; // find the id of the required drawable using the generated file name drawableId = getResources().getIdentifier(drawableName, "drawable", getPackageName()); // put the resulting drawable id into the SparseIntArray brownieNormal.put(n, drawableId); // repeat for the pressed images drawableName = "pressedBrownie" + i; drawableId = getResources().getIdentifier(drawableName, "drawable", getPackageName()); browniePressed.put(n, drawableId); }
/** * Returns the index for which {@link #keyAt} would return the * specified key, or a negative number if the specified * key is not mapped. */ public int indexOfKey(int key) { return binarySearch(mKeys, 0, mSize, key); }
/** * Puts a key/value pair into the array, optimizing for the case where * the key is greater than all existing keys in the array. */ public void append(int key, int value) { if (mSize != 0 && key <= mKeys[mSize - 1]) { put(key, value); return; } int pos = mSize; if (pos >= mKeys.length) { int n = ArrayUtils.idealIntArraySize(pos + 1); int[] nkeys = new int[n]; int[] nvalues = new int[n]; // Log.e("SparseIntArray", "grow " + mKeys.length + " to " + n); System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); System.arraycopy(mValues, 0, nvalues, 0, mValues.length); mKeys = nkeys; mValues = nvalues; } mKeys[pos] = key; mValues[pos] = value; mSize = pos + 1; }
int i = binarySearch(mKeys, 0, mSize, key);