Loading…
Loading…
30 карточек
What is the difference in the internal implementation of ArrayList and LinkedList, and how does it affect performance?
нажми, чтобы перевернуть
ArrayList stores elements in a contiguous memory block (Object[]), providing excellent CPU cache locality. When the array fills up, it copies elements to a new array 50% larger (growth factor of 1.5x).
LinkedList consists of separate node objects (Node<E>), each storing references to the previous and next elements. This results in an overhead of ~24-32 bytes per element and memory fragmentation.
CPU caching prefers contiguous blocks. For most tasks,
ArrayListis faster even with frequent middle insertions, as long as the collection size doesn't exceed a few thousand elements.
Состояния:
Когда да
Index-based reading, frequent iterations, predictable size, or data buffering.
Когда нет
Frequent insertions/deletions at the beginning/middle without using an iterator, or working under strict memory constraints.
Свайп вправо — знаю, влево — не знаю
