什么是SparseBooleanArray的明确目的? [我提到官方的android网站] [英] What is the Clear Purpose of SparseBooleanArray?? [ I referred official android site for this ]

查看:139
本文介绍了什么是SparseBooleanArray的明确目的? [我提到官方的android网站]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将android doc网站称为SparseBooleanArray类,但仍然没有想到该类关于该类的目的是什么?出于什么目的我们需要使用该类??

I referred the android doc site for "SparseBooleanArray" class but still not getting idea of that class about what is the purpose of that class?? For what purpose we need to use that class??

这是Doc Link
http://developer.android.com/reference/android/util/SparseBooleanArray.html

Here is the Doc Link http://developer.android.com/reference/android/util/SparseBooleanArray.html

推荐答案

从文档中我得到的是将Integer值映射到布尔值。

From what I get from the documentation it is for mapping Integer values to booleans.

也就是说,如果要映射,如果对于某个用户ID,应该显示一个窗口小部件,并且某些用户标识已被删除,您的映射中会有间隙。

That is, if you want to map, if for a certain userID a widget should be shown and some userIDs have already been deleted, you would have gaps in your mapping.

含义,使用普通数组,您将创建一个size = maxID的数组,并在index = userID处为元素添加一个布尔值。然后,当迭代数组时,你必须在最坏的情况下迭代maxID元素,如果该索引没有布尔值,则必须检查 null (例如, ID不存在)。这是非常低效的。

Meaning, with a normal array, you would create an array of size=maxID and add a boolean value to element at index=userID. Then when iterating over the array, you would have to iterate over maxID elements in the worst case and have to check for null if there is no boolean for that index (eg. the ID does not exist). That is really inefficient.

当使用hashmap执行此操作时,您可以将ID映射到布尔值,但增加了为密钥生成哈希值的开销(即这就是为什么它被称为* hash * map),这最终会在CPU周期和RAM使用中首先损害性能。

When using a hashmap to do that you could map the ID to the boolean, but with the added overhead of generating the hashvalue for the key (that is why it is called *hash*map), which would ultimately hurt performance firstly in CPU cycles as well as RAM usage.

这样SparseBooleanArray似乎是处理这种情况的一个很好的中途。

So that SparseBooleanArray seems like a good middleway of dealing with such a situation.

注意:尽管我的例子真的很有用,但我希望它能说明情况。

NOTE: Even though my example is really contrieved, I hope it illustrates the situation.

这篇关于什么是SparseBooleanArray的明确目的? [我提到官方的android网站]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