数据结构之集合与链表
总体
集合 | 链表 | |
---|---|---|
空间 | 连续 | 不连续 |
访问速度 | 下标访问,快 | 遍历,慢 |
插入速度 | 有可能需移动其他元素,慢 | 修改引用,快 |
代码
集合
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
List<Integer> iList = new ArrayList<>();
iList.add(1);
iList.add(2);
iList.add(3);
链表
/**
* 单向链表
*/
public class OneWayLink {
/**
* other property
*/
int i;
/**
* next OBJ,引用地址,实际对象可能在堆内存中的另一块空间
*/
OneWayLink next;
}
/**
* 双向链表
*/
class TwoWayLink {
/**
* other property
*/
int i;
/**
* previous OBJ
*/
TwoWayLink previous;
/**
* next OBJ
*/
TwoWayLink next;
}
如代码所示链表实现中,链上前后对象的关系通过对象的地址(指针)来关联,实际的对象可能在堆内存中的另一块区域,所以链表的内存空间是不连续的。
当插入、删除时,只需要修改对应对象的属性的值(引用地址)就可以。从而达到高效。