Constructor ArrayList java

Constructors for ArrayList Objects

To declare a reference variable for an ArrayList do this:


ArrayList myArray; // myArray is a reference to a future ArrayList object // that will hold references to objects of type E // "E" stands for any class name, for eg. "String"

The future ArrayList object will contain an array of references to objects of type E or to a descendant class of E.

To declare a reference variable and to construct an ArrayList, do this:


ArrayList myArray = new ArrayList[]; // myArray is a reference to an ArrayList // that holds references to objects of type E

The array has an initial capacity of 10 cells, although the capacity will increase as needed as references are added to the list. Cells will contain references to objects of type E [or a descendant class]. This may not be very efficient. If you have an idea of what the capacity you need, start the ArrayList with that capacity.

To declare a variable and to construct a ArrayList with a specific initial capacity do this:


ArrayList myArray = new ArrayList[ int initialCapacity ];

The initial capacity is the number of cells that the ArrayList starts with. It can expand beyond this capacity if you add more elements. Expanding the capacity of an ArrayList is slow. To avoid this, estimate how many elements are needed and construct an ArrayList of that many plus some extra.



Video liên quan

Chủ Đề