Sparsearray<e> is the compression of an array, which is represented by a compression method.
When we define an array, there are times in the array where most of the content is not used (or 0), resulting in a waste of memory controls. To save memory controls without affecting the data in the array, we used a compression method to represent the array.
The yellow in the figure shows the elements we used, and the other elements here are wasted. Take a look at the methods represented by sparse arrays
The first part of the sparse array represents the number of rows, columns, and elements used by the array. The second part records the position and content of the elements used in the original array.
Use of Sparsearray:
Increase
Public void put (int key, E value) {}publicvoid append (int key, E value) { }
By deleting
Public void Delete (int key) {}publicvoid Remove (int// Directly called delete (int key)publicvoid removeAt (int index) {}public void Clear () {}
Change
/** the processing in the source code is: if the put data already exists, is modified, otherwise it is added * */ Public void put (int key, E value)publicvoid setvalueat (int index, E Value
Check
PublicE Get (intkey) PublicE Get (intkey, E valueifkeynotfound)/**to view the keys in the first few places:*/ Public intKeyat (intindex)/**view values for the first few positions*/ PublicE Valueat (intindex)/**A value less than 0 is returned instead of 1 when the view key is located in a location that is not found. The negative value returned is the location where it was not found. */ Public intIndexofkey (intkey)/**see where the value is, no, return-1*/ Public intIndexofvalue (E value)
Sparsebooleanarray <---> Hashmap<integer, boolean>
Sparseintarray <---> Hashmap<integer, integer>
Sparsearray is specifically written for hashmap<interger,object> such as Android, with the aim of improving efficiency, with the core of binary lookup function (binarysearch)
New New Sparsearray<e> ();
Sparse array sparsearray<e> for Android performance optimization