SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch)

  1. private static int binarySearch(int[] a, int start, int len, int key) {
  2. int high = start + len, low = start - 1, guess;
  3. while (high - low > 1) {
  4. guess = (high + low) / 2;
  5. if (a[guess] < key)
  6. low = guess;
  7. else
  8. high = guess;
  9. }
  10. if (high == start + len)
  11. return ~(start + len);
  12. else if (a[high] == key)
  13. return high;
  14. else
  15. return ~high;
  16. }

所以,它存储的数值都是按键值从小到大的顺序排列好的。

包含的方法,

添加数据:

  1. public void put(int key, E value) {}
  2. public void append(int key, E value){}

删除操作:

  1. public void delete(int key) {}
  2. public void remove(int key) {}
  3. public void removeAt(int index){}
  4. public void clear(){}

修改数据:

  1. public void put(int key, E value)
  2. public void setValueAt(int index, E value)

查找数据:

  1. public E get(int key)
  2. public E get(int key, E valueIfKeyNotFound)

相应的也有SparseBooleanArray,用来取代HashMap<Integer, Boolean>,SparseIntArray用来取代HashMap<Integer, Integer>,大家有兴趣的可以研究。

SparseArray是android里为<Interger,Object>这样的Hashmap而专门写的类,目的是提高效率,其核心是折半查找函数(binarySearch)。在Android中,当我们需要定义

  1. HashMap<Integer, E> hashMap = new HashMap<Integer, E>();

时,我们可以使用如下的方式来取得更好的性能。

    1. SparseArray<E> sparseArray = new SparseArray<E>();

例子如下:

MainActivity如下:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cc.testsparsearray; 
   
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Set; 
import android.os.Bundle; 
import android.util.SparseArray; 
import android.app.Activity; 
/**
 * Demo描述:
 * SparseArray使用示例
 * 利用SparseArray替换使用HashMap<Integer,E>
 * 类似的还有SparseIntArray,SparseBooleanArray,LongSparseArray 
 
 * 参考资料:
 *   Thank you very much
 */ 
public class MainActivity extends Activity { 
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        init(); 
    
