Java Collection Learning (iii) ArrayList detailed introduction (source analysis) and use of examples

Source: Internet
Author: User
Tags addall int size object object serialization thread

In the previous chapter, we learned about the collection architecture. Beginning with this chapter, we explain the specific implementation classes of collection, first, the list, and the list of ArrayList is most commonly used. Therefore, we explain ArrayList in this chapter. Have a general understanding of ArrayList first, then learn its source code, and finally through the example to learn how to use it.

The 1th part ArrayList introduction

ArrayList Introduction

ArrayList is an array queue, which is the equivalent of a dynamic array. Its capacity can grow dynamically compared to an array in Java. It inherits from Abstractlist, implements the list, randomaccess, cloneable, java.io.Serializable these interfaces.
ArrayList inherited the Abstractlist and realized the list. It is an array queue that provides related additions, deletions, modifications, and traversal functions.
ArrayList implements the Randmoaccess interface, which provides a random access function. Randmoaccess is a list implemented in Java to provide quick access to the list. In ArrayList, we can quickly get the element object by the ordinal of the element, which is the fast random access. Later, we'll compare the efficiency of the list's "Quick Random access" and "access via iterator iterator."
ArrayList implements the Cloneable interface, which covers the function clone () and can be cloned.
ArrayList implements the Java.io.Serializable interface, which means that ArrayList supports serialization and can be transmitted through serialization.

Unlike vectors, operations in ArrayList are not thread-safe. Therefore, it is recommended that ArrayList be used in a single thread, while vectors or copyonwritearraylist can be selected in multiple threads.

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 relationship between ArrayList and collection is as follows:

ArrayList constructors

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

API for ArrayList

Collection is defined in the API Boolean add (E object) Boolean AddAll (collection<? extends E> Collectio n) void Clear () Boolean contains (object) Boolean Containsall (collection<? > Collection) Boolean equals (object) int hashcode () Boolean isempty () It Erator<e> iterator () boolean Remove (Object) Boolean RemoveAll (collection<?             > Collection) Boolean Retainall (collection<?> collection) int size () <T> t[] ToArray (t[] array) object[] ToArray ()//Abstractcollection the API void Add (int locati On, E object) boolean addall (int location, collection<. extends e> Collection) E Get (i NT location) int IndexOf (Object object) int LastIndexOf (Object object) LISTITERATOR&LT;E&G     T Listiterator (intLocation) listiterator<e> Listiterator () e-Remove (int location) e Set (int lo cation, E object) list<e> sublist (int start, int end)//ArrayList new API Object Clone () VO ID ensurecapacity (int minimumcapacity) void trimtosize () void RemoveRange ( int fromindex, int toindex)

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.