Implementation of the Java List<t> interface: ArrayList

Source: Internet
Author: User
Tags addall

Collection
├list
│LinkedList
ArrayList
│└Vector
│└stack
└set
Map
├Hashtable
├hashmap
└weakhashmap

        ArrayList is one of the self-implemented classes of Java under List<t>, which belongs to the non-secure type of thread. In a multithreaded environment, consider using the Collections.synchronizedlist (List L) function to return a thread-safe ArrayList class, or you can use the Copyonwritearraylist class under the concurrent concurrency package. You can also consider using the vector type.

ArrayList's internal storage structure is an extensible dynamic array ( in fact, there is no dynamic extension, just an ordinary array, when the array is filled, then apply for a new array structure, and then copy the value of the past, the new size is 1.5*n+1 ) .

       Private transient Object[] Elementdata;  

ArrayList implements the serializable interface, so it supports serialization, can be serialized transmission, the implementation of Randomaccess interface, support fast random access, in fact, through the subscript sequence number for fast access, the realization of the Cloneable interface, can be cloned.

Each ArrayList instance has a capacity, which refers to the size of the array used to store the list elements. It is always at least equal to the size of the list. As you add elements to the ArrayList, their capacity increases automatically. Autogrow leads to a re-copy of the data to the new array, so if you can predict how much data is available, you can specify its capacity when constructing ArrayList. Before adding a large number of elements, the application can also use the ensurecapacity operation to increase the capacity of the ArrayList instance, which reduces the amount of incremental redistribution, which is already mentioned in parentheses.

In addition, ArrayList uses the fail-fast mechanism: it is realized by recording the Modcount parameters. in the face of concurrent modifications, the iterator will soon fail completely, rather than risking arbitrary, indeterminate behavior at some uncertain time in the future.

The following is a description of the ArrayList constructor, which comes with three constructor functions:

(1) You can construct a default empty list with an initial capacity of 10 public ArrayList ()

(2) Constructs an empty list of the specified initial capacity public ArrayList (int initialcapacity)

(3) Constructs a list of elements that contain the specified Collection (in ArrayList Order of iterations in Collection) publicArrayList (collection<? extends e> c)

 

Here are the common APIs

1, increase

        PublicE Set (intIndex, E Element)//replaces the old node with the new nodes at the specified location, and returns the old junction .        Public BooleanAdd (e E)//adds a new element to the queue, which will be placed at the very end of the queue        Public voidAddintIndex, E Element)//inserts an element at the specified position, and the original element is then pushed backwards, noting that index cannot be greater than the current length of the queue        Public BooleanAddAll (collection<?extendsE> c)//In the order of the collection iterators, place the elements in the collection at the end of the queue, in turn        Public BooleanAddAll (intindex, COLLECTION&LT;?extendsE> c)//in the order of the collection iterators, sequentially//Place the elements in the collection on the specified index of the queue, and the original elements are pushed off one at a time

Alternatively, you can convert the original queue to an array form

Public

2. By deleting

 Public E Remove (int index)// Delete the element at the specified location Publicboolean remove (Object o)//  Delete the first element equal to obj (as determined by the Equals method)

It can also be removed in the following form:

protected void removerange (intint toindex)

This method has the following points to note
1, the method protected type, 2, will fromindex to toindex this segment of the node are removed, follow-up node. 3. The length of the queue changes to index, that is, the elements after the toindex position are removed.

You can also use this method to implement a form similar to Rangemove

         New Arraylist<integer> (arrays.aslist (0, 1, 2,3, 4, 5, 6));           Ints.sublist (2, 4). Clear ();  

3, change

(1) Modify the maximum volume of the queue

As mentioned earlier, when the volume of an array is full, we re-set the volume of the array to meet the need to continue adding

Array expansion is implemented by a public method ensurecapacity (int mincapacity), and we can call this method manually to increase the volume

When the minimum volume set is less than 1.5 times times the original volume, we will replace it by 1.5 times times the old volume, otherwise it will be replaced with the size of the parameter, creating a new array.

Since the volume is set, the copy method of the array is called once, it is very expensive here, so it is best to pre-order the size of the arrays as early as possible (when the elements are young), to prevent a large number of new elements, and constantly call the expansion volume and replication methods.

 Public void ensurecapacity (int mincapacity)

(2) Sometimes, the size of the ArrayList is just good, the internal dynamic array of more than half a little, nearly half of the array elements are idle. Or when we start to set the space too large, we can use the following methods to correct the size of the dynamic array

 Public void trimtosize ()

4. Check

 Public E get (int index)//  Returns the element at the specified position in this list. 


Reference: http://zhangshixi.iteye.com/blog/674856

Http://www.cnblogs.com/ITtangtang/p/3948555.html

Implementation of the Java List<t> interface: ArrayList

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.