Constructors in ArrayList in Java
In order to Create an ArrayList, we need to create an object of the ArrayList class. The ArrayList class consists of various constructors which allow the possible creation of the array list. The following are the constructors available in this class:
1. ArrayList()
This constructor is used to build an empty array list. If we wish to create an empty ArrayList with the name arr, then, it can be created as:
ArrayList arr = new ArrayList();
2. ArrayList(Collection c)
This constructor is used to build an array list initialized with the elements from the collection c. Suppose, we wish to create an ArrayList arr which contains the elements present in the collection c, then, it can be created as:
ArrayList arr = new ArrayList(c);
3. ArrayList(int capacity)
This constructor is used to build an array list with the initial capacity being specified. Suppose we wish to create an ArrayList with the initial size being N, then, it can be created as:
ArrayList arr = new ArrayList(N);
ArrayList in Java
Java ArrayList is a part of the Java collections framework and it is a class of java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package. The main advantage of ArrayList in Java is, that if we declare an array then we need to mention the size, but in ArrayList, it is not needed to mention the size of ArrayList. If you want to mention the size then you can do it.
Table of Content
- What is ArrayList in Java?
- Java ArrayList Example
- Important Features of ArrayList in Java
- Constructors in ArrayList
- ArrayList in Java methods
- Operations performed in ArrayList
- Complexity of Java ArrayList
- Advantages of Java ArrayList
- Disadvantages of Java ArrayList
- Conclusion
- FAQs of ArrayList