Sparsearray<e> detailed Android Programming

Source: Internet
Author: User

When I recently programmed, I found a hint for Hashmap<integer, e>:


The translation comes to be: Replace with sparsearray<e> to have better performance.
Let's take a look at the source code in the Sparsearray exactly what happened:

First, the construction
From the construction method we can see, it and the general list, can be pre-set container size, the default size is 10:

[Java]View Plaincopy
    1. Public Sparsearray () {
    2. This (10);
    3. }
    4. Public Sparsearray (int initialcapacity) {
    5. ......
    6. }



Second, increase
It has two ways to add a key-value pair:

[Java]View Plaincopy
    1. Public void put (int key, E value)
    2. Public void Append (int key, E value)




In the storage of data, is the use of two-point method, the following is its binary method of the source code:

[Java]View Plaincopy
  1. Private static int BinarySearch (int[] A, int start, int len, int key) {
  2. int high = start + len;
  3. int low = start- 1;
  4. While (High-Low > 1) {
  5. int guess = (high + low)/ 2;
  6. if (A[guess] < key) {
  7. low = guess;
  8. continue;
  9. }
  10. High = guess;
  11. }
  12. if (high = = start + len)
  13. return start + len ^ 0xFFFFFFFF;
  14. if (a[high] = = key) {
  15. return high;
  16. }
  17. return high ^ 0xFFFFFFFF;
  18. }




Therefore, it stores values that are key values from small to large in order.


Third, check
It has two methods to take values:

[Java]View Plaincopy
    1. Public E get (int key)
    2. Public e get (int key, E valueifkeynotfound)

The last variable name from the parameter can be seen, the value returned when the pass-through is not found.


To view the keys in the first few places:

[Java]View Plaincopy
    1. public int Keyat (int index)

To see the values for the first few locations:

[Java]View Plaincopy
    1. Public E valueat (int index)

Look at the location of the key, because the binary method to find the position of the key, so there is no return to the value of less than 0, rather than return-1, this point to note that the returned negative number is actually indicating where it is not found, if you save 5, find the key is greater than 5 values, return is-6:

[Java]View Plaincopy
    1. public int Indexofkey (int key)

To see where the value is, no, return-1:

[Java]View Plaincopy
    1. public int Indexofvalue (E value)



Iv. deletion of
It has four methods:

[Java]View Plaincopy
    1. Public void Delete (int key)
    2. Public void Remove (int key)


But in fact, the effect of delete and remove is the same, the Remove method called the Delete method, remove Source:

[Java]View Plaincopy
    1. Public void Remove (int key) {
    2. Delete (key);
    3. }

[Java]View Plaincopy
    1. Public void removeAt (int index)
    2. Public void Clear ()


The last one is to clear all


V. Change

[Java]View Plaincopy
    1. Public void setValueAt (int index, E value)
    2. Public void put (int key, E value)


The Put method can also modify a key-value pair, noting that if the key does not exist, it becomes the addition of a new key-value pair


Vi.. Other:
Sparsearray implements the Cloneable interface and can also call the Clone method.


Summary: Since the Android system suggests that we use sparsearray<e> instead of Hashmap<integer, E>, let's do what it says. Some of the methods inside, I have also been analyzed here, I hope to be helpful to you.

Sparsearray<e> detailed Android Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.