ArrayList、LinkedList、Vector的區別
①ArrayList底層實際上是採用數組實現的(並且該數組的類型的Object類型的)
②如果jdk6,採用Array.copyOf()方法來生成一個新的數組,如果是jdk5,採用的是System.arraycopy()方法(當添加的數據量大於數組的長度時候)
③List list = newArrayList()時,底層會生成一個長度爲10的數組來存放對象
④ArrayList、Vector底層都是採用數組實現的
⑤對於ArrayList和LinkeList,方法都不是同步的,對於Vector,大部分public方法都是同步的,
⑥LinkedList採用雙向循環鏈表
⑦對於ArrayList,查詢速度很快,增加和刪除(非最後一個節點)操作非常慢(本質上由數組的特性決定的)
⑧對於LinkedList,查詢速度非常慢,增加和刪除操作非常快(本質上是由雙向循環鏈表決定的)
- private transient Object elementData[];
- public ArrayList(int i) {
- if (i < 0) {
- throw new IllegalArgumentException((new StringBuilder())
- .append("Illegal Capacity: ").append(i).toString());
- } else {
- elementData = new Object[i];
- return;
- }
- }
- public ArrayList() {
- this(10);
- }
2、LinkList默認構造方法
- public LinkedList() {
- header = new Entry(null, null, null);
- size = 0;
- header.next = header.previous = header;
- }
- private static class Entry {
- Object element;
- Entry next;
- Entry previous;
- Entry(Object obj, Entry entry1, Entry entry2) {
- element = obj;
- next = entry1;
- previous = entry2;
- }
- }
- private transient Entry header;
- private transient int size;
- public Vector(int i, int j) {
- if (i < 0) {
- throw new IllegalArgumentException((new StringBuilder())
- .append("Illegal Capacity: ").append(i).toString());
- } else {
- elementData = new Object[i];
- capacityIncrement = j;
- return;
- }
- }
- public Vector(int i) {
- this(i, 0);
- }
- public Vector() {
- this(10);
- }
- protected Object elementData[];
- protected int capacityIncrement;