Android利用SparseArray替换使用HashMap<Integer,E>
来源:互联网 发布:冰点还原精灵类似软件 编辑:程序博客网 时间:2024/04/30 12:03
MainActivity如下:
- 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
- *
- * 参考资料:
- * 1 http://liuzhichao.com/p/832.html
- * 2 http://blog.csdn.net/xyz_fly/article/details/7931943
- * 3 http://my.eoe.cn/appadventure/archive/2824.html
- * 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如下:
<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
- Android利用SparseArray替换使用HashMap<Integer,E>
- Android利用SparseArray替换使用HashMap<Integer,E>
- Android中推荐用SparseArray替换HashMap<Integer,Object>
- Android使用SparseArray取代HashMap
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- Android编程之SparseArray<E>详解[取代HashMap]
- android中使用SparseArray代替HashMap
- Android:使用SparseArray代替HashMap优化性能
- Android SparseArray替代HashMap
- Android--SparseArray替代HashMap
- Android SparseArray代替HashMap
- 使用SparseArray代替HashMap
- Android -- SparseArray<E>详解
- 操作表ActionSheet和警报AlertView
- apache安装、简单配置负载均衡和会话保持
- jsp不能识别el表达式
- 游戏卡牌UI设计教程
- 网络游戏步骤
- Android利用SparseArray替换使用HashMap<Integer,E>
- 一个优秀的程序员是如何炼成的
- C语言第八回合:分支、循环和跳转
- Junit单体测试
- java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapOb
- 观察者模式
- 一步一步学zedboard之十编译内核
- mongodb学习4-简单的CRUD
- Member function callback