Java's ArrayList class

A, ArrayList Overview

  java.util.ArrayList is a variable-size array implemented, including the data storage element is called. This class is a collection class (container) , it allows us to be more convenient to store and manipulate data objects.

  This class can continue to add elements, its size also automatically increase to make up for this shortcoming array of fixed length.

Two, ArrayList using procedure

  1, View class

java.util.ArrayList <E>: after introduction of such use need to import

  <E> represents one of the specified data type, called generic . E, from the first letter of the Element (element), the appearances <E>, the use of a reference data type which can be replaced, which represents a memory reference type of elements of this set.

  2, view the constructor

    public ArrayList (): a content is configured to empty set.

    The basic format:

ArrayList<String> list = new ArrayList<String>();

      After JDK 7, the right angle brackets within the generic may be left blank, but <> still write. Simplified format:

ArrayList<String> list = new ArrayList<>();

Third, the common method

  For operation element reflects substantially - to add, delete, search. Common methods are as follows:

public boolean add (E e): the specified element to the end of the collection 
public E remove (int index): the position of the element to remove this collection specified. Returns the removed element 
public E get (int index): Returns the collection element at the specified location. Returns the element acquired 
public int size (): Returns the number of elements in this set. Traversing a collection index range may be controlled, to prevent cross-border

Fourth, through the collection

V. storing basic data types

  ArrayList object can not be stored basic types, the type of data stored reference only. Similar <int> can not write, the memory corresponding to the basic data types of packaging are possible. So, you want to store basic data types, in order to write the data type in <>, must be converted.

  Basic Package Type: Basic Package Type

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11421117.html