The other day, I was asked if I could understand the difference between ArrayList and LinkedList, and I couldn't answer, so I will post it as a memorandum.
ArrayList is a list structure that uses arrays internally. The difference from the standard array is that you can resize it later. By its nature, reading and writing values by index value is fast, but inserting / deleting elements becomes slower as the array size increases and the operation position approaches the beginning.
·merit Each element has a sequence number and is all indexed in memory so you can quickly access a particular element.
·Demerit When an element other than the end is added or subtracted, a rearrangement process is performed to move up or down the sequence number of all subsequent elements. As a result of that process, all the elements in the ArrayList are neatly arranged with all sequence numbers from 0 to the total number of elements minus 1, but it takes time for the process to be performed. I will. And the number of processes increases in proportion to the number of elements.
Method | Overview |
---|---|
add([int index,] E e) | Insert element e at the specified position index (insert at the end without index omitted) |
clear() | Remove all elements from list |
contains(Object e) | Determine if the list contains element o |
get(int index) | be |
indexOf(Object e) | Get the first index value where element o appears |
isEmpty() | Determine if the list is empty |
remove(int index|Object o) | Delete the specified element |
set(int index, E e) | Set index th element int size() |
int size() | Get the number of elements in the list |
LinkedList represents a linked list that references elements with bidirectional links. Due to its nature, inserting / deleting elements is faster than ArrayList because it only requires relinking. On the other hand, it has the property that it is not good at random access by index value. For this reason, LinkedList will be used in situations where there are many insert / delete operations, and ArrayList will be used in other cases. The methods that can be used in the LinkedList class are the same as ArrayList, so please refer to that as well. The following is an example of setting elements in LinkedList and reading their contents in order.
·merit When adding or subtracting elements, rewriting the link information is the end, so it is faster than ArrayList because the relocation process is not performed.
·Demerit Since each element does not hold a sequence number, when retrieving a specific element, it is necessary to count the sequence one by one from the beginning or the end, so compared to the ArrayList that holds the sequence number in advance. , It will take a lot of time.
ArrayList is used when random access is required for elements and there is not much need for insert / delete operations for elements in the array. LinkedList is used when the addition / deletion process is frequently required in addition to the last element and access to a specific element is not required.
How to use LinkedList and ArrayList properly ArrayList class
Recommended Posts