ArrayList array List collection interface and list interface differences
- The list interface expands the collection interface and adds an index-dependent method.
code example
Object get (int index) object Set (int index,object element) int indexOf (Object elem) void Add (int index,object Element) Object Remove (int index)
Most of the methods in the list interface are index-based.
ArrayList class
1, can be regarded as a modified version of a one-dimensional array, support random access, ArrayList the size of the object automatically adjusted. 2. The relative position of each element in the ArrayList object is represented by an index, the index range 0~n-1 3, and the element time at which the index is accessed is the constant level, given an index. 4, delete the element, the worst case is O (n-index), so it takes less time to delete the elements on the back. 5, insert the element, the worst case is O (n-index), so insert the end of the operation takes less time.
Source method Analysis (specific to Java documnet)
Public ArrayList Object (int initialcapacity); Specifies the size of the initialization capacity
Public boolean Add (Object o); Insert element at end
public int size (); Returns the number of valid elements for a container
......
Serializable of ArrayList objects
ObjectOutputStream
Cloning of ArrayList objects
Implements the Clonable interface method:
Public Object clone ();
A reference to an element in a ArrayList object is copied, not an object, so it is called a shallow copy.
Public class Arraylistdemo {public static void Main (string[] args) { arraylist<integer> list=new Arraylist<integer> (3); List.add (1); List.add (2); System.out.println ("List size=" +list.size ()); Arraylist<integer> temp= (arraylist<integer>) List.clone (); SYSTEM.OUT.PRINTLN ("Temp size=" +temp.size ()); www.90168.org does not change the Clone object List.add (3); SYSTEM.OUT.PRINTLN ("list result size=" +list.size ()); SYSTEM.OUT.PRINTLN ("temp result size=" +temp.size ());} } /**list size=2*temp size=2*list result size=3*temp result size=2*/
ArrayList expansion, time of allocation
1, the array general expansion 50% 2, the Add () method to set Newcapacity (oldcapacity*3)/2+1 has no practical meaning, just for the balance of time and space.
Fail-first iterators
1, Inherit the Abstractlist modcount field Add (), remove () Modcount plus 1 once the next () method is active, you cannot modify the ArrayList.
public class ArrayListDemo2 {public static void Main (string[] args) { arraylist<integer> list=new Arraylist<integer> (); List.add (2); List.add (3); Iterator it=list.iterator (); while (It.hasnext ()) { int ele= (int) it.next (); The modification will throw the exception List.add (1, 4); System.out.println (Ele);}}}
The difference from the vector class
1, Vector class is also based on the implementation of the array 2, vector class methods are mostly synchronous.
ArrayList array List