Java ArrayList Detailed introduction and usage examples

Source: Internet
Author: User
Tags addall object object serialization

An overall understanding of ArrayList

ArrayList is an array queue, which is equivalent to a dynamic array. Its capacity can grow dynamically compared to the array in Java. It inherits the Abstractlist and implements the list,randomaccess,cloneable,java.io.serializable of these interfaces.

ArrayList inherits the Abstractlist and implements the list. It is an array queue that provides related additions, deletions, modifications, and traversal functions.

ArrayList implements the Randomaccess interface, which provides a random access function, randomaccess is used in Java to be implemented by the list, providing quick access to the list. In ArrayList, we can quickly get the element object by the ordinal of the element, which is fast random access. Later, we will compare the efficiency of the list's fast random access and access through the iterator iterator.

ArrayList implements the Cloneable interface, which covers the function clone () and can be cloned.

The ArrayList implements the Java.io.Serializable interface, which means that the ArrayList supports serialization and can be transmitted by serialization.

Unlike vectors, the operations in ArrayList are not thread-safe, so it is recommended that you use ArrayList in a single thread, and you can select vectors or copyonwritearraylist in multiple threads.

ArrayList constructor function

The default constructor ArrayList ()//capacity is the default capacity size of ArrayList. When capacity is insufficient due to increased data, the capacity adds half of the previous capacity. ArrayList (int capacity)//Create a arraylistarraylist that contains Collection (collection<? extends e> Collection)

  

ArrayList's API

Collection defined in Apiboolean Add (E object) Boolean AddAll (collection<? extends e> Collection) v OID Clear () Boolean contains (Object object) Boolean Containsall (collection<?> C Ollection) Boolean equals (Object object) int Hashcode () boolean isEmpty () iterator< E> iterator () boolean remove (Object object), Boolean RemoveAll (collection<?> collect Ion) Boolean Retainall (collection<?> Collection) int size () <T> t[] Toarr Ay (t[] array) object[] ToArray ()//Abstractcollection defined in apivoid Add (int location, E Object) Boole                 An AddAll (int. location, collection<? extends e> Collection) E get (int. location) int  IndexOf (Object object) int LastIndexOf (Object object) listiterator<e> listiterator (int Location) LISTITERATOR&LT E> listiterator () e Remove (int location) e Set (Int. location, E object) LIST&LT;E&G             T sublist (int start, int end)//ArrayList new Apiobject clone () void ensurecapacity (int minimumca pacity) void TrimToSize () void removerange (int fromIndex, int toindex)

 

The inheritance relationship of ArrayList 

Java.lang.Object   ?     Java.util.abstractcollection<e>         ?     Java.util.abstractlist<e>               ?     Java.util.arraylist<e>public class Arraylist<e> extends abstractlist<e>        implements List<e Randomaccess, cloneable, java.io.Serializable {}

 

The ArrayList contains two important objects: Elementdata and size.

Elementdata is an array of type "object[" that holds the elements added to ArrayList. In fact, Elementdata is a dynamic array, and we can execute its initial capacity through the constructor ArrayList (int initialcapacity), if it is initialcapacity through a constructor without arguments ArrayList () To create a ArrayList, the Elementdata capacity defaults to 10. Elementdata array size will be based on the growth of ArrayList capacity and dynamic growth, specific growth mode, please refer to the source code analysis in the Ensurecapacity () function.

() size is the actual size of the dynamic array.

ArrayList source code to make analysis

Http://www.cnblogs.com/skywang12345/p/3308556.html

Java ArrayList Detailed introduction and usage examples

Related Article

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.