什么是数据结构:
数据结构是计算机存储、组织数据的方式。
数据结构是指相互之间在一种或多种特定关系的数据元素的集合。
通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同效率的检索算法和索引技术有关。
常见的数据结构:数组(Array)、栈(Stack)、链表(Linked List)、哈希表(Hash)、队列(Queue)、堆(Heap)、图(Graph)、树(Tree)
Java中集合框架其实就是数据结构的实现的封装。
不同的数据结构的操作性能是不同的:(有的查询性能很快、有的插入速度很快、有的是插入头和尾的速度很快、有的做等值判断很快、有的做范围查询很快、有的允许元素重复、有的不允许重复等等),在开发中是要根据具体的需求来选择的。
最简单的数据结构就是数组
数据结构的作用:
1、模拟生活中数据的存储(对信息的增删改查的操作);
如果把数据保存在数组的最后一个位置,至少需要操作一次;
如果把数据保存在数组第一个位置,如果存在N个元素,此时需要操作N次(后面的元素要整体后移)
如果删除最后一个元素,操作一次;
如果删除第一个元素,操作N次。
如果根据索引查询元素:操作一次;
如果根据元素查询索引,此时使用线性搜索。
链表结构:
1) 单向链表:只能从头遍历到尾/只能从尾遍历到头
2) 双向链表:既可以从头遍历到尾,又可以从尾遍历到头
通过引用来表示上一节点和下一节点的关系
对LinekdList操作的性能分析:
1) 增加操作:
双向链表可以直接获取自己的第一个和最后一个节点;
如果删除第一个元素:操作一次;
如果删除最后一个元素:操作一次;
如果删除中间的元素:
找到元素节点平均操作:(1+N)/2次;
找到节点之后做删除操作:1次。
3) 查询操作:
平均:(N+1)/2次
ArrayList:查询和更改较快,新增和删除较慢;
LinkedList:查询和更改较慢,新增和删除较快。
数据结构是计算机存储、组织数据的方式。
数据结构是指相互之间在一种或多种特定关系的数据元素的集合。
通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同效率的检索算法和索引技术有关。
常见的数据结构:数组(Array)、栈(Stack)、链表(Linked List)、哈希表(Hash)、队列(Queue)、堆(Heap)、图(Graph)、树(Tree)
Java中集合框架其实就是数据结构的实现的封装。
不同的数据结构的操作性能是不同的:(有的查询性能很快、有的插入速度很快、有的是插入头和尾的速度很快、有的做等值判断很快、有的做范围查询很快、有的允许元素重复、有的不允许重复等等),在开发中是要根据具体的需求来选择的。
最简单的数据结构就是数组
数据结构的作用:
1、模拟生活中数据的存储(对信息的增删改查的操作);
2、作为程序员的开发工具(使用非常频繁,所以把共同的操作封装成工具)。
接下来浏览一下ArrayList源码(只选取常用的方法):
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
transient Object[] elementData;
private int size;
/**
* 构造一个具有指定初始容量的空列表。
* @param initialCapacity
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* 构造一个初始容量为 10 的空列表。
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
/**
* 构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的。
* @param c
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
/**
* 将此 ArrayList 实例的容量调整为列表的当前大小。
*/
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}
/**
* 如有必要,增加此 ArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。
* @param minCapacity
*/
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != EMPTY_ELEMENTDATA)
// any size if real element table
? 0
// larger than default for empty table. It's already supposed to be
// at default size.
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
/**
* 返回此列表中的元素数。
* @return
*/
public int size() {
return size;
}
/**
* 如果此列表中没有元素,则返回 true
* @return
*/
public boolean isEmpty() {
return size == 0;
}
/**
* 如果此列表中包含指定的元素,则返回 true。
* @param o
* @return
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
* 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
* @param o
* @return
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
* 返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
* @param o
* @return
*/
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
* 返回此 ArrayList 实例的浅表副本。
* @return
*/
public Object clone() {
try {
ArrayList<?> v = (ArrayList<?>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
/**
* 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
* @return
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
/**
* 按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型。
* @param a
* @return
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
/**
* 返回此列表中指定位置上的元素。
* @param index
* @return
*/
public E get(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
return (E) elementData[index];
}
/**
* 用指定的元素替代此列表中指定位置上的元素。
* @param index
* @param element
* @return
*/
public E set(int index, E element) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
/**
* 将指定的元素添加到此列表的尾部。
* @param e
* @return
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
* 将指定的元素插入此列表中的指定位置。
* @param index
* @param element
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
/**
* 移除此列表中指定位置上的元素。
* @param index
* @return
*/
public E remove(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
/**
* 移除此列表中首次出现的指定元素(如果存在)。
* @param o
* @return
*/
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
/**
* 移除此列表中的所有元素。
*/
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
/**
* 按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部。
* @param c
* @return
*/
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
/**
* 从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。
* @param index
* @param c
* @return
*/
public boolean addAll(int index, Collection<? extends E> c) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
* 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
* @param fromIndex
* @param toIndex
*/
protected void removeRange(int fromIndex, int toIndex) {
// Android-changed : Throw an IOOBE if toIndex < fromIndex as documented.
// All the other cases (negative indices, or indices greater than the size
// will be thrown by System#arrayCopy.
if (toIndex < fromIndex) {
throw new IndexOutOfBoundsException("toIndex < fromIndex");
}
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
}
针对ArrayList存储数据增删改查(CRUD)的性能分析:
1)保存操作:如果把数据保存在数组的最后一个位置,至少需要操作一次;
如果把数据保存在数组第一个位置,如果存在N个元素,此时需要操作N次(后面的元素要整体后移)
平均:(N+1)/2次,N表示数组中元素的个数,如果要扩容,更慢,性能更低。
如果删除最后一个元素,操作一次;
如果删除第一个元素,操作N次。
平均:(N+1)/2次
3)修改操作:操作一次。
如果根据索引查询元素:操作一次;
如果根据元素查询索引,此时使用线性搜索。
平均(N+1)/2次
链表结构:
1) 单向链表:只能从头遍历到尾/只能从尾遍历到头
2) 双向链表:既可以从头遍历到尾,又可以从尾遍历到头
通过引用来表示上一节点和下一节点的关系
接下来浏览一下LinkedList源码(只选取常用的方法):
public class LinkedList<E> extends AbstractSequentialList<E> implements
List<E>, Deque<E>, Cloneable, java.io.Serializable {
transient int size = 0;
transient Node<E> first;
transient Node<E> last;
/**
* 构造一个空列表。
*/
public LinkedList() {
}
/**
* 构造一个包含指定 collection 中的元素的列表,这些元素按其 collection 的迭代器返回的顺序排列。
*
* @param c
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
/**
* Links e as first element.
*/
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
/**
* Links e as last element.
*/
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
/**
* Inserts element e before non-null Node succ.
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
/**
* Unlinks non-null first node f.
*/
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null; // help GC
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
/**
* Unlinks non-null last node l.
*/
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
/**
* Unlinks non-null node x.
*/
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
/**
* 返回此列表的第一个元素。
*
* @return
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
/**
* 返回此列表的最后一个元素。
*
* @return
*/
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
/**
* 移除并返回此列表的第一个元素。
*
* @return
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* 移除并返回此列表的最后一个元素。
*
* @return
*/
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
/**
* 将指定元素插入此列表的开头。
*
* @param e
*/
public void addFirst(E e) {
linkFirst(e);
}
/**
* 将指定元素添加到此列表的结尾。
*
* @param e
*/
public void addLast(E e) {
linkLast(e);
}
/**
* 如果此列表包含指定元素,则返回 true。
*
* @param o
* @return
*/
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
* 返回此列表的元素数。
*
* @return
*/
public int size() {
return size;
}
/**
* 将指定元素添加到此列表的结尾。
*
* @param e
* @return
*/
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* 从此列表中移除首次出现的指定元素(如果存在)。
*
* @param o
* @return
*/
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
/**
* 添加指定 collection 中的所有元素到此列表的结尾,顺序是指定 collection 的迭代器返回这些元素的顺序。
*
* @param c
* @return
*/
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
/**
* 将指定 collection 中的所有元素从指定位置开始插入此列表。
*
* @param index
* @param c
* @return
*/
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked")
E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
/**
* 从此列表中移除所有元素。
*/
public void clear() {
for (Node<E> x = first; x != null;) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
/**
* 返回此列表中指定位置处的元素。
*
* @param index
* @return
*/
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
/**
* 将此列表中指定位置的元素替换为指定的元素。
*
* @param index
* @param element
* @return
*/
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
/**
* 在此列表中指定的位置插入指定的元素。
*
* @param index
* @param element
*/
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 移除此列表中指定位置处的元素。
*
* @param index
* @return
*/
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
/**
* Tells if the argument is the index of an existing element.
*/
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
/**
* Tells if the argument is the index of a valid position for an iterator or
* an add operation.
*/
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Size: " + size;
}
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
/**
* 返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
*
* @param o
* @return
*/
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
/**
* 返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1。
*
* @param o
* @return
*/
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
/**
* 获取但不移除此列表的头(第一个元素)。
*
* @return
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
/**
* 获取但不移除此列表的头(第一个元素)。
*
* @return
*/
public E element() {
return getFirst();
}
/**
* 获取并移除此列表的头(第一个元素)
*
* @return
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
/**
* 获取并移除此列表的头(第一个元素)。
*
* @return
*/
public E remove() {
return removeFirst();
}
/**
* 将指定元素添加到此列表的末尾(最后一个元素)。
*
* @param e
* @return
*/
public boolean offer(E e) {
return add(e);
}
/**
* 在此列表的开头插入指定的元素。
*
* @param e
* @return
*/
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
/**
* 在此列表末尾插入指定的元素。
*
* @param e
* @return
*/
public boolean offerLast(E e) {
addLast(e);
return true;
}
/**
* 获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
*
* @return
*/
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
/**
* 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
*
* @return
*/
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
/**
* 获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
*
* @return
*/
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
/**
* 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
*
* @return
*/
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
/**
* 将元素推入此列表所表示的堆栈。
*
* @param e
*/
public void push(E e) {
addFirst(e);
}
/**
* 从此列表所表示的堆栈处弹出一个元素。
*
* @return
*/
public E pop() {
return removeFirst();
}
/**
* 从此列表中移除第一次出现的指定元素(从头部到尾部遍历列表时)。
*
* @param o
* @return
*/
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
/**
* 从此列表中移除最后一次出现的指定元素(从头部到尾部遍历列表时)。
*
* @param o
* @return
*/
public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
}
对LinekdList操作的性能分析:
1) 增加操作:
双向链表可以直接获取自己的第一个和最后一个节点;
如果新增的元素在第一个或最后一个位置,那么操作只有一次。
如果删除第一个元素:操作一次;
如果删除最后一个元素:操作一次;
如果删除中间的元素:
找到元素节点平均操作:(1+N)/2次;
找到节点之后做删除操作:1次。
3) 查询操作:
平均:(N+1)/2次
平均:(N+1)/2次
ArrayList:查询和更改较快,新增和删除较慢;
LinkedList:查询和更改较慢,新增和删除较快。