网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Android特有的数据结构SpareArray详解

在android developer 的开发文当中是这么描述SparesArray的:

成都创新互联专注于离石网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供离石营销型网站建设,离石网站制作、离石网页设计、离石网站官网定制、重庆小程序开发公司服务,打造离石网络公司原创品牌,更为您提供离石网站排名全网营销落地服务。

SparseArrays map integers to Objects. Unlike a normal array ofObjects, there can be gaps in the indices. It is intended to be more efficientthan using a HashMap to map Integers to Objects.

大概意思是SparseArrays是映射Integer—> Objects 类型的,就像这样: SparseArrays< Object>而且在指数级别的数量的增长上来说和HashMap相比较的话,性能方面SparseArrays更好。

  

为什么会突然说SparseArrays呢?因为我在看Activity源码的时候看到了这段代码:

privatestaticclassManagedDialog {

        DialogmDialog;

        BundlemArgs;

   }

private SparseArraymManagedDialogs;

我不知道SparseArray是什么,然后点进去继续看这个类的实现,和开始介绍

/**

 * SparseArrays map integers to Objects.  Unlike a normal array of Objects,

 * there can be gaps in the indices.  It is intended to be more memory efficient

 * than using a HashMap to map Integers toObjects, both because it avoids

 * auto-boxing keys and its data structure doesn'trely on an extra entry object

 * for each mapping.

 **/

前半段和上面api文档上介绍的一样,后半段的意思就是因为数据结构的实体类不依赖外在实体并且避免了自动装箱时候的key,所以效率更高。(效率高的另外一个原因我们等下再说)

我继续回来看代码发现在下面这段代码中用到了SparseArray.

 

finalint[]ids = b.getIntArray(SAVED_DIALOG_IDS_KEY);

finalintnumDialogs = ids.length;

 mManagedDialogs =newSparseArray(numDialogs);

    for(inti = 0; i < numDialogs; i++) {

       final Integer dialogId = ids[i];

         Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId));

      if (dialogState !=null) {

        // Calling onRestoreInstanceState() below will invoke dispatchOnCreate

         // so tell createDialog() not to do it, otherwise we get an exception

               final ManagedDialog md =new ManagedDialog();

                md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId));

                md.mDialog = createDialog(dialogId, dialogState, md.mArgs);

                if (md.mDialog !=null) {

                   mManagedDialogs.put(dialogId, md);

                    onPrepareDialog(dialogId,md.mDialog,md.mArgs);

                    md.mDialog.onRestoreInstanceState(dialogState);

                }

          }

      }

上面这段代码做的事情就是根据对话框id的个数来创建SparesArrays的大小。并且根据ManagedDialog的id存dialogState,也就是存dialog的状态。说白了就是格式的HashMap。

 

我回去继续阅读SparesArrays的源码发现:

Android 特有的数据结构SpareArray 详解

 

它的构造方法设置了默认大小为10,

和List一样,获取元素的方法有两个如图所示我们可以发现

public E get(int key)方法还是调用的public E get(int key, E valueIfKeyNotFound)。只是在找不到元素的时候设置了默认的返回值为null。

值得注意的是,它查找的时候用的是二分查找(折半查找)算法,所以快。

Android 特有的数据结构SpareArray 详解

感觉整个SparesArrays的核心就是binarySearch。我们看下它的源代码:

static int binarySearch(int[] array, int size, intvalue) {

        int lo =0;

        int hi =size - 1;

        while(lo <= hi) {

           final int mid = (lo + hi) >>> 1;

           final int midVal = array[mid];

 

            if(midVal < value) {

               lo = mid + 1;

            }else if (midVal > value) {

               hi = mid - 1;

            }else {

               return mid;  // value found

            }

        }

        return~lo;  // value not present

    }

 

    static intbinarySearch(long[] array, int size, long value) {

        int lo =0;

        int hi =size - 1;

 

        while(lo <= hi) {

           final int mid = (lo + hi) >>> 1;

           final long midVal = array[mid];

 

            if(midVal < value) {

               lo = mid + 1;

            }else if (midVal > value) {

               hi = mid - 1;

            }else {

                return mid;  // value found

            }

        }

        return~lo;  // value not present

    }

 

再来看它删除的时候有四种方法;

Android 特有的数据结构SpareArray 详解

前三种中

public void delete(int key)是根据折半查找删除,

public void remove(int key)调用的是delete(int key)

public void removeAt(int index)根据index删除

public void removeAtRange(int index, int size)这个是调用publicvoid removeAt(int index)(上面截图中没有)

 

添加数据方法有public voidput(int key, E value)

Android 特有的数据结构SpareArray 详解

在红框中的代码表示 put的时候会先查案当前的SparesArrays中是否存在此数据,如果存在那么就是修改这个数值,不会添加,如果没有查找到那么久会添加。

还有一个方法是修改数组中的数值的

public void setValueAt(int index, Evalue)

Android 特有的数据结构SpareArray 详解

如注释中说的,会给index的位置的value进行修改。

public int keyAt(int index)这个方法是根据index来获取key值。范围是0-size-1.

Android 特有的数据结构SpareArray 详解

同样的public E valueAt(int index)就是根据index来获取对应位置的object。如图:

Android 特有的数据结构SpareArray 详解

还有public void append(int key,E value)方法,这个方法是在key远远大于数组中所有的key的时候添加数据提高性能的方法,如果key>0 并且key小于等等数组中最后一个的key的情况下还是调用的put方法。

Android 特有的数据结构SpareArray 详解

最后一个方法就是toString,也就是打印出整个数组的字符串。这个没多大意思就不贴出代码了。

 

另外 想要阅读android源码的一种方式可以戳这里:

http://poarry.blog.51cto.com/5970996/1630731

 

以上纯属个人见解,如有失误,欢迎指正poarryScript@gmail.com


分享文章:Android特有的数据结构SpareArray详解
分享链接:http://bjjierui.cn/article/jgdjjs.html

其他资讯