我想知道SparseArray
和ArrayList
的性能和效率,哪个更好用。
我不明白何时使用 SparseArray
以及何时使用 ArrayList
?
最佳答案
SparseArray
的目的是在您有一个有很多空白的列表时节省内存。如果您只有 10 个项目,并且索引它们的数字范围从 0 到 1000,那么一个 ArrayList
将有很多 null
条目,这将是非常浪费的。 SparseArray
将在内部使用数据结构来避免该问题。
这种情况下的替代方案是 HashMap
,如果您有很多项目,它比 SparseArray
更好。
The implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.
关于java - SparseArray 与 ArrayList 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25444226/