notes
ArrayList is based on arrays and LinkedList is based on linked lists
ArrayList, good at query, small space
LinkedList, good at insert and delete, large space
Using an ArrayList requires an appropriate initial size, because expansion costs a lot
The original
-
Because Array is an index-based data structure, it is fast to search and read data through an Array using an index. The time to fetch the Array is O(1), but deleting the data is expensive because it requires reordering all the data in the Array.
-
LinkedList insertion is faster than ArrayList. Because unlike ArrayList, LinkedList doesn’t need to change the size of the array or reload all the data into a new array when the array is full, which is the worst case of ArrayList, order (n) time, The time complexity of insert or delete in LinkedList is only O(1). An ArrayList also needs to update the index when inserting data (except at the end of the array).
-
Similar to inserting data, LinkedList is superior to ArrayList when deleting data.
-
LinkedList requires more memory because the location of each index in ArrayList is the actual data, whereas each node in LinkedList stores the actual data and the location of the surrounding nodes (an instance of LinkedList stores two values: Each Node instance stores three values: E item,Node Next, and Node pre.
When is it better to use LinkedList instead of ArrayList
1) Your app doesn’t randomly access data. Because if you need the NTH element in the LinkedList, you need to count the data from the first element to the NTH element, and then read the data.
2) Your application inserts and removes more elements and reads less data. Because inserting and deleting elements does not involve rearranging data, it is faster than ArrayList.
So that’s the difference between ArrayList and LinkedList. Use ArrayList whenever you need unsynchronized index-based data access. ArrayList is fast and easy to use. But remember to give an appropriate initial size to minimize the size of the array.
Links:www.nowcoder.com/questionTer…
Source: Niuke.com