In the process of reading List/arraylist source code, the following notes were made. LinkedList pending Update.
List
The List is an interface that inherits from the Collection interface. An interface is a definition of a function that is not specifically implemented. The List interface has several features
1. Duplicate elements can exist. This is not the same as set, and the set interface does not allow duplicate elements to appear.
2. There are four methods that are accessed according to the subscript: get (int), set (int, E), add (int, e), remove (int).
3. Special Iterator--listiterator are provided. Listiterator iterators allow inserting elements, replacing elements, and bidirectional traversal during the traversal of elements. Listiterator features are used less, generally enough to use Iterator.
4. The ToArray () method returns a copy of the list element and operates on the copy without affecting the original list.
ArrayList
1. ArrayList is an implementation of the list interface and is a list implementation based on a variable-size Array.
2. Array is a fixed-size data structure that allows the built-in array size to be variable by requesting a new array space and copying the ArrayList data to a new array.
3. Because of Array-based, the Get (int), which is accessed by subscript only and does not change the structure, can be completed in constant time by the set (int, E). The Add method, on average, is a constant time complexity, i.e. adding n elements takes time O (n). Add (int) is appended at the end, adding more efficiency besides the need to enlarge the built-in Array, and add (int, E) is inserted in the middle, and each time the element behind the insertion point is shifted backwards, the efficiency is slower.
4. Iterator adopts fail-fast design idea. After iterator is returned by iterator () or listiterator (), if you bypass the iterator remove and add and make structural changes to the List, iterator will throw Concurrentmodifica Tionexception exception. Fail-fast is generally manifested in the concurrency situation, in a single-threaded case can also be reproduced, as the following method is executed, will throw Concurrentmodificationexception point to the 14th line of code.
1 Public Static voidMain () {2list<string> list =NewArraylist<>();3 4List.add ("AA");5List.add ("BB");6 7Iterator<string> iter =list.iterator ();8 if(Iter.hasnext ()) {9 System.out.println (Iter.next ());Ten } One AList.add ("DD"); - if(Iter.hasnext ()) { - System.out.println (Iter.next ()); the } -}
5. The declaration implements the Randomaccess interface. Randomaccess is a markup interface, which does not have any member method, member variable declaration, only represents the property with random access. Random access facilitates performance optimizations for some algorithms. In the implementation of the generic algorithm, the instanceof is first used to determine whether the List has random access properties, and then choose a more appropriate algorithm implementation.
[Java] list/arraylist Source code Learning notes