Android利用SparseArray替换使用HashMap<Integer,E>

来源:互联网 发布:冰点还原精灵类似软件 编辑:程序博客网 时间:2024/04/30 12:03

MainActivity如下:

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


 

main.xml如下:

<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> 

//---------------------------------------------------------自己使用-----

总结:(1)遍历SparseArray使用for array的循环。

           (2)加入一个LinearLayout里面有三个ImageView,将三个ImageView放进去比较方便操作。

    否则,会使用比较多的if else等。

0 0