Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
What is it connected with?
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
// ...
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned = null;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
ListItr(int index) {
// assert isPositionIndex(index);
next = (index == size) ? null : node(index);
nextIndex = index;
}
public boolean hasNext() {
return nextIndex < size;
}
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
ArrayList is practically(*) always faster than LinkedList. This is because ArrayList stores data in a contiguous chunk of memory. LinkedList allocates memory for each node and connects them with links.
Modern processors work well with continuous data. This is due to the operation of the cache and the superscalar architecture. The processor can load data ahead, use vector SSE instructions, etc.
https://habr.com/en/post/312078/
In LinkedList, each iteration will probably result in a memory access. For an array, this is incrementing a counter in a processor register. The time difference can reach hundreds of times.
*LinkedList is asymptotically (given a large enough size) faster at inserting and removing elements from the middle, since the array needs to shift elements. However, this can only be seen on arrays with hundreds or even thousands of elements.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question