在 Java 中,我什么时候会在 sparseArray 上使用 ArrayList?

In Java, when would i use ArrayList over a sparseArray?

使用 ArrayList 比使用 sparseArray 有什么优势吗? sparseArray 是更好的内存管理,因为它不会像 ArrayList 那样将 null 放入空槽中。但是你会总是推荐我使用 sparseArray 而不是非常常用的 ArrayList 吗?

明确地说,我不是在问什么是 sparseArray(我已经在上面定义了它),我是在问什么时候人们想在 sparseArray 上使用 ArrayList。

当列表不是稀疏时,ArrayList 比 SparseArray 需要更少的内存,并且在 O(1) 而不是 O(log n) 中按索引访问。

来自SparseArray class documentation

Note that this container keeps its mappings in an array data structure, using a binary search to find keys. 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%.