High-intensity learning and training fifteenth day summary: ArrayList and LinkedList difference

一、ArrayList

ArrayList can handle is a variable length array type, here not limited to "number" group, ArrayList is a generic class, can hold any type of object. As the name implies, it is an array ArrayList list, so its internal use is an array to store the object, because the Object is the parent class of all types, and therefore there is a ArrayList internal array of type Object class object stored. Commonly used methods are ArrayList class add (), clear (), get (), indexOf (), remove (), sort (), toArray (), toString (), etc., while a private ArrayList internal class that implements the interface Iterator , so you can use the iterator () method to get ArrayList iterator, at the same time, there is a private class implements ListIterator interface, ArrayList can also call listIterator () method to get ListIterator iterators.

Since ArrayList rely array to store objects, but packaging it up, so look for the efficiency of some of its methods is O (n), with almost ordinary array efficiency, but this is an ArrayList variable "array" and you can store all the specified object.

In addition, since all ArrayList methods are carried out by default in a single thread, so ArrayList is not thread safe. If you want to use in a multi-threaded, you should use the static method synchronizedList Colletions class () for ArrayList to call.

Two, LinkedList

LinkedList can see as a doubly linked list, all operations can be considered to be a doubly linked list of operations because it implements the Deque interface and the List interface. Similarly, LinkedList is thread safe, if you use it in a concurrent environment, the same static method synchronizedList Colletions class () can be called for LinkedList.

LinkedList implemented internally in, but not with normal array to store data, but the use of the node To store data, there is a first node pointing to a list head and tail of the last node point list. Unlike ArrayList only add data to the end of the array, LinkList can be easily inserted in the data list head or end of the list, or insert the data before and after the specified node is removed also provides a list head or tail of the linked list node, or removed an intermediate node, you can also check whether a node exists. add () method in the default list data is inserted tail. In short, LinkedList provides a number of convenient methods of operation, and its insertion or other methods to increase the efficiency significantly higher than ArrayList type, but the efficiency of the query to be lower, because it is a doubly linked list.

Therefore, the maximum difference between LinkedList and ArrayList are LinkedList more flexible and efficient part of the method is much more efficient than ArrayList corresponding method, for the case where the data frequented, and requires the operator to be flexible enough to recommend LinkedList; For array changes small, mainly used to query the case, you can use ArrayList.

Guess you like

Origin www.cnblogs.com/godoforange/p/11610215.html