Java SparseBooleanArray 的明确目的是什么?[我为此提到了官方android网站]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18822708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the Clear Purpose of SparseBooleanArray?? [ I referred official android site for this ]
提问by Ripal Tamboli
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??
我参考了“SparseBooleanArray”类的android doc站点,但仍然不了解该类的目的是什么?我们需要使用那个类的目的是什么??
Here is the Doc Link http://developer.android.com/reference/android/util/SparseBooleanArray.html
这是文档链接 http://developer.android.com/reference/android/util/SparseBooleanArray.html
采纳答案by LuigiEdlCarno
From what I get from the documentation it is for mapping Integer values to booleans.
从我从文档中获得的信息来看,它用于将整数值映射到布尔值。
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.
也就是说,如果您要映射,如果应该为某个用户 ID 显示一个小部件并且某些用户 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.
意思是,对于普通数组,您将创建一个 size=maxID 的数组,并向 index=userID 处的元素添加一个布尔值。然后在迭代数组时,在最坏的情况下,您必须迭代 maxID 元素,并且必须检查null
该索引是否没有布尔值(例如,ID 不存在)。那实在是效率低下。
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.
当使用 hashmap 来做到这一点时,你可以将 ID 映射到布尔值,但会增加为键生成哈希值的开销(这就是为什么它被称为 * hash*map),这最终会首先在 CPU 周期中损害性能以及 RAM 使用率。
So that SparseBooleanArray seems like a good middleway of dealing with such a situation.
所以 SparseBooleanArray 似乎是处理这种情况的一个很好的中间方式。
NOTE:Even though my example is really contrieved, I hope it illustrates the situation.
注意:尽管我的例子真的很做作,但我希望它说明了这种情况。
回答by Thihara
Like the javadoc says, SparseBooleanArrays map integers to booleans
which basically means that it's like a map with Integer as a key and a boolean as value (Map).
就像 javadoc 所说的那样,SparseBooleanArrays map integers to booleans
这基本上意味着它就像一个以 Integer 作为键和一个布尔值作为值(Map)的映射。
However it's more efficient to use in this particular case It is intended to be more efficient than using a HashMap to map Integers to Booleans
但是在这种特殊情况下使用更有效 It is intended to be more efficient than using a HashMap to map Integers to Booleans
Hope this clears out any issues you had with the description.
希望这可以解决您对描述的任何问题。
回答by JamisonMan111
I found a very specific and wonderful use for the sparse boolean array.
我发现了稀疏布尔数组的一个非常具体和美妙的用途。
You can put a true or false value to be associated with a position in a list.
您可以将真值或假值与列表中的位置相关联。
For example: List item #7 was clicked, so putting 7 as the key and true as the value.
例如:单击了列表项 #7,因此将 7 作为键和 true 作为值。
回答by Archit Puri
There can be three ways to store resource id's
可以通过三种方式来存储资源 ID
1 Array
1阵列
Boolean array containing id's as indexes.If we have used that id set it to true else false
包含 id 作为索引的布尔数组。如果我们使用了该 id,请将其设置为 true 否则为 false
Though all the operations are fast but this implementation will require huge amount of space.So it can't be used
虽然所有的操作都很快,但是这个实现需要大量的空间。所以它不能使用
High Space Complexity
高空间复杂度
2 HashMap
2哈希映射
Key-ID
密钥 ID
Value-Boolean True/False
值布尔型 True/False
Using this we need to process each id using the hashing function which will consume memory.Also there may be some empty locationswhere no id will be stored and we also need to deal with crashes.So due to usage complexityand medium space complexity, it is not used.
使用这个我们需要使用散列函数来处理每个 id 会消耗内存。另外可能会有一些空位置没有 id 将被存储,我们还需要处理崩溃。所以由于使用复杂性和中等空间复杂性,它未使用。
Medium Space Complexity
中等空间复杂度
3 SparseBooleanArray
3稀疏布尔数组
It is middle way.It uses mappingand Array Implementation
它是中间方式。它使用映射和数组实现
Key - ID
钥匙 - 身
Value - Boolean True/False
值 - 布尔值 True/False
It is an ArrayList which stores id's in an increasing order.So minimum space is used as it only contains id's which are being used.For searching an id binary search is used. Though Binary Search O(logn) is slower than hashing O(1) or Array O(1),i.e. all the operations Insertion, Deletion, Searching will take more timebut there is least memory wastage.So to save memory we prefer SparseBoolean Array
它是一个以递增顺序存储 id 的 ArrayList。因此使用最小空间,因为它只包含正在使用的 id。对于搜索 id 使用二进制搜索。虽然二分搜索 O(logn) 比散列 O(1) 或数组 O(1) 慢,即所有操作插入、删除、搜索将花费更多时间但内存浪费最少。所以为了节省内存我们更喜欢稀疏布尔数组
Least Space Complexity
最小空间复杂度