• Android 之SparseArray<E>详解


    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
  • 相关阅读:
    VSTO开发指南(VB2013版) 第四章 Excel编程
    VSTO开发指南(VB2013版) 第三章 Excel编程
    VSTO开发指南(VB2013版) 第二章 Office解决方案介绍
    VSTO开发指南(VB2013版) 第一章 Office对象模型
    打印预览
    打印
    工具函数
    开始使用
    模版对应信息
    解决PLSQL或者sqlplus连接oracle慢的方法
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4638522.html
Copyright © 2020-2023  润新知