SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch)
private static int binarySearch(int[] a, int start, int len, int key) { int high = start + len, low = start - 1, guess; while (high - low > 1) { guess = (high + low) / 2; if (a[guess] < key) low = guess; else high = guess; } if (high == start + len) return ~(start + len); else if (a[high] == key) return high; else return ~high; }
所以,它存储的数值都是按键值从小到大的顺序排列好的。
包含的方法,
添加数据:
public void put(int key, E value) {} public void append(int key, E value){}
public void delete(int key) {} public void remove(int key) {} public void removeAt(int index){} public void clear(){}
public void put(int key, E value) public void setValueAt(int index, E value)
public E get(int key) public E get(int key, E valueIfKeyNotFound)
SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的类,目的是提高效率,其核心是折半查找函数(binarySearch)。在Android中,当我们需要定义
HashMap<Integer, E> hashMap = new HashMap<Integer, E>();
SparseArray<E> sparseArray = new SparseArray<E>();