Initialize an ArrayList in Java

Cheav Sovannarith
Java Epic
Published in
1 min readJul 22, 2019

How we initialize ArrayList in java

Photo by A. L. on Unsplash

ArrayList is a class which extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

  1. With add()

2. With Double Brace

3. With Shared Reference

This will create a immutable list

4. From Array

This produce a fixed size list

5. From a Stream interface

6. From Immutable list factory method in java 9

This will create a fixed immutable list up to 10 elements

the new factory methods for Collections introduced in Java 9 creating unmodified collections.

Hope this article help you a lots 🐵!

--

--