开发中,一个贴切的数据结构,可以非常好的满足需求的同时,最大程度的节省内存,这是一个精细的开发者必须要考虑的事情。
在这里,稍微总结一下安卓开发中经常会使用到的一些数据结构,请大佬批评指正。
1,数据结构与算法
数据结构,表示一种数据模型,模型的元素之间存在着逻辑结构以及存储结构;
算法表示一种规则或者说是一种策略。
数据对象中数据元素之间的相互关系,也就是逻辑关系基本上可以分为:
集合结构,线性结构,属性结构,图形结构。
存储关系:
链式存储,顺序存储;
Android中的List就是线性的存储结构,List本身是一个接口,使用过程中其实是使用它的实现类,其中ArrayList是线性的存储结构,LinkList是链式的存储结构。
线性的存储结构的特点是快速的查询,可以很方便的计算各个元素的地址,但是插入和删除的操作消耗是比较大的,因为删掉或者插入中间的某一个元素,这个元素后面的元素都要进行位置的变迁,
线性存储单元,在内存中是连续的。但是链式存储单元之间,可以是连续的也可以是不连续的。链式存储单元内部包含存储数据的部分,还包含一个指示其直接后继的信息的部分,其实就是相当于c语言中的指针,Java中的下一个元素的对象。
链式存储的特点是,查找耗费比较高,但是删除和插入操作是最快的,因为插入删除,只是和当前的位置的一个节点有关系。
到底层看看源码是怎么写的:
private static final int MIN_CAPACITY_INCREMENT = 12;
/**
* The elements in this list, followed by nulls.
*/
transient Object[] array;
/**
* Constructs a new instance of {@code ArrayList} with the specified
* initial capacity.
*
* @param capacity
* the initial capacity of this {@code ArrayList}.
*/
public ArrayList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]);
}
/**
* Constructs a new {@code ArrayList} instance with zero initial capacity.
*/
public ArrayList() {
array = EmptyArray.OBJECT;
}
/**
* This method controls the growth of ArrayList capacities. It represents
* a time-space tradeoff: we don't want to grow lists too frequently
* (which wastes time and fragments storage), but we don't want to waste
* too much space in unused excess capacity.
*
* NOTE: This method is inlined into {@link #add(Object)} for performance.
* If you change the method, change it there too!
*/
private static int newCapacity(int currentCapacity) {
int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ?
MIN_CAPACITY_INCREMENT : c