摘要: 1代码优化1.1缓存结果使用Android的稀疏数组(SpareArray):SparseArray、SpareBooleanArray、SparseIntArray对结果进行缓存.当然也可以使用也可是使用Java容器缓存.不过Android定义的SpareArray类,当键是整数时,比容器的HashMap效率高.因为HashMap使用的是java.lang.Integer对象,而SparseArray使用的基本类型int.因此使用HashMap会创建很多Integer对象,而使用SpareArray则可避免.当然HashMap也是有好处的,就是可以不依赖android.总之:使用缓存存储结果 阅读全文
posted @ 2014-02-21 17:11 似水流云 阅读(327) 评论(0) 推荐(0) 编辑
摘要: SparseArray是android里为这样的Hashmap而专门写的class,目的是提高效率,其核心是折半查找函数(binarySearch) private static int binarySearch(int[] a, int start, int len, int key) { int high = start + len, low = start - 1, guess; while (high - low > 1) { guess = (high + low) / 2; if (a[guess] ... 阅读全文
posted @ 2014-02-21 16:48 似水流云 阅读(245) 评论(0) 推荐(0) 编辑
摘要: 一、SparseIntArray API SparseIntArrays map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure . 阅读全文
posted @ 2014-02-21 16:36 似水流云 阅读(397) 评论(0) 推荐(0) 编辑
摘要: 多数Android开发者都知道在Android中可以使用HashMap来映射一种对应关系,在java开发中HashMap也算是一种很重要的数据存储结构。然后很多人在Android开发中多数都会用HashMap来存储这种映射形式的数据。 其实,Android提供了一种更加适合Android开发的数据结构——SparseArray。初看之下,好像是一种数组,其实不然。数组的index是连续的。而SparseArray可以不连续,所以导致SparseArray就具有HashMap的一些特性,但是比HashMap的性能要好。既然性能要好很多,那是不是所有可以用HashMap的地方都可以用Spar... 阅读全文
posted @ 2014-02-21 16:27 似水流云 阅读(1350) 评论(0) 推荐(0) 编辑