SparseIntArray

public class SparseIntArray
extends Object

java.lang.Object
   ↳ com.android.utils.SparseIntArray


SparseIntArrays 會將整數對應至整數。與一般整數陣列不同,索引中可能會出現間隙。與使用 HashMap 將 Integer 對應至 Integer 相比,這項功能更有效率。

摘要

公用建構函式

SparseIntArray()

Creates a new SparseIntArray containing no mappings.

SparseIntArray(int initialCapacity)

Creates a new SparseIntArray containing no mappings that will not require any additional memory allocation to store the specified number of mappings.

公用方法

void append(int key, int value)

將鍵/值組合放入陣列,並針對鍵大於陣列中所有現有鍵的情況進行最佳化。

void clear()

Removes all key-value mappings from this SparseIntArray.

void delete(int key)

移除指定鍵的對應關係 (如有)。

int get(int key)

取得指定鍵對應的 int,如果沒有這類對應,則取得 0

int get(int key, int valueIfKeyNotFound)

取得指定鍵對應的 int,如果沒有這類對應,則取得指定值。

int indexOfKey(int key)

傳回 keyAt(int) 會傳回指定鍵的索引,如果未對應指定鍵,則傳回負數。

int indexOfValue(int value)

傳回 valueAt(int) 會傳回指定鍵的索引,如果沒有任何鍵對應至指定值,則傳回負數。

int keyAt(int index)

指定 0...size()-1 範圍內的索引,傳回這個 SparseIntArray 儲存的第 index 個鍵/值對應項目的鍵。

void put(int key, int value)

新增從指定鍵到指定值的對應,並取代指定鍵的前一個對應 (如有)。

void removeAt(int index)

移除指定索引處的對應關係。

int size()

Returns the number of key-value mappings that this SparseIntArray currently stores.

int valueAt(int index)

指定 0...size()-1 範圍內的索引,傳回這個 SparseIntArray 儲存的第 index 個鍵/值對應。

公用建構函式

SparseIntArray

public SparseIntArray ()

建立不含對應的新 SparseIntArray。

SparseIntArray

public SparseIntArray (int initialCapacity)

建立新的 SparseIntArray,其中不含任何對應,且不需要額外分配記憶體來儲存指定數量的對應。

參數
initialCapacity int

公用方法

附加

public void append (int key, 
                int value)

將鍵/值組合放入陣列,並針對鍵大於陣列中所有現有鍵的情況進行最佳化。

參數
key int

value int

清除

public void clear ()

從這個 SparseIntArray 移除所有鍵/值對應。

刪除

public void delete (int key)

移除指定鍵的對應關係 (如有)。

參數
key int

get

public int get (int key)

取得從指定鍵對應的 int,如果沒有這類對應,則取得 0

參數
key int

傳回
int

get

public int get (int key, 
                int valueIfKeyNotFound)

取得指定鍵對應的 int,如果沒有這類對應,則取得指定值。

參數
key int

valueIfKeyNotFound int

傳回
int

indexOfKey

public int indexOfKey (int key)

傳回 keyAt(int) 會傳回指定鍵的索引,如果指定鍵未對應,則傳回負數。

參數
key int

傳回
int

indexOfValue

public int indexOfValue (int value)

傳回 valueAt(int) 會傳回指定鍵的索引,如果沒有任何鍵對應至指定值,則傳回負數。請注意,這項作業是線性搜尋,與依鍵查詢不同,而且多個鍵可以對應到同一個值,但這項作業只會找出其中一個。

參數
value int

傳回
int

keyAt

public int keyAt (int index)

指定 0...size()-1 範圍內的索引,傳回這個 SparseIntArray 儲存的第 index 個鍵/值對應項中的鍵。

參數
index int

傳回
int

聯繫

public void put (int key, 
                int value)

新增從指定鍵到指定值的對應,並取代指定鍵的前一個對應 (如有)。

參數
key int

value int

removeAt

public void removeAt (int index)

移除指定索引處的對應。

參數
index int

size

public int size ()

傳回這個 SparseIntArray 目前儲存的鍵/值對應數量。

傳回
int

valueAt

public int valueAt (int index)

指定 0...size()-1 範圍內的索引,傳回這個 SparseIntArray 儲存的第 index 個鍵/值對應。

參數
index int

傳回
int