JAVA ArrayList VS background list

Source: Internet
Author: User

JAVA ArrayList VS background list

 

The relationships between concepts are as follows:

Illustration:

List is an interface that inherits from the Collection interface. It represents an ordered queue. It allows repeated elements.
AbstractList is an abstract class that inherits from AbstractCollection. AbstractList implements functions except size () and get (int location) in the List interface.
AbstractSequentialList is an abstract class that inherits from AbstractList. It has fewer methods. Designed specifically for the shortlist.
ArrayList, struct List, Vector, and Stack are four implementation classes of List.

Background 1:

ArrayList stores objects through an Object [] array. Note that this array is of the transient type. Its add () and remove () methods are implemented through System. arraycopy (...). The get () method is an element of the returned array.

 

AbstractSequentialList further implements the add (), get (), and remove () methods. It calls the corresponding methods in the ListIterator class to implement these methods. This aims to improve the execution speed through the bidirectional list feature. It declares the listIterator (int index) method as abstract type, requiring users to implement it again.

The shortlist inherits from AbstractSequentialList and stores objects through a two-way linked list. Note that the header of this linked list is of the transient type. The header does not contain actual data. It is only used as the head node of the linked list. Its add (), get (), and remove () methods are operations on two-way linked lists. It implements the listIterator (int index) method and defines the embedded ListIterator class through the bidirectional linked list feature.

Background 2:

ArrayList is an array of variable sizes. when more elements are added to the ArrayList, their size will increase dynamically. internal elements can be accessed directly through the get and set methods, because ArrayList is essentially an array.
A pair list is a double-stranded table. It has better performance than ArrayList when adding or deleting elements, but it is weaker than ArrayList in get and set.

Source code:

ArrayList source code Abstract:

// Size,IsEmpty,Get,Set,Iterator, AndListIteratorAnd other operations are constant time. add operations are close to the constant time.ArrayListAn instance has a property: capacity. capacity is at least as large as size, and it automatically grows. It indicates the number of elements that ArrayList can store.

 

 

Private static final intDEFAULT_CAPACITY = 10; // default capacity.
 
Private static finalObject [] EMPTY_ELEMENTDATA ={}; // capacity indicates the size of the array buffer.
Private transientObject [] elementData; // This is the main element of arraylist: array.
 
 
Public voidEnsureCapacity (IntMinCapacity); // This method and subsequent methods are used to operate capacity, which can increase capacity.
 
Private voidGrow (IntMinCapacity) // This method shows the size of each scale-out, basically equivalent to: newCapacity = oldCapacity + (oldCapacity> 1 );
 
Public intIndexOf (Object o) // searches for objects in sequence. Linear Time
 
PublicE get (IntIndex) // index to obtain the object. Constant time
 
PublicE set (IntIndex, E element) // Linear Time
 
Public booleanAdd (E) // equivalent to linear time
 
Public voidAdd (IntIndex, E element) // insert, linear time
 
PublicE remove (IntIndex) // Delete, linear time








 


 

 


 

 

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.