Today see a Sparsearray class, check the relevant information to summarize.
Sparsearray refers to a sparse array (Sparse array), where the so-called sparse array is where most of the content values in the array are not used (or both 0), and only a small portion of the space is used in the array. Therefore, the memory space wasted, in order to save memory space, and do not affect the contents of the array of content values, we can use a compressed way to represent the contents of the sparse array.
from the construction method we can see, it and the general list, can be pre-set container size, the default size is ten, is a tool class provided by Android
Purely literally, sparsearray refers to a sparse array (Sparse array), where the so-called sparse array is the majority of the content values in the array are not used (or all 0), and only a small portion of the space is used in the array. Therefore, the memory space wasted, in order to save memory space, and do not affect the contents of the array of content values, we can use a compressed way to represent the contents of the sparse array.
Sparsearray used to replace Hashmap<integer, object>.
Sparsebooleanarray used to replace Hashmap<integer, boolean>.
Sparseintarray used to replace Hashmap<integer, integer>.
Suppose there is an array of 9*7, with the following contents:
In this array, there are 63 spaces, but only 5 elements are used, resulting in a waste of 58 element space. Let's use the sparse array to redefine the array:
Where the first part of the sparse array records the number of columns and rows of the original array and the number of elements used, the second part records the position and contents of the elements in the original array. After compression, the original need to declare an array of size 63, and after using compression, only need to declare an array of size 6*3, only 18 storage space.
How to use: add
public void put (int key, E value) {}public void append (int key, E value) {}
Delete:
public void Delete (int key) {}public void remove (int key) {}///directly called delete (int key) public void removeAt (int index) {}public void Clear () {}
Modify:
public void put (int key, E value) public void setvalueat (int index, E value)
Modifying the data initially thought that only setvalueat (int index, e value) could modify the data, but later found that the put (int key, E value) could also modify the data, and we looked at the source of the put (int key, E value), Before you put the data, you will find out whether the data you want to put already exists, and if so, add it if it exists.
Find:
Public E get (int key)
Public e Get (int. key, E valueifkeynotfound) public e valueat (int index)
Sparsearray<e> to replace HashMap for better performance