Home > Java > JavaBase > body text

How to initialize the ArrayList collection in java

王林
Release: 2019-11-26 15:13:01
forward
2660 people have browsed it

How to initialize the ArrayList collection in java

Overview

ArrayList is a non-thread-safe collection based on dynamic arrays. The elements of ArrayList can be empty and repeated. , and at the same time it is ordered (the order of reading and storage is consistent).

ArrayList inherits AbstractList and implements List, RandomAccess (can be accessed quickly), Cloneable (can be cloned), java .io.Serializable (supports serialization)

More free related video recommendations:java video

There are three ways to initialize ArrayList:

1. No-parameter construction, the default length is 10, which is the most commonly used initialization method:

/**
  * Constructs an empty list with an initial capacity of ten.
  */
  public ArrayList() {
      this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
  }
Copy after login

At this time, we start from the source code As you can see, there is only one line of code in it: this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA, then the defined DEFAULTCAPACITY_EMPTY_ELEMENTDATA can be found in the source code:

/**
  * Shared empty array instance used for default sized empty instances. We
  * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
  * first element is added.
  */
 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
Copy after login

It can be known from the comments that an empty array is defined in the source code as the default size , and determine how much to expand the array when the first element is added. This logic will be explained in the next section of adding elements.

2. Specify the initialization length:

/**
  * Constructs an empty list with the specified initial capacity.
  * @param  initialCapacity  the initial capacity of the list
  * @throws IllegalArgumentException if the specified initial capacity
  *         is negative
  */
  public ArrayList(int initialCapacity) {
     if (initialCapacity > 0) {
         this.elementData = new Object[initialCapacity];
     } else if (initialCapacity == 0) {
         this.elementData = EMPTY_ELEMENTDATA;
     } else {
         throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
     }
  }
Copy after login

3. Use a Collection object to construct

/**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }
Copy after login

Related article tutorial recommendations :Introduction to java development

The above is the detailed content of How to initialize the ArrayList collection in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!