##ArrayList
ArrayList internally is actually a dynamic array
1 |
|
And if we look at the constructor1
2
3
4
5
6
7
8
9
10public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
Initlally, it tries to create an empty array or a new array with ‘initialCapacity’;
##LinkedList
1 | public class LinkedList<E> |
LinkedList internally is actually a double linked list, it has head and tail (first, last node) information.
So when get(index) to get an element, in array, the time complexitiy is O(1); but linkedlist is O(n);
1 |
|
More details can be found in java SDK, It seems most of time we can use ArrayList.
Which is similar we use ‘Vector’ in C++;