    private void init(){ 
           
        SparseArray sparseArray=new SparseArray<String>(); 
           
        //增加的两种方式 
        sparseArray.append(0, "This is 0"); 
        sparseArray.append(1, "This is 1"); 
        sparseArray.append(2, "This is 2"); 
           
        sparseArray.put(3, "This is 3"); 
        sparseArray.put(4, "This is 4"); 
           
        //遍历 
        for (int i = 0; i < sparseArray.size(); i++) { 
            System.out.println("遍历得到位置"+i+"的值为:"+sparseArray.get(i)); 
        
           
        //查找某个位置的键 
        int key =sparseArray.keyAt(1); 
        System.out.println("查找位置1处的键 key="+key); 
           
        //查找某个位置的值 
        String value=(String) sparseArray.valueAt(1); 
        System.out.println("查找位置1处的值 value="+value); 
           
        //修改的两种方式 
        sparseArray.put(0, "This is new 0"); 
        sparseArray.put(1, "This is new 1"); 
        sparseArray.setValueAt(2, "This is new 2"); 
        sparseArray.setValueAt(3, "This is new 3"); 
        for (int i = 0; i < sparseArray.size(); i++) { 
            System.out.println("修改后遍历得到位置"+i+"的值为:"+sparseArray.get(i)); 
        
           
        //删除 
        sparseArray.delete(0); 
        System.out.println("删除操作后sparseArray大小 size="+sparseArray.size()); 
        //注意: 
        //在执行删除后sparseArray的size()减小了1 
        //为了遍历完,应该将循环条件修改为i < sparseArray.size()+1 
        //HashMap也有类似的情况.参见分割线以下的例子 
        //如果关于SparseArray的遍历有什么好的方法或者建议,多谢 
        for (int i = 0; i < sparseArray.size()+1; i++) { 
            System.out.println("删除后遍历得到位置"+i+"的值为:"+sparseArray.get(i)); 
        
           
           
           
           
           
           
          
        System.out.println("//////////////这是分割线////////////////"); 
           
           
           
           
           
        HashMap<Integer, String> hashMap=new HashMap<Integer, String>(); 
        hashMap.put(0, "000"); 
        hashMap.put(1, "111"); 
        hashMap.put(2, "222"); 
        hashMap.put(3, "333"); 
        hashMap.put(4, "444"); 
        for (int i = 0; i < hashMap.size(); i++) { 
            System.out.println("HashMap遍历得到位置"+i+"的值为:"+hashMap.get(i)); 
        
           
        hashMap.remove(Integer.valueOf(0)); 
        System.out.println("删除操作后hashMap大小 size="+hashMap.size()); 
        //注意: 
        //在执行删除后hashMap的size()减小了1 
        //为了遍历完,应该将循环条件修改为i < hashMap.size()+1 
        for (int i = 0; i < hashMap.size()+1; i++) { 
            System.out.println("HashMap遍历得到位置"+i+"的值为:"+hashMap.get(i)); 
        
           
           
           
        //但是这样做是意义不大的,我们常用的是利用keySet来遍历,如下: 
        Set<Integer> set = hashMap.keySet(); 
        for (Iterator<Integer> iter = set.iterator(); iter.hasNext();) { 
            Integer keyTemp = iter.next(); 
            String valueTemp = hashMap.get(keyTemp); 
            System.out.println("利用keySet遍历:"+keyTemp + "的值是" + valueTemp); 
        
           
    
 
 
main.xml如下:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"
   
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="SparseArray使用示例" 
        android:layout_centerInParent="true" /> 
   
</RelativeLayout

Android 之SparseArray<E>详解的更多相关文章

  1. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  2. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  3. ANDROID L——Material Design详解(UI控件)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! Android L: Google已经确认Android L就是Android Lolli ...

  4. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

  5. Cordova 打包 Android release app 过程详解

    Cordova 打包 Android release app 过程详解 时间 -- :: SegmentFault 原文 https://segmentfault.com/a/119000000517 ...

  6. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  7. 给 Android 开发者的 RxJava 详解

    我从去年开始使用 RxJava ,到现在一年多了.今年加入了 Flipboard 后,看到 Flipboard 的 Android 项目也在使用 RxJava ,并且使用的场景越来越多 .而最近这几个 ...

  8. Android中mesure过程详解

    我们在编写layout的xml文件时会碰到layout_width和layout_height两个属性,对于这两个属性我们有三种选择:赋值成具体的数值,match_parent或者wrap_conte ...

  9. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

随机推荐

  1. HTML5 事件

    下面的表格列出了可插入 HTML 5 元素中以定义事件行为的标准事件属性. Window 事件属性 - Window Event Attributes 表单事件 - Form Events 键盘事件 ...

  2. QStringLiteral的两篇外文解释(编译期转换成QString)

    http://blog.qt.io/blog/2014/06/13/qt-weekly-13-qstringliteral/ https://woboq.com/blog/qstringliteral ...

  3. springMVC框架下JQuery传递并解析Json数据

    springMVC框架下JQuery传递并解析Json数据

  4. rbd块映射

    rbd块映射: root@u18:~# rbd create kvm/test002.img --size root@u18:~# rbd info kvm/test002.img rbd image ...

  5. ListView.MultiChoiceModeListener

    参考:http://www.cnblogs.com/a284628487/p/3460400.html和http://blog.csdn.net/mayingcai1987/article/detai ...

  6. CentOS下配置多个Tomcat同时运行 本篇文章来源于 Linux公社网站(www.linuxidc.com)

    原文地址:http://blog.csdn.net/tjcyjd/article/details/46553361 版权声明:本文为博主原创文章,未经博主允许不得转载. 同一服务器部署多个tomcat ...

  7. PHP学习笔记3-逻辑运算符

    逻辑运算符图解: 逻辑且&&: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/26 ...

  8. Python 模块续和面向对象的介绍(六)

    一.基本模块 shutil 文件.目录.压缩包的处理模块 shutil.copyfile(src, dst) 拷贝文件 >>> shutil.copyfile('a.log','b. ...

  9. Web Application的目录结构

    Java web工程下的webapp或WebContent就是工程的发布文件夹,发布时会把该文件夹发布到tomcat的webapps里. 一个web应用必须要有的目录文件如下: webapp/WebC ...

  10. MVC是一种用于表示层设计的复合设计模式

    它们之间的交互有以下几种:       1.当用户在视图上做任何需要调用模型的操作时,它的请求将被控制器截获.       2.控制器按照自身指定的策略,将用户行为翻译成模型操作,调用模型相应逻辑实现 ...